dongtiannai0654 2019-04-16 17:27
浏览 101
已采纳

根据商店woocommerce的总支出自动申请优惠券

I need to apply a previously created coupon in woocommerce cart based on total spent in shop by logged in users. Example, If user already spent $300 or more in previous orders, in the next order, automatically apply "xxx" coupon.

based on "Apply automatically a coupon based on specific cart items count in Woocommerce" answer thread, that is what I have so far:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupon_based_on_cart_items_count', 25, 1 );
function auto_add_coupon_based_on_cart_items_count( $user_id, $cart ) {

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

    // Setting and initialising variables
    $coupon = 'descuentolealtad'; // <===  Coupon code
    $matched = false;
    $customer = new WC_Customer( $user_id );

    if( $customer->get_total_spent >= 60 ){
        $matched = true; // Set to true
    }

    // If conditions are matched add coupon is not applied
    if( $matched && ! $cart->has_discount( $coupon )){
        // Apply the coupon code
        $cart->add_discount( $coupon );

        // Optionally display a message
        wc_add_notice( __('Descuento de Lealtad aplicado'), 'notice');
    }
    // If conditions are not matched and coupon has been appied
    elseif( ! $matched && $cart->has_discount( $coupon )){
        // Remove the coupon code
        $cart->remove_coupon( $coupon );

        // Optionally display a message
        //wc_add_notice( __('Descuento de Lealtad removido'), 'error');
    }
}   

I'm trying to use the dedicated get_total_spent() function from woocommerce but is giving me a blank screen.

Any help is appreciated.


Edit

That is my working code very different as I am using a negative fee:

add_action('woocommerce_cart_calculate_fees' , 'discount_based_on_customer_orders', 10, 1);
function discount_based_on_customer_orders( $cart_object ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;  
    // Getting "completed" customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
    ) );
    // Orders count
    $customer_orders_count = count($customer_orders);
    // The cart total
    $cart_total = WC()->cart->get_total(); // or WC()->cart->get_total_ex_tax()

    // First customer order discount
    if( empty($customer_orders) || $customer_orders_count == 0 ){
        $discount_text = __('Loyalty Discount', 'woocommerce');
        $discount = -7;
    }

}
  • 写回答

1条回答 默认 最新

  • duan1226 2019-04-16 18:19
    关注

    Updated - There is many mistakes in your code like:

    • For this hook there is only one variable argument: $cart… and not $user_id and $cart
    • Since Woocommerce 3, use the method get_total_spent() and not the property get_total_spent.
    • You should first check that user is logged in too.

    Use the following revisited code instead:

    add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupon_based_on_total_spent', 10, 1 );
    function auto_add_coupon_based_on_total_spent( $cart ) {
    
        if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_user_logged_in() )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Setting and initialising variables
        $coupon = 'summer'; // <===  Coupon code
        $spent_amount = 60;
    
        $customer = new WC_Customer(get_current_user_id());
    
        // If conditions are matched add coupon is not applied
        if( $customer->get_total_spent() >= $spent_amount && ! $cart->has_discount( $coupon )){
            // Apply the coupon code
            $cart->add_discount( $coupon );
    
            // Optionally display a message
            wc_add_notice( __('Descuento de Lealtad aplicado'), 'notice');
        }
        // If conditions are not matched and coupon has been appied
        elseif($customer->get_total_spent() < $spent_amount && $cart->has_discount( $coupon )){
            // Remove the coupon code
            $cart->remove_coupon( $coupon );
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should better works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作