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);
    }

}
0

You Can Get Product Special Price Like This :-

$price = $item->getProduct()->getSpecialPrice();
  • 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/… – jafar pinjar May 16 at 6:16
  • let me check... – Rk Rathod May 16 at 6:17
  • I am getting special price correctly but I need cost price? How can I get that? – Joydev Pal May 16 at 6:25
  • $item->getProduct()->getCost() using this which price you get ??? – Rk Rathod May 16 at 6:26
  • The special price @Rk Rathod – Joydev Pal May 16 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);        

}

  • It's not working. Thank for your help. – Joydev Pal May 16 at 18:56

Your Answer

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

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