cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2 - Appending a custom price in Magento Product value

Magento2 - Appending a custom price in Magento Product value

We have a requirement to add a custom price for each product. To add the custom price, we are using "checkout_cart_product_add_after" event and adding the price using below code

 

class BindCustomprice implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $item=$observer->getEvent()->getData('quote_item');
        $product=$observer->getEvent()->getData('product');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        if($product->getQty() > 10){
        // Load the custom price
            $price = $product->getFinalPrice()+10;
        }else{
            $price = $product->getFinalPrice()+20;
        }
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);

    }
}

The price are added successfully. However, the setCustomPrice() function sets the custom price for the product ignoring the tier pricing applicable for it.

Our product has a tier pricing applicable ie: Buy 4 and get a product for 5.25GBP each. This tier pricing is not applicable using above code. We need to append the price in product along with the applicable tier pricing or any other discount.

Can anyone please guide me through this.

 

Note: We are using Configurable product with custom options for this.

 

EDIT

I have further observed that, Tier Price for simple products are loaded properly however null array is returned for configurable product. I have used code $product->getTierPrice()

Whereas, when we retrieve sku ie: $product->getSku(), it is returning the SKU of simple product.

 

I just need to add some extra fee to each product. Please help me through this.