簡體   English   中英

在 WooCommerce 中為每 10 個訂單應用折扣

[英]Apply discount for every 10th order in WooCommerce

我需要在下 10 個訂單后給客戶 10% 的折扣,下一個 10 個和下一個 10 個。

我找到了一些代碼並對其進行了修改。 因此,基於在 Woocommerce 答案代碼中以編程方式應用優惠券,這是我的代碼嘗試:

///* 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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM