I am trying to set discount amount on each cart item in the cart with some condition.

I have followed like this to achieve that.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
    <group name="totals">
        <item name="testdiscount" instance="Vendor\Module\Model\Quote\Discount" sort_order="500"/>

then in Vendor\Module\Model\Quote\Discount.php

namespace Vendor\Module\Model\Quote;
use Magento\Catalog\Model\ProductRepository;
class Discount extends 
 protected $_priceCurrency;
 protected $_productRepository;
 protected $_resource;
 public function __construct(
   \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
   \Magento\Framework\App\ResourceConnection $resource,
   ProductRepository $productRepository
     $this->_priceCurrency = $priceCurrency;
     $this->_resource = $resource;
     $this->_productRepository = $productRepository;

    public function collect(
      \Magento\Quote\Model\Quote $quote,
      \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
      \Magento\Quote\Model\Quote\Address\Total $total
   $data = $quote->getItems();
    if (!count($data)) {
        return $this;
   parent::collect($quote, $shippingAssignment, $total);
    foreach ($data as $item) {
            $productSku = $item['sku'];
            $checkSkuExist  = $this->checkProduct($productSku);//Looking over in my custom table the sku is present or not
        return $this;


public function checkProduct($productSku){
    $connection = $this->getConnection();
    $sql = "select * from custom_table where sku='".$productSku."'";
    $resultProduct = $connection->query($sql);
    $resultQuery = $resultProduct->fetchAll();

     $parent_sku = $resultQuery[0]['parent_sku'];
    return true;
  return false;

 public function getConnection(){
   $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
  return $connection;


Here When i am check the item SKU against the custom table, if the SKU present in the table then, applying the discount amount for that quote item.

Which is not applying any discount amount for the quote item.

Can anyone suggest me the better way how this can be achieve.

Thanks in Advance!!.

  • Did you get any errors? – aravind Jul 31 '19 at 14:03
  • @aravind, No there is no erro, But the Discount file is hitting, but discount is not applied to the total, Am i used correctly? – jafar pinjar Jul 31 '19 at 14:04
  • @aravind, Yes i referred this link before posting, but there discount applying to whole cart, not for the individual items in the cart, so i posted as new question – jafar pinjar Jul 31 '19 at 14:09
  • @aravind, could you check it how we can do this? – jafar pinjar Aug 1 '19 at 9:57
  • sorry @jafar pinjar i didn't have time... – aravind Aug 1 '19 at 10:27

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.