1

So I wrote this overcomplicated(because I just started using php few days ago) code in php, that it should simply add a custom checkbox on the checkout page when certain products are in the cart, but for some reason it works for simple products only and I need it for variable products. I've tried using the variations ID's as well as products ID's. Can you tell me where I'm being wrong ?

add_action( 'woocommerce_review_order_before_submit', 'webroom_check_if_product_in_cart' );
function webroom_check_if_product_in_cart() {
    $product_id1 = 9145; // CHANGE THIS WITH YOUR PRODUCT ID 9145      scratch-8974
    $product_id2 = 9151; // CHANGE THIS WITH YOUR PRODUCT ID
    $product_id3 = 9152; // CHANGE THIS WITH YOUR PRODUCT ID
    $product_id4 = 9153; // CHANGE THIS WITH YOUR PRODUCT ID
    $product_id5 = 9155; // CHANGE THIS WITH YOUR PRODUCT ID
    $product_id6 = 9156; // CHANGE THIS WITH YOUR PRODUCT ID
    $product_cart_id1 = WC()->cart->generate_cart_id( $product_id1 );
    $product_cart_id2 = WC()->cart->generate_cart_id( $product_id2 );
    $product_cart_id3 = WC()->cart->generate_cart_id( $product_id3 );
    $product_cart_id4 = WC()->cart->generate_cart_id( $product_id4 );
    $product_cart_id5 = WC()->cart->generate_cart_id( $product_id5 );
    $product_cart_id6 = WC()->cart->generate_cart_id( $product_id6 );
    $in_cart1 = WC()->cart->find_product_in_cart( $product_cart_id1 );
    $in_cart2 = WC()->cart->find_product_in_cart( $product_cart_id2 );
    $in_cart3 = WC()->cart->find_product_in_cart( $product_cart_id3 );
    $in_cart4 = WC()->cart->find_product_in_cart( $product_cart_id4 );
    $in_cart5 = WC()->cart->find_product_in_cart( $product_cart_id5 );
    $in_cart6 = WC()->cart->find_product_in_cart( $product_cart_id6 );
    if ( $in_cart1 || $in_cart2 || $in_cart3 || $in_cart4 || $in_cart5 || $in_cart6) { 
       echo add_my_checkout_tickbox();
   }
}

Final version:

add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkbox' );
function add_custom_checkbox() {
    ## ----- CHECK IF CERTAIN PRODUCTS (COULD ALSO BE VARIABLE PRODUCTS) ARE IN CART ----- ##

    $product_ids = array (9145, 9151, 9152, 9153, 9155, 9156); // Search for this products (PARENT ID)

    // Loop though cart items searching for the defined products
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Product id
        $product_id = $cart_item['product_id'];

        // Display checkbox if product found in cart
        if ( in_array( $product_id, $product_ids) ) {
            echo add_my_checkout_tickbox();
        }
    }
}
  • Hi, take a look at "[Get in WooCommerce cart the product ID of a cart item](https://stackoverflow.com/questions/41104409/get-in-woocommerce-cart-the-product-id-of-a-cart-item/41105518#41105518)" & [Minumun cart amount except for several specific products in WooCommerce](https://stackoverflow.com/a/62190559/11987538). if you combine both codes you can certainly find an answer to your question. If it still does not work, please adjust your question and we will be happy to help you – 7uc1f3r Jun 19 '20 at 20:19
  • 2
    The reason why I do not answer your question (immediately) is because I believe that you will learn more from this by looking for the solution yourself than getting it as a ready answer. A few tips: **1)** place multiple product IDs in an array. **2)** use the foreach loop with `WC()->cart->get_cart()` (see example links). **3)** if `$product_id` is `in_array`, do something... good luck! – 7uc1f3r Jun 19 '20 at 20:26
  • Thanks a lot for the references and the trust. It turns out the code I needed was very similar with those you referenced to me, and it works for variable products when I use the parent product's ID, but I still don't know why the previous one did't work for variable products. Anyway here is my final version. – tudosie traian Jun 20 '20 at 13:28
  • Feel free to post your changes in answer to your own question versus adjusting your existing question. After that you can mark your question as resolved, well done! – 7uc1f3r Jun 20 '20 at 19:31

1 Answers1

2

This is the final version of the code, after reading the references provided by '7uc1f3r', above. Thanks for the help.

add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkbox' );
function add_custom_checkbox() {
    ## ----- CHECK IF CERTAIN PRODUCTS (COULD ALSO BE VARIABLE PRODUCTS) ARE IN CART ----- ##

    $product_ids = array (9145, 9151, 9152, 9153, 9155, 9156); // Search for this products (PARENT ID)

    // Loop though cart items searching for the defined products
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Product id
        $product_id = $cart_item['product_id'];

        // Display checkbox if product found in cart
        if ( in_array( $product_id, $product_ids) ) {
            echo add_my_checkout_tickbox();
        }
    }
}