簡體   English   中英

根據 WooCommerce 購物車小計自動添加可變數量的特定產品

[英]Auto add a variable quantity of specific product based on WooCommerce cart subtotal

我正在嘗試根據客戶花費的總成本將禮品產品添加到購物車。 這是一家出售禮券的餐廳。 該計划運行如下:客戶在單次交易中每消費 50 英鎊,他們將自動免費獲得 10 英鎊的禮券。 因此,如果他們在禮券上花費 100 英鎊,他們將免費獲得 2 x 10 英鎊的禮券(依此類推)。 為了讓事情盡可能簡單,我們限制了禮券的面額(25 英鎊 - 250 英鎊,25 英鎊面額增加)。 我並不特別想支付/安裝另一個需要更新/管理的插件等。老實說,這只是聖誕節期間,所以執行此操作的代碼片段會好得多。 我認為它的工作方式是客戶將代金券添加到購物車,當他們進入購物車時,它顯示他們也有額外的“免費”代金券。

我在網上找到了一些代碼並對其進行了一些更改。 代碼如下:

   
function auto_add_specific_product_to_cart() {
           
   // select product ID
   $product_id = 1428;
           
   // if cart empty, add it to cart
   if ( WC()->cart->get_cart_total() <= 99 ) {
WC()->cart->add_to_cart( $product_id, 1 ); }
 
else if ( WC()->cart->get_cart_total() <= 149 ) {
WC()->cart->add_to_cart( $product_id, 2 ); }
 
else if ( WC()->cart->get_cart_total() <= 199 ) {
WC()->cart->add_to_cart( $product_id, 3 ); }
 
else if ( WC()->cart->get_cart_total() <= 249 ) {
WC()->cart->add_to_cart( $product_id, 4 ); }
     
}

我在右邊、左邊和中間都有問題。 它不斷地將免費代金券添加到購物車中,因此最終客戶最終會在那里獲得 20 或 30 份代金券; 用於識別不同閾值(50 到 99、100 到 149 等)的 PHP 無法正常工作。 我可能把這弄得太復雜了,沒有考慮清楚,但有一點幫助會很好。 我在網上找到了很多打折產品的代碼,但沒有任何東西可以免費贈送。

這里將根據購物車小計自動添加到購物車中的“特定產品憑證”的可變數量。

下面的代碼基於在 WooCommerce答案代碼中以最少的購物車數量添加免費贈品 該代碼自動生成購物車小計閾值(最小和最大金額)以及要添加到購物車的憑證產品的相關數量。

$max_quantity變量中設置的數量決定了FOR循環上的迭代次數(因此不同生成的閾值的數量和可以添加到購物車的憑證產品的最大數量)

// Auto add a quantity of voucher product based on cart subtotal
add_action( 'woocommerce_before_calculate_totals', 'auto_add_voucher_product_based_on_subtotal' );
function auto_add_voucher_product_based_on_subtotal( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Settings
    $voucher_product_id = 37; // Voucher product Id
    $max_quantity       = 20; // Here set the max item quantity (for voucher product)
    $min_subtotal       = 50;  // Starting threshold min amount
    $cart_subtotal      = 0;  // Initializing

    // Loop through cart items (first loop)
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // When voucher product is is cart
        if ( $voucher_product_id == $cart_item['product_id'] ) {
            $voucher_key = $cart_item_key;
            $voucher_qty = $cart_item['quantity'];
            // $cart_item['data']->set_price(0); // (optional) set the price to zero
        } else {
            $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax']; // Get subtotal
        }
    }

    // If voucher product is not already in cart, add it
    if ( ! isset($voucher_key) && $cart_subtotal >= $min_subtotal ) {
        $cart->add_to_cart( $voucher_product_id );
    }
    // Check vouvher product quantity and adjust it depending on the subtotal amount.
    else {
        // Loop dynamically through subtotal threshold min and max amounts setting the right quantity
        // $i is the min amount, $j the max amount and $q the related quantity to check
        for ( $i = $min_subtotal, $j = 100, $q = 1; $q < $max_quantity; $i += 50, $j += 50, $q++ ) {
            if ( $cart_subtotal >= $i && $cart_subtotal < $j && $voucher_qty != $q ) {
                $cart->set_quantity( $voucher_key, $q );
            }
        }
        if ( $cart_subtotal >= $i && $voucher_qty != $q ) {
            $cart->set_quantity( $voucher_key, $q );
        }
    }
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM