Skip to content

How to get current Cart_id/quote_id in plugin file Magento 2?

namespace AsmSetselleridPlugin;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiDataTotalsItemExtensionFactory;

class Item
{
    /**
     * @var TotalsItemExtensionFactory
     */
    protected $totalsItemExtensionFactory;

    /**
     * ItemConverterPlugin constructor.
     *
     * @param TotalsItemExtensionFactory $totalsItemExtensionFactory
     */
    public function __construct(
        MagentoCheckoutModelCart $cart,
        TotalsItemExtensionFactory $totalsItemExtensionFactory
    ) {
        $this->totalsItemExtensionFactory = $totalsItemExtensionFactory;
         $this->cart = $cart;
    }

    /**
     * @param ItemConverter $subject
     *
     * @param $result
     * @return mixed
     */
    public function afterModelToDataObject(
        ItemConverter $subject,
        $result
    ) {

        $writer = new ZendLogWriterStream(BP . '/var/log/test.log'); 
        $logger = new ZendLogLogger(); 
        $logger->addWriter($writer); 
        $logger->info('Your text message');

        $cart = Mage::getModel('checkout/cart')->getQuote();
            foreach ($cart->getAllItems() as $item) {
                $productName = $item->getProduct()->getName();
                $productPrice = $item->getProduct()->getPrice();

                $logger->info($item->getProduct()->getName());
                $logger->info($item->getProduct()->getPrice());
            }


        $extensionAttributes = $result->getExtensionAttributes();
        if ($extensionAttributes === null) {
            $extensionAttributes = $this->totalsItemExtensionFactory->create();
        }
        $pointsDelta = 60;
        $extensionAttributes->setMine($pointsDelta);
        $result->setExtensionAttributes($extensionAttributes);
        return $result;
    }
}

Here am trying fetch current cart_id but it showing error like..

/Setsellerid/Plugin/Item.php' on line 57","trace":"Trace is not available.

so how to get current cart id, thank you in advance