0

Есть скидка на woocommerce

function woo_discount_total(WC_Cart $cart) {

$woo_current_price = $cart->subtotal; // Текущая итоговая сумма

if($woo_current_price >= 10000) {

    $discount = $cart->subtotal * 0.05; // 0.05 - это 5%
    $cart->add_fee("Скидка в 5% за заказ на сумму от 10 000 рублей ", -$discount);

} elseif($woo_current_price > 10000) {

    $discount = $cart->subtotal * 0.05; // 0.05 - это 5%
    $cart->add_fee("Скидка в 5% за заказ на сумму более 10 000 рублей ", -$discount);

}

}

add_action("woocommerce_cart_calculate_fees" , "woo_discount_total");

Вопрос: можно ли скидку привязать к пользователю, чтоб при разовой покупке в 10000 руб. скидка в 5% уже была постоянная?

1 ответ 1

2

Добавьте новый метод:

public function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' )
    ) );

    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

Добавьте условие в существующий код:

elseif( get_customer_total_order() > 10000 ) { 

    $discount = $cart->subtotal * 0.05; // 0.05 - это 5%
    $cart->add_fee( "Сумма ваших предыдущих заказов превышает 10 000 рублей, действует скидка в 5% на данный заказ", -$discount );    
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.