coupon code description magento on cart - magento

how do we fetch the coupon code description to the shopping cart. Currently magento shows Coupon code "test001" applied.
I am wondering how do we fetch the description we added in magento backend instead of that message.
display message like "Coupon code "test001" has been applied, you just saved $100 happy shopping."
Magento get coupon description I was looking this but it didn;t worked for me .

Try this code .It works for me
$rule = Mage::getModel('salesrule/rule')->getCollection()
->addFieldToFilter('code', '1234');
foreach ($rule as $value) {
$description = $value->getDescription();
print_r($description);
}.
I put this code in cart controller under the addAction() function.

codes submited by Mahmood Rehman works, even in 1.7.0.2.
change "app/code/core/Mage/Checkout/controllers/CartController.php"
in public function couponPostAction()
look for
if ($couponCode == $this->_getQuote()->getCouponCode()) {
$this->_getSession()->addSuccess(
$this->__('Coupon code "%s" was applied.', Mage::helper('core')->htmlEscape($couponCode)) . ' '.$description
);
}
change it to
if ($couponCode == $this->_getQuote()->getCouponCode()) {
$description = '';
try {
$rule = Mage::getModel('salesrule/rule')->getCollection()
->addFieldToFilter('code', 'YOURCOUPONCODE');
foreach ($rule as $value) {
$description = $value->getDescription();
}
} catch (Exception $e) {
Mage::logException($e);
}
$this->_getSession()->addSuccess(
$this->__('Coupon code "%s" was applied.', Mage::helper('core')->htmlEscape($couponCode)) . ' '.$description
);
of course this could be improved, but does the job.
Regards,

Related

Magento - Script to copy attributes from SKU and add suffix

i work on Magento 1.8.1CE
But now i need to replace a lot of values ....
Everything from attributes
SKU must be copied to Artikelnummer
And all vakues from Barcode (when not empty) should be copied to SKU with a suffix -kd
Could any help me writting a script for this?
This should do the trick. Be sure to run on a testing environment first. When it is finished check the file var/log/system.log for any errors.
<?php
require_once 'abstract.php';
class Upgrade extends Mage_Shell_Abstract
{
public function run()
{
echo 'Starting upgrade script...' . PHP_EOL;
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('Artikelnummer')
->addAttributeToSelect('Barcode');
foreach ($products as $product)
{
$artikelnummer = $product->getArtikelnummer();
$barcode = $product->getBarcode();
if (!$artikelnummer || !$barcode)
{
Mage::log('Unable to update product. Skipped. SKU = ' . $product->getSku());
continue;
}
// set Artikelnummer value = SKU
$product->setArtikelnummer($product->getSku());
// set SKU = barcode + -kd suffix
$newBarcode = $barcode . '-kd';
$product->setSku($newBarcode);
$product->save();
}
echo 'Finished' . PHP_EOL;
}
}
$shell = new Upgrade;
$shell->run();

first time customer free shipping on your first order

My Question:
How to logical and programmatic develop for this requirement.
My requirement that : I need free shipping on customer first order. I have not any used coupon code or discount.
I have directly set free shipping when customer sign up and place order.
I have create one custom shipping methods then add my custom code for above requirment.
Shipping methods URL: following this url
http://inchoo.net/magento/custom-shipping-method-in-magento/
then added my code in carrier.php file like below.
$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) {
$customer = Mage::getSingleton('customer/session')->getCustomer();
//echo '<pre>';
//print_r(get_class_methods($customer));
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $customer->getId());
if (!$orders->getSize())
{
$result->append($this->_getFreeRate());
return $result;
}
}else{
if ($expressAvailable) {
$result->append($this->_getExpressRate());
}
$result->append($this->_getStandardRate());
return $result;
}

Magento how to create shipping label dynamically

I need to generate shipping label dynamically once order is placed.
I have fedex shipping method configured and works fine for setting the order status to shipping once order placed and from admin am able to create shipping label manually and it is giving pdf when i click Print Shipping label after shipping label created.
Now this process needs to be automated - how i can able to create hipping labels dynamically? Is there any observer or class overwriting examples. Please help me to create shipping label dynamically
If you want an official Shipping Label with bar code you will need to purchase an extension like this one:
http://www.cobbconsulting.net/magento-fedex-extension.html
The extension has a cahcing feature where it will store all of your shipping labels so you can easily re-print them at anytime.
As know, you can print shipping label only when have invoice. We want let you know extension can help you solved. Or you can check our code for create
Magento 1: http://www.mlx-store.com/magento-extensions/shipping/print-shipping-label.html
Magento 2: http://www.mlx-store.com/magento2-extensions/shipping/print-shipping-label-for-magento-2.html
or use code
Below is the controller.
public function printShippingLabelAction(){
$ids= $this->getRequest()->getPost('order_ids');
if (!empty($invoicesIds)) {
$orders = Mage::getResourceModel('sales/order')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $ids))
->load();
if (!isset($pdf)){
$pdf = Mage::getModel('sales/order_pdf_order')->getPdf($orders );
} else {
$pages = Mage::getModel('sales/order_pdf_order')->getPdf($orders );
$pdf->pages = array_merge ($pdf->pages, $pages->pages);
}
return $this->_prepareDownloadResponse('order'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
'.pdf', $pdf->render(), 'application/pdf');
}
$this->_redirect('*/*/');
}
Create model
class Mage_Sales_Model_Order_Pdf_Order extends Mage_Sales_Model_Order_Pdf_Invoice
{
public function getPdf($orders = array())
{
$this->_beforeGetPdf();
$this->_initRenderer('order');
$pdf = new Zend_Pdf();
$this->_setPdf($pdf);
$style = new Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($orders as $order) {
$page = $this->newPage();
$this->insertLogo($page, $order->getStore());
$this->insertAddress($page, $order->getStore());
$this->insertOrder(
$page,
$order,
Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
);
$this->insertDocumentNumber(
$page,
Mage::helper('sales')->__('Order # ') . $order->getIncrementId()
);
$this->_drawHeader($page);
foreach ($order->getAllItems() as $item){
if ($item->getOrderItem()->getParentItem()) {
continue;
}
$this->_drawItem($item, $page, $order);
$page = end($pdf->pages);
}
$this->insertTotals($page, $order);
if ($order->getStoreId()) {
Mage::app()->getLocale()->revert();
}
}
$this->_afterGetPdf();
return $pdf;
}}

Category Import in Magento without products

I want to import data related to categories first and then products. But when i am going to upload only category csv file it is showing error "sku not found" and when i am trying to import merged data of category and products csv it is importing products but not showing any category and in products showing 0 record found and in import showing upload success message. Can anyone please help me.
You have to import categories alone.
Put all information in your csv, then go through a custom script like this one :
(create a script directory in your root Magento folder, and a category.php file in this location).
then you'll be able to import your category by going to yoursite.com/script/category.php
You can find example here :
http://www.magentoworks.net/import-bulk-category-in-magento
Regards,
Please refer my tutorial which create category and subcategory using the magento script.
http://www.pearlbells.co.uk/import-the-categories-programmatically/
foreach ($arrResult as $import_category) {
try {
if (strtolower($import_category[16]) == 'true') {
$enabled = 1;
} else {
$enabled = 0;
}
if ($import_category[1] == 0) {
$parentId = '2';
}
else {
$parentId = $list[$import_category[1]];
}
$category = Mage::getModel('catalog/category');
$category->setName($import_category[2]);
$category->setMetaTitle($import_category[2]);
$category->setIncludeInMenu(1);
$category->setUrlKey($import_category[10]);
$category->setDescription(strip_tags($import_category[11]));
$category->setMetaDescription($import_category[12]);
$category->setMetaKeywords($import_category[13]);
$category->setIsActive($enabled);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->setCustomUseParentSettings(true);
$category->setImage($import_category[6]);
$category->save();
$list[$import_category[0]] = $category->getId();
echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
} catch (Exception $e) {
echo 'Something failed for category ' . $import_category[2] . PHP_EOL;
print_r($e);
}
}

Magento - Adding products to cart using a loop

I get a request from an extrenal site containing some product ids.
In my module i try to load the products and add them to the shopping cart. I tried it with this code:
public function indexAction() {
$ids = explode(',', $this->getRequest()->getParam('products'));
Mage::log('ADDING PRODUCTS');
$cart = Mage::getModel('checkout/cart');
$cart->init();
$pModel = Mage::getSingleton('catalog/product');
//$cart->addProductsByIDs($ids);
foreach ($ids as $id) {
Mage::log('Loading: ' . $id);
$product = $pModel->load($id);
Mage::log('Loaded: ' . $product->getId());
try {
$cart->addProduct($product, array('qty' => '1'));
} catch (Exception $e) {
Mage::log($e);
continue;
}
}
$cart->save();
if ($this->getRequest()->isXmlHttpRequest()) {
exit('1');
}
$this->_redirect('checkout/cart');
}
I can see in the system.log that it loads the products correctly. But after the redirect i have the second product in my cart twice. The first one is missing. Using $cart->addProductsByIDs($ids) works great but then i cant influence the quantity of the products anymore.
Does someone know what i am doing wrong and give me a hint?
Thx
I had the same problem and I fixed it by loading the product model inside every loop:
public function AddMultipleItemsAction() {
$products = explode(',', $this->getRequest()->getParam('products'));
$quantities = explode(',', $this->getRequest()->getParam('quantities'));
$numProducts = count($products);
$cart = $this->_getCart();
for($i=0;$i<$numProducts;$i++) {
$product_id = $products[$i];
$quantity = $quantities[$i];
if ($product_id == '') continue;
if(!is_numeric($quantity) || $quantity <= 0) continue;
$pModel = Mage::getModel('catalog/product')->load($product_id);
if($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
try {
$eventArgs = array(
'product' => $pModel,
'qty' => $quantity,
'additional_ids' => array(),
'request' => $this->getRequest(),
'response' => $this->getResponse(),
);
Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);
$cart->addProduct($pModel, array('product'=>$product_id,'qty' => $quantity));
Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);
Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$pModel));
$message = $this->__('%s was successfully added to your shopping cart.', $pModel->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
} catch (Mage_Core_Exception $e) {
if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
Mage::getSingleton('checkout/session')->addNotice($pModel->getName() . ': ' . $e->getMessage());
}
else {
Mage::getSingleton('checkout/session')->addError($pModel->getName() . ': ' . $e->getMessage());
}
} catch (Exception $e) {
Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart'));
}
}
}
$cart->save();
$this->_getSession()->setCartWasUpdated(true);
$this->_redirect('checkout/cart');
}
I then have an "Add all items to cart" button which executes the following Javascript code:
<script type="text/javascript">
function addAllItemsToCart() {
productsArr = new Array();
quantitiesArr = new Array();
$$('#product-listing-table .qty').each(
function (input, index) {
productsArr[index] = encodeURIComponent(input.readAttribute('product_id'));
quantitiesArr[index] = encodeURIComponent(input.value);
}
);
var url = '/MyModule/Cart/AddMultipleItems/products/'+productsArr.join(',')+'/quantities/'+quantitiesArr.join(',')+'/';
setLocation(url);
}
For this to be able to work, I put an extra product_id attribute on the quantity textbox, such as:
<input type="text" size="2" product_id="<?php echo $_product->getId();?>" name="productqty_<?php echo $_product->getId();?>" class="qty" />
and the whole list of products is inside a div with ID product-listing-table
Just checked with some custom add to cart code that I've got, and confirmed is working correctly, and the only difference I have is:
$cart->addProduct($product, array(
'qty' => 1,
'product' => $product->getId(),
'uenc' => Mage::helper('core')->urlEncode(Mage::helper('core/url')->getCurrentUrl())
));
// Also make sure we know that the cart was updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
That said, your error doesn't make it sound like you're actually having trouble in this area. I can't imagine it would be this, but is it possible that the cart model needs to be save()d every time you add a product to the cart? It's worth a punt.

Resources