1

I need to set the product's price as it's added to the cart based on a separate table which holds each customer's individual prices. To do this I'm wrapping the Magento\Catalog\Model\Product::getFinalPrice() method, with the following plugin:

public function aroundGetFinalPrice(\Magento\Catalog\Model\Product $subject, \Closure $proceed)
{
    $customerPrice = $this->customerPriceFactory->create();

    $customerPriceColl = $customerPrice->getCollection();
    $customerPriceQuery = $customerPriceColl->addFieldToSelect('price1')
                                            ->addFieldToFilter('product_id',$subject->getId())
                                            ->addFieldToFilter('customer_id',$this->getCustomerId());

    $retPrice = $customerPriceQuery->getFirstItem()->getData()['price1'];

    return $retPrice;

}

This works great for simple products, but when a configurable product is added to the cart $subject only appears to be the parent product, not the actual child product the customer selected? This of course means the price can't be accurately determined, as each child may have different prices/tiers.

I have quickly checked Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price::getFinalPrice() with some debug output but that doesn't seem to be called when a product is added to the cart.

0

Right, figured out the required addition, from the Configurable\Price::getFinalPrice() method I mentioned. The following is used to extract the actual product, if a configurable product is passed in. I have obviously not tested this in all cases but it seems to solve my problem, so hopefully this is useful for someone else.

if ($subject->getCustomOption('simple_product') && $subject->getCustomOption('simple_product')->getProduct()) {
    $subject = $subject->getCustomOption('simple_product')->getProduct();
}

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.