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

时间:2019-04-16 17:27:23

标签: php wordpress woocommerce cart discount

我需要根据登录用户在商店中的总支出在woocommerce购物车中应用先前创建的优惠券。例如,如果用户在先前的订单中已经花费了$ 300或更多,则在下一个订单中,自动应用“ xxx”优惠券。

基于Apply automatically a coupon based on specific cart items count in Woocommerce答案线程,这就是我到目前为止的内容:

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');
    }
}   

我正在尝试使用woocommerce专用的get_total_spent()功能,但是却给了我空白的画面。

感谢您的帮助。


修改

那是我的工作代码,与我使用负费用非常不同:

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 个答案:

答案 0 :(得分:3)

已更新-您的代码中有很多错误,例如:

  • 对于此钩子,只有一个变量参数:$cart…而不是$user_id$cart
  • 从Woocommerce 3开始,请使用方法get_total_spent(),而不要使用属性get_total_spent
  • 您应该首先检查用户是否也登录了。

请改用以下重新访问的代码:

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 );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该会更好。