亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查可變產品 ID 是否在 woocommerce 購物車中?

如何檢查可變產品 ID 是否在 woocommerce 購物車中?

PHP
慕森卡 2023-06-18 17:24:07
所以我在 php 中寫了這個過于復雜的代碼(因為我幾天前才開始使用 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();   }}
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

這是代碼的最終版本,在閱讀了上面“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();

        }

    }

}


查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 414 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號