0

I'm currently able to change the way the cart works by updating the qty value in the cart, i do this by first deleting the item before it is re-added using the checkout_cart_product_add_before observer.

Here is my observer class (the simple products work but the configurable products do not delete)

<?php
namespace XX\XX\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Checkout\Model\Session as CheckoutSession;
use \Magento\Checkout\Model\Cart;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

class DeleteAndReAddFromCart implements ObserverInterface {
    protected $request;
    protected $_cart;
    /** @var CheckoutSession */
    protected $checkoutSession;
    protected $configurableproduct;
    public function __construct(\Magento\Framework\App\RequestInterface $request, cart $cart, CheckoutSession $checkoutSession, Configurable $configurableproduct) {
        $this->_cart = $cart;
        $this->request = $request;
        $this->checkoutSession = $checkoutSession;
        $this->configurableproduct = $configurableproduct;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $info = $observer->getInfo();
        $order = $observer->getEvent()->getOrder();
        $product = $observer->getProduct();
        $productId = $observer->getProduct()->getId();
        $prodtype = $observer->getProduct()->getTypeId(); //is configarable or simple
        $info = $observer->getInfo();
        $items = $this->_cart->getItems();
        $itemCount = count($items);
        if ($itemCount > 0) {
            if ($prodtype != 'configurable') {
                foreach ($items as $item) {
                    if ($prodtype != 'configurable') {
                        if ($item->getProduct()->getId() == $productId) {
                            $itemId = $item->getItemId();
                            $item->isDeleted(true);
                            $this->_cart->save();
                        }
                    } else {
                    }
                }
            } else {
                $product2 = $observer->getProduct();
                if ($product2->getTypeId() == "configurable") {
                    $configClass = $product2->getTypeInstance();
                    $childProduct = $configClass->getProductByAttributes($info['super_attribute'], $product2);
                    $productIdd = $childProduct->getId();
                    //the above id is the correct id of the simple product
                    $c = $this->_cart;
                    $childProduct->isDeleted(true);
                    $c->removeItem($childProduct->getId())->save();
                    $this->_cart->save();
                }
            }
        }
    }
}

its the code below this condition

if ($product2->getTypeId() == "configurable") {

condition thats not working, the simple products above it successfully remove from cart.

Your Answer

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

Browse other questions tagged or ask your own question.