Magento get coupon description - magento

I am trying to retrive the text set in the Magento Coupon Description field to use as part of a validation rule. Does anyone know how to load a coupon price rule using the coupon code and retrieve the associated coupon description text?

Under Magento 1.3, you can use this code (not tested as I have no 1.3 within easy reach) :
$rule = Mage::getModel('salesrule/rule')->load($code, 'coupon_code');
if ($rule->getId()) {
$description = $rule->getDescription();
}

I have used in magento 1.9 and below code is working fine for me.
$oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
$message = $oRule->getData();
$description = $message['description'];
$this->_getSession()->addError(
$this->__($description, Mage::helper('core')->escapeHtml($couponCode))
);

$oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code');
$oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
var_dump($oRule->getData());
you can refer for same Magento - get rule from coupon code

Related

Get Custom Product Information Value

I have amazon import script in my magento store. And each product has "Amazon Import Products" inside Product Information (Near General, Prices, Meta Information, Images...).
What I'm trying to get using PHP is located in "Amazon Import Products" And the value of "Amazon Product URL"
Here is my code to choose the product by SKU:
$sku = $id;
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$amazonlink =
Can someone help? The only thing I can find online is how to get product name or images etc but not how to get custom attributes? Also this speed sensitive so I would like to fetch it by the name and not to loop through all the attributes
Try
$sku = $id;
$_product=Mage::getModel('catalog/product')->load($sku, 'sku');
$amazonlink = $_product->getData('custom_attributes_code_here');
// or
$amazonlink = $_product->getCustomAttributesCodeHere();
Below is the safest way to get custom attribute value.
$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}
Above code is explained in my blog post here
Getting custom attribute value in Magento

Can one generate a link to the checkout page for a cart in magento?

I've got an instance of magento 1.7 CE running, and a second site that calls it via the SOAP api v2 from php.
I can't seem to find out how to add an array of products (given by productId or SKU) to a cart and then redirect to the cart page.
I've tried adding the items to the cart via shoppingCartProductAdd, which works, however I can't find out how to then open that cart back on magento.
I've also tried directly formulating a link that passes products via GET, however this only works for a single product ( checkout/cart/add?product=[id]&qty=[qty] ), for my purpose a whole array of products needs to be passed before redirecting to magento.
Any ideas?
Figured it out.
Basically one can use a link shaped like
http://example.com/checkout/cart/add?product=1&related_product=2,3,4,5
To fill the shoppingcart with products with id 1 .. 5 and then go to the cart in magento.
In my case I generated the link like this
if(!isset($session)) {
$client = new SoapClient('http://example.com/index.php/api/v2_soap?wsdl=1');
$session = $client->login('username', 'Qq314asdgUScrncfD7VMb');
}
if(!isset($cart)) {
$cart = $client->shoppingCartCreate($session);
}
$ids = array();
foreach($items as $id) {
$result = $client->catalogProductInfo($session, $id." ", null, 'sku');
$ids[] = $result->product_id;
}
$this->Session->delete('Cart');
$this->redirect('http://example.com/checkout/cart/add?product='.$ids[0].'&related_product=' . implode(array_slice($ids, 1), ','));

How can I get the Url of a product from its SKU in Magento

The question is simple.
If I know the sku of my product and nothing else, how can I retrieve the url to that item. This is commonly useful for third party integration where the unique id at the remote service wont match a product id. Or maybe you want to make a search box to search by sku only.
You can just do this:
$sku = 'ecco'; // SKU you want to load. 'ecco' is a sku in the Magento demo data
$url = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getProductUrl();
echo $url;
Also.... You could add this to a file named 'geturlbysku.php' in magento root dir. This code enables a small amount of json representing the product to be pulled easily, enabling small javascript integration. Product URL is part of the dataset
<?php
require_once '/app/Mage.php';
umask(0);
Mage::app('admin');
$actual_link = "http://$_SERVER[HTTP_HOST]";
$sku = $_GET['sku'];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$fullUrl = "/catalog/product/view/id/" . $product->getId();
header("Content-Type: application/json");
echo $_GET['callback'] . '('.json_encode($product->getData()).')';
?>
Use like this...
www.yoursite.com/geturlbysku.php?sku=theskuoftheproduct
Part of the data returned includes "url_path":"your-product-name.html" which corresponds to the real url.
You can use the load method. This is quicker and easier than all other solutions.
Mage::getModel('catalog/product')->load('sku', 'sku');
For more information on loading products Click Here
$product = Mage::helper('catalog/product')->getProduct($this->getData('sku'), Mage::app()->getStore()->getId(), 'sku');
$url = Mage::getUrl($product->getUrlPath());
You can access the full url of the product based on its sku by adding this to a method and then setting the sku attribute on the class to the sku that you wish to search by.

Duplicating a product with Code in Magento

I am trying to write a custom module which is capable of duplicating a product into multiple products with only varying SKU. I have tried using function duplicate() under /app/code/core/Mage/Catalog/Model/Product.php in my custom module. But its not working.
I am using the below code in my custom Obesrever.php file to duplicate, but duplication is not occuring
$product = $observer->getEvent()->getProduct();
$newProduct = $product->duplicate();
can anyone suggest me any links to do this or any code format would be helpful.
Thanks
It would be great if you can post the complete function that you are trying to debug or create duplicate products and config.xml (where you are trying to call the event).
Below code works for me in CE 1.9.2.2 without any problems. This function does the following tasks:
Creates duplicate of the original product
Sets the stock to "In Stock" and Qty to "100" (hard coded for now)
Automates reindexing
public function indexAction() //change the function name
{
$productId = $observer->getEvent()->getProduct()->getId();
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
//new product status is disabled - to view in the frontend you will need to set the status as enabled
$newProduct->setStatus(1);
$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value-' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
//set the product stock and qty to display product in the frontend
//while creating duplicate product, it will update the new product to have qty 0 and out of stock
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($newProduct->getId());
if ($stockItem->getId() > 0 && $stockItem->getManageStock())
{
$qty = 100;
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
$newProduct->getResource()->save($newProduct);
//automate reindexing - to display the product in the frontend
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}
Hope this helps.
Happy Coding...

Magento - show a custom customer attribute in new oder validation E-mail

How can I show the value of a custom customer attribute in new oder validation E-mail, using Magento ver. 1.4.1.1
I have added this code to /sales/model/order.php :
public function getCustomerNumber() {
if (!$this->getCustomerId()) return;
$customer = Mage::getModel('customer/customer')->load( $this->getCustomerId());
$customer = $customer->getData('customer_number');
return ($customer);
and {{var order.getCustomer()}} in order email template but it get the word 'Object' and not the right value.
Thanks for help.

Resources