繁体   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