In magento if we need to check, if any shopping cart rule is applied to the product or not.
To do so we can perform it in a test file in our root folder.
lets perform the same, create a file test.php in root folder and write the below code:
require_once("app/Mage.php");
Mage::app('default');
$product_id = 53;
$_product= Mage::getModel('catalog/product')->load($product_id);
$coll = Mage::getResourceModel('salesrule/rule_collection')->load();
foreach($coll as $rule){
$rule->afterLoad();
}
$quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$real_quote = Mage::getSingleton('sales/quote')->load($quoteId);
$product = Mage::getModel('sales/quote_item')->setQuote($real_quote)->setProduct($_product);
$product->setAllItems(array($_product));
$product->getProduct()->setProductId($_product->getEntityId());
foreach($coll as $rule)
{
if ($rule->getConditions()->validate($product))
{
echo $rule->getName().'/'.$rule->getData('discount_amount').'<br>';
}
}
exit;
In the above code we have first included the mage.php file which loaded the magento app.
Then we stored the product id in the parameter $product_id. With the help of the $product_id we loaded the product in the parameter $_product.
then, in $coll parameter we loaded the sales rules collection. Afterwards we make the entries in the quote tables by setting the quoteid from checkout/session.
In the $product parameter we set the quote_item table entries by loading the model getModel('sales/quote_item').
Now, in the foreach loop we perform the validation to check the rules with the product to know which are the rules that are applied on the product.
In the if loop condition, if the validate returns true then the statement prints the Name and the Discount amount of the rule.
This way we can use the above code where ever we want in our magento system to perform this operation for some purpose.
0 Comment(s)