1

Here in my custom module:

enter image description here

This product with 50 quantities price is 6,000

But when I add to cart this multiplies

So how I can fix this?

enter image description here

Can I use observer to change this or are there any other best way?
Thanks

 if ($quoteId != 0 && ($priceOne!=$item->getCustomPrice() || $quoteQty!=$item->getQty())) {
                $flag = 1;
                $item->setCustomPrice($priceOne);
                $item->setOriginalCustomPrice($priceOne);
                $item->setQty($quoteQty);
                //$item->setRowTotal($priceOne * $quoteQty);
                $item->setRowTotal($priceOne * 1);
                $item->getProduct()->setIsSuperMode(true);
                if ($helper->checkAndUpdateForDiscount($item)) {
                    $item->setNoDiscount(1);
                } else {
                    $item->setNoDiscount(0);
                }
                $item->save();
                $this->_messageManager->addNotice(
                    __(
                        "You can't edit quote items"
                    )
                );
            }
        }
    }
10
  • I'm not understanding what is trying to be achieved. What is the price for a single unit? Are you trying to provide a tier pricing structure that has discounts for bulk purchases?
    – Ian at SSU
    Commented Feb 20, 2019 at 13:35
  • hi i want to send price to cart Commented Feb 20, 2019 at 14:05
  • but we are giving quantities is 50s price 6,000 when add to cart it's multiplying we not need this for this module only Commented Feb 20, 2019 at 14:07
  • I'm still not understanding what is trying to be achieved. What is the price for a single unit, 10 units, 100 units? Seem like you're trying to use the qty field incorrectly and might be able to configure the product catalog to achieve goals without custom code. For example, use an attribute to show the number of units in a package. That said, I've used the event sales_quote_product_add_after to add custom pricing logic when an item is added to cart.
    – Ian at SSU
    Commented Feb 20, 2019 at 14:24
  • i will explain. Commented Feb 20, 2019 at 14:25

1 Answer 1

0

I still think you probably want to set price to $120 and then control available qty's via the frontend. Then you might want to enforce qty's allowed in cart via a quote observer/interceptor.

To provide you an answer to the question of how to manipulate price for products in the cart a method I've used before is to observe the sales_quote_product_add_after event. Below is a basic idea of the concept:

Company/Module/etc/events.xml

<event name="sales_quote_product_add_after">
    <observer name="company_module_quote_product_add_after"
              instance="Company/Module/Observer/QuoteProductAddAfterObserver" />
</event>

Company/Module/Observer/QuoteProductAddAfterObserver.php

...
class QuoteProductAddAfterObserver implements ObserverInterface
{
...
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $items = $observer->getEvent()->getData('items');
        foreach ($items as $quoteItem) {
           //...custom logic to items in cart...
        }
    }
}
6
  • but also quantities not show as 1 Commented Feb 20, 2019 at 14:57
  • The answer provides a way to "How to change price, the price calculation in cart through custom module Magento 2?"
    – Ian at SSU
    Commented Feb 20, 2019 at 15:00
  • can i get price and divided by the total number of quantities Commented Feb 20, 2019 at 15:01
  • i added code............. Commented Feb 20, 2019 at 15:04
  • The event provides access to an array of \Magento\Quote\Model\Quote\Item. If the provided information is not enough to get you working toward a solution you might want to consider looking for a third-party extension to meet your needs.
    – Ian at SSU
    Commented Feb 20, 2019 at 15:05

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.