0

I've been working on this problem for some time now. When I add Item A to my basket I currently have it so it adds Item B with a custom price. Based on the number of Item A added to the basket, the price of Item B will change (not at a fixed amount) so i've created an observer to try and do this.

The first time Item B gets added to basket i'm able to set its custom price. However, if it is already within the basket I can't then update its price.

This is my Controller:

if($itemInBasket == true){
    $product = $this->_product->load(7);
    $product->setData('custom_overwrite_price', $customPrice);

    $this->_eventManager->dispatch(
        'checkout_cart_product_add_after',
        ['quote_item' => $frontCoverReference, 'product' => $product]
    );
}
else{
    if($numFrontCovers > 0){
        $params = array(
            'form_key' => $this->_formKey->getFormKey(),
            'product' => 7,
            'qty' => 1
        );
            
        $product = $this->_product->load(7);
        $product->setData('custom_overwrite_price', $customPrice);
        $this->_cart->addProduct($product, $params);
        $this->_cart->save();
    }
}

This is my Observer (Event - checkout_cart_product_add_after):

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $item = $observer->getEvent()->getData('quote_item');
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

    $_product = $observer->getEvent()->getProduct();
    $_id = $_product->getId();

    if($_id == 7 || $_id == 8){
        $product = $this->_product->load($_id);
        $price = $product->getData('custom_overwrite_price');
        if($price){
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }
    }
    return $this;
}

Within the observer I have made sure the price is pulling through correctly and that it is getting into the if statement checking the product ID. The issue is that the price is never actually assigned to the product

0

Please check that you have assigned custom attributes in the quote_item group in catalog_attributes.xml

app/code/Vendor/Module/etc/catalog_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="custom_price"/>
        <attribute name="original_custom_price"/>
    </group>
</config>
2
  • It seems to be the same as before. I didn't have this file previously though so it may be part of the problem – A. Fletcher Apr 26 at 10:14
  • I had a piece of code that would loop through all of the carts items and set them to £10000. This triggers when it's the first time the item is added to cart but not if its already there – A. Fletcher Apr 26 at 10:17

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.