3

I am trying to find the discount amount which match the shopping cart rule based on product. I want to get discount amount that matches my product price rule based on product details(product id, sku). here is the code i am using but its not working.

<?php 
$products_id = $_helper->productAttribute($_product, $_product->getId(), 'id');
$_p = $objectManager->get('Magento\Catalog\Model\Product')->load($products_id);
$price_rules = array();
$rules = $objectManager->create('Magento\SalesRule\Model\Rule\Action\Collection')
                       ->load();
foreach($rules as $rule){
  $rule->afterLoad(); 
}  

$quoteId = $objectManager->getSingleton('checkout/session')->getQuoteId(); 
$real_quote = $objectManager->getSingleton('sales/quote')->load($quoteId);                
$product = $objectManager->get('sales/quote_item')->setQuote($real_quote)->setProduct($_p);  
$product->setAllItems(array($_p));                 
$product->getProduct()->setProductId($_p->getEntityId());    
foreach($rules as $rule) 
{  
    if ($rule->getConditions()->validate($product))  
    {            
        echo ':)';
        echo $rule->getData('discount_amount');
    }        
}

?>

1 Answer 1

1

You should init an quote item like this:

$real_quote = $objectManager->get('Magento\Checkout\Model\Session')->getQuote();
$item = $objectManager->get('Magento\Quote\Model\Quote\Item')->setQuote($real_quote)->setProduct($product);
$item->setAllItems(array($product));
$item->getProduct()->setProductId($product->getEntityId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.