Php 如何检查可变产品ID是否在购物车中?

Php 如何检查可变产品ID是否在购物车中?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,所以我写了这篇过于复杂的文章,因为我几天前才开始用php编写代码,当某些产品在购物车中时,它应该只在结帐页面上添加一个自定义复选框,但出于某种原因,它只适用于简单的产品,而我需要它用于可变产品。我试过使用变体ID和产品ID。你能告诉我哪里错了吗 add_action( 'woocommerce_review_order_before_submit', 'webroom_check_if_product_in_cart' ); function webroom_check_if_product_i

所以我写了这篇过于复杂的文章,因为我几天前才开始用php编写代码,当某些产品在购物车中时,它应该只在结帐页面上添加一个自定义复选框,但出于某种原因,它只适用于简单的产品,而我需要它用于可变产品。我试过使用变体ID和产品ID。你能告诉我哪里错了吗

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();
   }
}
最终版本:

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();
        }
    }
}

在阅读了上述“7uc1f3r”提供的参考资料后,这是代码的最终版本。谢谢你的帮助

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();
        }
    }
}

嗨,来看看&。如果你把这两种代码结合起来,你肯定能找到问题的答案。如果它仍然不起作用,请调整您的问题,我们将很乐意帮助您。我之所以不立即回答您的问题,是因为我相信,通过自己寻找解决方案,您将从中学到更多,而不是将其作为现成的答案。一些提示:1在一个数组中放置多个产品ID。2将foreach循环与WC->cart->get_cart一起使用,参见示例链接。3如果$product\u id在\u数组中,请执行某些操作。。。祝你好运非常感谢您的推荐和信任。事实证明,我需要的代码与您引用的代码非常相似,当我使用父产品的ID时,它适用于可变产品,但我仍然不知道前一个代码为什么不适用于可变产品。无论如何,这是我的最终版本。请随意发布您对自己问题答案的更改,而不是调整您现有的问题。之后,您可以将您的问题标记为已解决,做得好!