问题:在 WooCommerce 中每 10 个订单应用折扣

我需要在下 10 个订单后给客户 10% 的折扣,下一个 10 个和下一个 10 个。

我找到了一些代码并对其进行了修改。所以基于Apply a coupon programmatically in Woocommerceanswer code,这是我的代码尝试:

///* 10% Discount for10 subsequent order and repeating for every 10th purchase

add_action('woocommerce_before_calculate_totals', 'discount_based_on_10 orders');
function discount_based_on_10_orders( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Your settings
    $coupon_code      = '10 Or More Discount'; // Coupon code
    $total_orders = 10; // 10th order discount

    // Initializing variables
    $applied_coupons  = $cart->get_applied_coupons();
    $coupon_code      = sanitize_text_field( $coupon_code );

    // Applying coupon
    if( ! in_array($coupon_code, $applied_coupons) && $total_orders >= $10 ){
        $cart->add_discount( $coupon_code );
        wc_clear_notices();
        wc_add_notice( sprintf(
            __("Your have a surprise %s A discount has been Applied! $ You have reached your 10th order discount %s.", "woocommerce"),
            wc_format_order( $order_number ), '10%', '<strong>' . wc_format_order({
$total_orders ) . '</strong>'
        ), "notice");
    }
    // Removing coupon
    elseif( in_array($coupon_code, $applied_coupons) && $total_orders < $order_number ){
        $cart->remove_coupon( $coupon_code );
    }
}

然而,这并没有给出预期的结果。我有一个优惠券代码 10 或更多折扣。我不知道如何测试它,它不是我的网站,所以我正在寻求快速学习曲线。

解答

关于您的代码尝试的一些评论/建议:

  • 不要使用优惠券,而是负费用。这样做的好处是您不必为已经知道折扣代码并因此自己输入优惠券的用户使用优惠券提供安全性

  • $total_orders没有定义,统计订单可以使用wc_get_customer_order_count()函数

  • 上面的功能立即保证了这只对有账号的用户有效

  • 当用户已经有 9、19、29 等订单时,应用折扣/显示消息,因为当前订单是第 10、20、30 等。

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Gets the current user’s ID.
    $user_id = get_current_user_id();

    // 10%
    $percent = 10;

    // User ID exists
    if ( $user_id >= 1 ) {
        // Get the total orders by a customer.
        $order_count = wc_get_customer_order_count( $user_id );

        // Every 10th order
        if ( $order_count % 10 == 9 ) {
            // Discount calculation
            $discount = $cart->cart_contents_total * $percent / 100;

            // Only on cart page
            if ( is_cart() ) {
                // Message
                $message = sprintf( __( 'Your have a surprise, A discount has been applied! This is your %sth order', 'woocommerce' ), $order_count + 1 );

                // Check if a notice has already been added
                if ( ! wc_has_notice( $message ) ) {
                    wc_clear_notices();

                    wc_add_notice( $message, 'notice' );
                }
            }

            // Set the discount (For discounts (negative fee) the taxes as always included)
            $cart->add_fee( __( 'Discount', 'woocommerce' ) . " (" . $percent . "%)", -$discount );
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
Logo

WooCommerce社区为您提供最前沿的新闻资讯和知识内容

更多推荐