根据 WooCommerce 购物车小计自动添加可变数量的特定产品

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

我正在尝试根据客户花费的总费用将礼品产品添加到购物车。这是一家出售礼券的餐厅。 该计划的运作方式如下:客户在单笔交易中每消费 50 英镑,他们将自动免费获得一张 10 英镑的礼券。因此,如果他们在礼券上花费 100 英镑,他们将免费获得 2x 10 英镑的礼券(依此类推)。 为了尽可能简单,我们限制了礼券的面额(25 英镑 - 250 英镑增加了 25 英镑的面额)。 我不是特别想支付 for/install 另一个需要 updated/managed 等的插件。老实说,这只是在圣诞节期间,所以执行此操作的代码片段是好得多。 我看到它的工作方式是客户将代金券添加到购物车,当他们去购物车时,它显示他们也有额外的 'free' 代金券。

我在网上找到了一些代码并对其进行了一些修改。代码如下:

   
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 无法正常工作。我可能让这太复杂了,没有仔细考虑,但有点帮助会很好。 我在网上找到了很多打折产品的代码,但没有免费赠送东西的代码。

此处将根据购物车小计自动将可变数量的“特定产品优惠券”添加到购物车。

以下代码基于答案代码。该代码自动生成购物车小计阈值(最小和最大金额)以及要添加到购物车的凭证产品的相关数量。

$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 文件。已测试并有效。