cancel
Showing results for 
Search instead for 
Did you mean: 

I want to check with one condition before adding product to the cart magento 2 ?

SOLVED

I want to check with one condition before adding product to the cart magento 2 ?

Hi guys,

               i have a condition. if that condition success then only the product should added to the cart.I have a condition but i want to know where exactly i should place that condition. I need files to add that condition .

can any one help me ?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: I want to check with one condition before adding product to the cart magento 2 ?

For add custom conditions before the add To cart in Magento 2, You need to find the line with $block->getAddToCartUrl from Catalog module templates.

 

getAddToCartUrl() function is responsible for fetch add action of cart.

 

Many files contain the line as above defined, which is responsible for add to cart in the store. So you need to override specific file and before above action, you need to set your custom conditions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

3 REPLIES 3

Re: I want to check with one condition before adding product to the cart magento 2 ?

By default all the products in Magento 2 before add, They perform action Add Controller in Checkout module.

You can check file,  vendor/magento/module-checkout/Controller/Cart/Add.php

 

You need to Override controller file in your custom module or you can create plugin for execute() method of Add.php file for setting your custom conditions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: I want to check with one condition before adding product to the cart magento 2 ?

But i want to put that condition in .phtml file. Can you guide me ?

how can i acheive it ?

Re: I want to check with one condition before adding product to the cart magento 2 ?

For add custom conditions before the add To cart in Magento 2, You need to find the line with $block->getAddToCartUrl from Catalog module templates.

 

getAddToCartUrl() function is responsible for fetch add action of cart.

 

Many files contain the line as above defined, which is responsible for add to cart in the store. So you need to override specific file and before above action, you need to set your custom conditions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial