0

I need to access a custom attribute of the current product in Vendor\ModuleName\Plugin\Checkout\Controller\Cart. I have tried getting this from the request param, checkoutSession,registry (eventhough its deprecated) but failed.

Is this possible? Following is my code,

    <?php

    namespace Vendor\ModuleName\Plugin\Checkout\Controller\Cart;

    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\App\RequestInterface;
    use Magento\Framework\Controller\Result\RedirectFactory;
    use Magento\Framework\ObjectManagerInterface;
    use Magento\Framework\UrlInterface;

    class Add
    {
        /**
         * @var ObjectManagerInterface
         */
        protected $objectManager;

        /**
         * @var RedirectFactory
         */
    protected $resultRedirectFactory;

    /**
     * @var UrlInterface
     */
    protected $url;
    protected $request;
    protected $registry;
    protected $_checkoutSession;

    /**
     * Data constructor.
     *
     * @param ObjectManagerInterface          $objectManager
     * @param RedirectFactory                 $resultRedirectFactory
     * @param UrlInterface                    $url
     * @param RequestInterface                $request
     * @param \Magento\Checkout\Model\Session $_checkoutSession
     */
    public function __construct(
        ObjectManagerInterface $objectManager,
        RedirectFactory $resultRedirectFactory,
        UrlInterface $url,
        RequestInterface $request,
        \Magento\Checkout\Model\Session $_checkoutSession
    ) {
        $this->objectManager = $objectManager;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->url = $url;
        $this->request = $request;
        $this->_checkoutSession = $_checkoutSession;
    }

    /**
     * Around cart execute
     *
     * @param \Magento\Checkout\Controller\Cart\Add $subject
     * @param \Closure                              $proceed
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function aroundExecute(
        \Magento\Checkout\Controller\Cart\Add $subject,
        \Closure $proceed
    ) {
        $cartData = $this->_checkoutSession->getQuote()->getAllVisibleItems();
        $cartDataCount = count($cartData);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
        $logger->info('-------------------------------Test------------------------');
        $logger->info($cartDataCount);
        $logger->info($product->getId());
        $logger->info($product->getName());
        $logger->info(var_dump($this->request->getParams()));

        $contactUrl = $this->url->getUrl(
            'contact'
        );
        $result = [];
        $result['backUrl'] = $contactUrl;

        $subject->getResponse()->representJson(
            $this->objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result)
        );
        return;
    }
}

FYI, I am aware that use of ObjectManager is discouraged. This was done for for testing purpose only.

1

If you want to get current Add to cart request path of the product then you have to use request param product .magento send product id as a parameter to add to Add.php.

    protected function _initProduct()
{
    $productId = (int)$subject->getRequest()->getParam('product');
    if ($productId) {
        $storeId = $this->_objectManager->get(
            \Magento\Store\Model\StoreManagerInterface::class
        )->getStore()->getId();
        try {
            // productRepository instead of \Magento\Catalog\Api\ProductRepositoryInterface
            return $this->productRepository->getById($productId, false, $storeId);
        } catch (NoSuchEntityException $e) {
            return false;
        }
    }
    return false;
}

_initProduct function provide product object.

0

Have you considered using one of the events for the cart this should make updating information easier.

Events

  • checkout_cart_add_product_complete
  • checkout_cart_update_item_complete
  • checkout_cart_product_add_after
  • checkout_cart_update_items_before
  • checkout_cart_update_items_after
  • checkout_cart_save_before
  • checkout_cart_save_after
  • checkout_cart_product_update_after

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.