Автоматически применять скидку к роли пользователя, если в Woocommerce не применен купон - PullRequest
2 голосов
/ 09 октября 2019

У меня есть этот код ниже, но я хочу, чтобы он действовал только в том случае, если пользователь не использует другой код, и если пользователь использует другой купон, отключите его.

на основе " Применить скидку для определенной роли пользователя в Woocommerce" код ответа, я пытался посмотреть, как проверить, есть ли купон, но не могу его выяснить.

// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    // Only for 'company' user role
    if ( ! current_user_can('affiliate') )
        return; // Exit

    // Only for 'company' user role
    if ( ! current_user_can('affiliate') )
        return; // Exit

    // HERE define the percentage discount
    $percentage = 15;

    $discount = $cart->get_subtotal() * $percentage / 100;
    // Applying discount
    $cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}

Любая помощь приветствуется.

1 Ответ

0 голосов
/ 10 октября 2019

Попробуйте следующее, чтобы установить пользовательскую скидку на основе определенной роли пользователя, если в корзине нет примененных купонов:

// Applying conditionally a discount for a specific user role and no applied coupons
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    // Only for 'affiliate' user role
    if ( ! current_user_can('affiliate') )
        return; // Exit

    // Only if there is no applied coupons in cart
    if ( ! empty( $cart->get_applied_coupons() ) )
        return; // Exit

    // HERE define the percentage discount
    $percentage = 15;

    $discount = $cart->get_subtotal() * $percentage / 100; // Calculation

    // Applying discount
    $cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}

Код продолжается functions.php файл изваша активная детская тема (или активная тема). Проверено и работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...