2

Magento 2 current product how to check it is come from the cart edit in observer.

Please refer the cart edit url:

http://127.0.0.1/mage231/checkout/cart/configure/id/26/product_id/1/

Thanks in Advanced

2
  • can you please elaborate your question in detail, what you actually want to do?
    – ASQ
    May 7, 2019 at 9:21
  • I want to If the product is cumming from the cart edit then i want to apply my custom functionality.so how i check product is normal or coming from cart edit May 7, 2019 at 9:26

3 Answers 3

4

USE This Code

private $request;

public function __construct(
...
\Magento\Framework\App\Request\Http $request
) {
        ...
        $this->request = $request;

  }

Use This Condition ......

$moduleName = $this->request->getModuleName();// checkout
$controller = $this->request->getControllerName();// cart
$action     = $this->request->getActionName();// configure
$route      = $this->request->getRouteName();// checkout
if (($moduleName == 'checkout') && ($controller == 'cart') && ($action == 'configure') && ($route == 'checkout')) 
{
                  //Your Code
}
2

Try this:

$quote_data = $objectManager->get('\Magento\Checkout\Model\Session')->getQuote();
$result = $quote_data_->getAllVisibleItems();
$itemsIds = array();
foreach ($result as $quote_data_Item) {
    array_push($itemsIds, $quote_data_Item->getProduct()->getId());
}

if(in_array($productId, $itemsIds)){

    echo "in cart";
}
1
  • 1
    This is a bestway but if more quote data available then foreach loop take a time. May 8, 2019 at 9:22
1

Use following code to check controller and module

echo $controllerName = $this->getRequest()->getControllerName();
echo $actionName = $this->getRequest()->getActionName();
echo $routeName = $this->getRequest()->getRouteName();
echo $moduleName = $this->getRequest()->getModuleName(); 

You can verify by controller and module name, it would be different than default product view. use if condition to do this.

Hope you find your solution

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.