【问题标题】:Adding a promotional product when a certain cart amount is reached达到特定购物车金额时添加促销产品
【发布时间】:2017-02-11 06:41:47
【问题描述】:

我正在 WooCommerce 中寻找合适的钩子,因为当达到一定的购物车数量(例如 100 个常规单位)时,我需要将促销产品添加到购物车中。

我也用过钩子'init',但我觉得不对。

这是我的代码:

function add_free_product_to_cart(){
    global $woocommerce;
    $product_id = 2006; 
    $found = false;
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) 
    {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) 
        {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
            $found = true;
        }
        if(!$found)
        {
            $maximum = 100;
            $current = WC()->cart->subtotal;
            if($current > $maximum){
                $woocommerce->cart->add_to_cart( $product_id );
            }           
        }       
    }   
}
add_action( 'woocommerce_add_to_cart', 'add_free_product_to_cart' );

为此我应该使用哪个钩子?

或者你能给我一个类似问题的相关链接吗?

谢谢

【问题讨论】:

  • 对不起这个错误一定是 add_action( 'init', 'add_free_product_to_cart' );

标签: php wordpress woocommerce cart product


【解决方案1】:

由于您的目标是在购物车中添加促销产品,因此您可以使用 woocommerce_before_calculate_totals 钩子通过自定义构建函数来实现此目的。

如果客户更新购物车(也嵌入在该自定义功能中),您还必须删除该促销项目。

代码如下:

add_action( 'woocommerce_before_calculate_totals', 'adding_promotional_product', 10, 1 );
function adding_promotional_product( $cart ) {

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $promo_id = 99; // <=== <=== <=== Set HERE the ID of your promotional product
    $targeted_cart_subtotal = 100; // <=== Set HERE the target cart subtotal
    $has_promo = false;
    $subtotal = 0;

    if ( ! $cart->is_empty() ){

        // Iterating through each item in cart
        foreach ($cart->get_cart() as $item_key => $cart_item ){
            $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
            // If Promo product is in cart
            if( $product_id == $promo_id ) {
                $has_promo = true;
                $promo_key= $item_key;
            } else {
                // Adding subtotal item to global subtotal
                $subtotal += $cart_item['line_subtotal'];
            }
        }
        // If Promo product is NOT in cart and target subtotal reached, we add it.
        if( ! $has_promo && $subtotal >= $targeted_cart_subtotal ) {
            $cart->add_to_cart( $promo_id );
            // echo 'add';
        // If Promo product is in cart and target subtotal is not reached, we remove it.
        } elseif( $has_promo && $subtotal < $targeted_cart_subtotal ) {
            $cart->remove_cart_item( $promo_key );
        }
    }
}

此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码经过测试并且可以正常工作。

相关话题:WooCommerce - Auto add or auto remove a freebie product from cart

代码更新于 (2018-10-01)

【讨论】:

  • 我只有一个问题。关于测试,当我使用购物车中特定商品上的箭头来更新购物车中的商品数量(以达到一定的购物车数量)时,只有在我刷新页面或继续结帐...??这是一个奇怪的问题。无论如何我可以在添加或删除免费/促销项目时自动刷新页面,以确保客户始终查看正确的金额?
  • @LoicTheAztec 这还能用吗?我将它添加到我的函数文件并更改了促销 ID 和目标购物车总数,但该项目没有被添加
  • @Rob 刚刚测试了代码,它在上一个 woocommerce 版本 3.7 上仍然可以完美运行
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
相关资源
最近更新 更多