I need to add an additional product to the cart. I have differents options to choose so I asign one checkbox to everyone of them. Then, depends the checkbox clicked it should add the right product to the cart. The problem begans when in my function it's not detected if the checkbox is checked, I have an if() but always returns the 'else'. It should add the additional product if the checkbox is checked but it does not. The code to add the product works correctly
add_action('woocommerce_add_to_cart', 'custom_add_to_cart');
function custom_add_to_cart() {
$id = get_the_ID();
if($id = '147430'){
global $woocommerce;
$product_id_mes = 147054;
$product_id_anual = 147295;
$found = false;
if(isset($_POST['checkmes']) && $_POST['checkmes'] == 'Si'){
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id_mes )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id_mes );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id_mes );
}
} else{
//if 'checkmes' isn't checked don't add the additional product
}
}
}
There are 2 problems in your code:
if($id = '147430')
here you're doing the assignment. you need to use==
loose comparison or===
strict comparison.get_the_ID()
function for getting the product ID, you need to use 2nd param of action which is$product_id
Other suggestions:
global $woocommerce;
line if not required to add since you're using theWC()
function not the global$woocommerce
object.count()
instead ofsizeof
function.Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.