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

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

WooCommerce 中特定購物車小計的免費贈品產品

WooCommerce 中特定購物車小計的免費贈品產品

PHP
qq_遁去的一_1 2023-07-21 18:07:01
我有一種產品很便宜。當且僅當購物車小計為 200 件或更多時,我想贈送該產品。如果購物車小計為 200 件或更多,請將產品添加到購物車并將價格設置為 0。如果購物車小計少于 200,請從購物車中刪除該產品。在添加和刪除時,我使用通知來通知客戶。除了產品價格發生變化外,一切都按計劃進行。如果有人可以檢查我正在使用的代碼并修復這一問題,我將非常感激。add_action( 'woocommerce_before_calculate_totals', 'free_product_if_cart_minimum', 10, 1 );function free_product_if_cart_minimum( $cart ) {    // go away if admin    if (is_admin() && !defined( 'DOING_AJAX' )) return;    // say no to hook repetition     if (did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;    $minimum_amount = 200;    $free_product_id = 4576;    $cart_items_total = 0;    // cart loop    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){            // is the free product there?        if ( $cart_item['data']->get_id() == $free_product_id ) {                    $free_item_key = $cart_item_key;        // set the price to zero        // I've tried them both without success                // $free_item_key->set_price( 0 );        // $free_product_id->set_price( $price * 0.00 );    }        // cart subtotal incl. tax and discounts    $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];        }    // add the free product if not already there    if ( $cart_items_total >= $minimum_amount && !isset( $free_item_key ) ) {        $cart->add_to_cart( $free_product_id );        wc_add_notice( 'Thank you! Here\'s your free product.', 'notice' );    }    // if below the minimum, remove the free product    elseif ( $cart_items_total < $minimum_amount && isset( $free_item_key ) ) {        $cart->remove_cart_item( $free_item_key );        // display notice after removal        wc_add_notice( 'Your cart subtotal is less than 200 and therefore, the free product was removed.', 'notice' );    }}
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

以下內容將完成這項工作,將購物車中的免費產品價格設置為零:


add_action( 'woocommerce_before_calculate_totals', 'conditionally_add_free_product' );

function conditionally_add_free_product( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    // Settings

    $minimum_amount   = 200;

    $free_product_id  = 4576;


    // Initializing

    $cart_subtotal = 0;

    $cart_items    = $cart->get_cart();


    // Loop through cart items (first loop)

    foreach ( $cart_items as $cart_item_key => $cart_item ){

        // When free productis is cart

        if ( $cart_item['data']->get_id() == $free_product_id ) {

            $free_item_key = $cart_item_key; // Free product found (get its cart item key)


            $cart_item['data']->set_price(0);

        }

        // Get cart subtotal incl. tax and discounts (excluding free product)

        else {

            $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];

        }

    }


    // When cart total is up to the minimum amount, add the free product if not already there

    if ( $cart_subtotal >= $minimum_amount && ! isset($free_item_key) ) {

        $cart_item_key = $cart->add_to_cart( $free_product_id );


        // display notice after removal

        wc_add_notice( __("Thank you! Here's your free product."), 'notice' );

    }

    // if below the minimum, remove the free product

    elseif ( $cart_subtotal < $minimum_amount && isset( $free_item_key ) ) {


        $cart->remove_cart_item( $free_item_key );


        // display notice after removal

        wc_add_notice( sprintf(

           __("Your cart subtotal is less than %s and therefore, the free product was removed."), wc_price($cart_subtotal)

        ), 'notice' );

    }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 122 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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