0

I am developing a magento site for furniture sale. I have some grouped products.

If a customer add 1 piece of any product of the group to cart, the special price will be added to cart.

If the customer add more than 1 piece of any product of the group to cart, the cost price will be added to cart.

Suppose, in grouped product, Product A has

regular price: $989.99 
special price: $732.99 
cost price: $669.99 

If a customer add 1 quantity to the cart, the subtotal price will be $732.99 and if the customer add 3 quantity to the cart, the subtotal price will be 2009.97 ($669.99 * 3).

To do the funtionality, I have created a observer and set the custom price as cost price but it always add special price, not the cost price. Can anyone help me to figure out the issue?

Here is the events.xml:

**app/code/vendor_name/module_name/etc/events.xml**

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="customprice" instance="Vendor_name\Module_name\Observer\CustomPrice" />
    </event>
</config>

Here is the observer file: app/code/vendor_name/module_name/Observer/CustomPrice.php

<?php

namespace Vendor_name\Module_name\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;


class CustomPrice implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer) {

        $item = $observer->getEvent()->getData('quote_item');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

        $price = $item->getProduct()->getCost(); //set your price here

        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
    }

}

3 Answers 3

0

You Can Get Product Special Price Like This :-

$price = $item->getProduct()->getSpecialPrice();
6
  • hi @Rathod, in my case setting custom price working fine, but tax price not getting updated, here is my question,magento.stackexchange.com/questions/274520/… May 16, 2019 at 6:16
  • let me check... May 16, 2019 at 6:17
  • I am getting special price correctly but I need cost price? How can I get that?
    – Joydev Pal
    May 16, 2019 at 6:25
  • $item->getProduct()->getCost() using this which price you get ??? May 16, 2019 at 6:26
  • The special price @Rk Rathod
    – Joydev Pal
    May 16, 2019 at 6:27
0

Try this code, It may be helpful to resolve your issue:

public function execute(\Magento\Framework\Event\Observer $observer) {

$item = $observer->getEvent()->getData('quote_item');

$item = ( $item->getParentItem() ? $item->getParentItem() : $item );

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$prodObj = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProduct()->getId());
$price = $prodObj->getCost(); //set your price here

$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);        

}

2
  • It's not working. Thank for your help.
    – Joydev Pal
    May 16, 2019 at 18:56
  • checkout_cart_product_add_after is tracking only the last item. When I am adding multiple items of the grouped product at time. Only the last item price got updated. Others are remaining the same. please advise
    – Liz Eipe C
    Nov 3, 2020 at 11:47
0

For grouped product I have looped through all the product if multiple products added to basket at the same.

  public function execute(Observer $observer)
{
    try {
        /** @var \Magento\Quote\Model\Quote\Item $item */
        $items = $observer->getEvent()->getData('quote_item');
        foreach ($items->getQuote()->getAllItems() as $item) {
            $price = $this->priceHelper->getPrice($item->getProduct(), $item->getQty());
          
            if ($price != null) {
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
                $item->getProduct()->setIsSuperMode(true);
            }
        }
    } catch (\Exception $e) {
        $this->logger->error($e->getMessage());
    }
}

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.