0

Wel i have plugin which shows price in frontend from custom attribute.

public function afterGetPrice(\Magento\Catalog\Model\Product $product, $price) {
    if($product->getData('price_per_area')) $price = $product->getData('price_per_area');
    return $price;
}

Now i need to revert price to original price when product is added to cart, made an observer with event: checkout_cart_product_add_before

public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    $item = $observer->getEvent()->getData('product');
    if($item->getData('price_per_area')){
        $item->setPrice(($item->getSpecialPrice() ? $item->getSpecialPrice() : $item->getPrice()));
    }
}

and here i have issue: product is added to cart but the price is still the one from custom attribute, when i click on update cart button then it shows the correct price

before cart update after cart update as you can see from screenshots the price which is shown in summary is correct in both cases.

also tried my observer with checkout_cart_product_add_after result was same. tried with custom theme and luma

is it magento/template bug or there is some work around ?

1 Answer 1

0

Found out what caused this bug.

it seem plugin function afterGetPrice influenced checkout price too, also didn't need an observer.

modified my plugin code so it will not affect checkout prices.

public function afterGetPrice(\Magento\Catalog\Model\Product $product, $price) {
    if($product->getData('price_per_area')){ //per meter
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface');
        $moduleName     = $requestInterface->getModuleName(); 
        if($product->getData('price_per_area')){
            if($moduleName != 'checkout'){
                $price = $product->getData('price_per_area');
            }
        }
    }
    return $price;
}

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.