Find Shopping Cart Rule Match by Product Programmatically in Magento

magento-1.7magento-1.8moduleMySQL

Question Updated

I am being try to find the discount amount which match the shopping cart rule based on product. I replicate the module 'Shopping Cart Price Rule' to another module which i want to create my discount rule under condition tab.My new module does not have coupon code.It has rule information and condition only.

So Here i want to get discount amount that matches my new module rule based on product details(product id, sku) with out coupon code.

/**
 * Get rules collection for current object state
 *
 * @return Mage_SalesRule_Model_Mysql4_Rule_Collection
 */
protected function _getRules()
{
    $key = $this->getWebsiteId() . '_' . $this->getCustomerGroupId() . '_' . $this->getCouponCode();
    return $this->_rules[$key];
}

Here we dont use $this->getCouponCode() to find the rule match.

Before going to replicated module, i just want to know is it possible in shopping cart price rule module.

My question is , How do i find the discount amount based on product that matches shopping cart price rule with out giving coupon code ?

Best Answer

I tried with external file,Given product id to find the matched sales rule conditions.

require_once("app/Mage.php");

Mage::app('default'); 

$pid = 53;
$_product= Mage::getModel('catalog/product')->load($pid);

$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 ':)';
       echo $rule->getData('discount_amount');
  }        
}   
exit;