6

I have a Shopping cart rule which applies to a particular category with some discount.

I have a situation wherein I want to validate product against this rule whether this rule can be applied to that product or not.

I have tried following code, but it gives me Boolean true all the time.

protected function validate($product) {

        $coupon_code = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
        $oCoupon = Mage::getModel('salesrule/coupon')->load($coupon_code, 'code');
        $oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());

        // Sales Rule Id of the coupon we want to validate
         $ruleId = $oCoupon->getRuleId();
        //echo "<br />";
        // Id of Product we want to validate
        $productId = $product->getId();

        /** @var Mage_SalesRule_Model_Rule $salesRule */
        $salesRule = Mage::getModel('salesrule/rule')->load($ruleId);

        /** @var Mage_Catalog_Model_Product $product */
        $product = Mage::getModel('catalog/product')->load($productId);

UPDATE 1: Tried with the following code also. But thats returning nothing.

<?php

set_time_limit(3000);
ini_set('memory_limit', '-1');
include_once 'app/Mage.php';
umask(0);
Mage::app();

$product_ids = array(1199, 1200, 1202);
foreach ($product_ids as $productId) {

    Mage::getSingleton('core/session', array('name' => 'frontend'));

// Get customer session
    $session = Mage::getSingleton('customer/session');

// Get cart instance
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();
<?php

set_time_limit(3000);
ini_set('memory_limit', '-1');
include_once 'app/Mage.php';
umask(0);
Mage::app();

$product_ids = array(1199, 1200, 1202);
foreach ($product_ids as $productId) {

    Mage::getSingleton('core/session', array('name' => 'frontend'));

    // Get customer session
    $session = Mage::getSingleton('customer/session');

    // Get cart instance
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();

    echo $coupon_code = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
    $oCoupon = Mage::getModel('salesrule/coupon')->load($coupon_code, 'code');
    $oRule = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());

    // Sales Rule Id of the coupon we want to validate
    $ruleId = $oCoupon->getRuleId();

    /** @var Mage_SalesRule_Model_Rule $salesRule */
    $salesRule = Mage::getModel('salesrule/rule')->load($ruleId);

    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::getModel('catalog/product')->load($productId);

    $item = new Varien_Object();
    $item->setProduct($product);
    echo $isValid = $salesRule->getConditions()->validate($product);
}
?> 
        $item = new Varien_Object();
        $item->setProduct($product);

        $isValid =  $salesRule->getActions()->validate($item);

        return $isValid; //Returning true always       
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.