Как рассчитать общую экономию в Woocommerce - PullRequest
0 голосов
/ 11 апреля 2020

UPD
Решено:

function wc_discount_total_30() {

    global $woocommerce;

    $discount_total = 0;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {

   $_product = $values['data'];

        if ( $_product->is_on_sale() ) {
        $regular_price = $_product->get_regular_price();
        $sale_price = $_product->get_sale_price();
        $discount = ($regular_price - $sale_price) * $values['quantity'];
        $discount_total += $discount;
        }

    }    

if ( $woocommerce->cart->subtotal + $woocommerce->cart->shipping_total > $woocommerce->cart->total + $woocommerce->cart->shipping_total || $discount_total >0 ) {

    echo '<tr class="cart-discount">
    <th>'. __( 'Saved:', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'Saved', 'woocommerce' ) .' ">' 
    . wc_price( $woocommerce->cart->subtotal - $woocommerce->cart->total + $woocommerce->cart->shipping_total + $discount_total ) .'</td>
    </tr>';
}    

}

// Hook our values to the Basket and Checkout pages

add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99);

-------------------------- ------------------------------------------

ОРИГИНАЛЬНЫЙ ВОПРОС:

Я хочу рассчитать и отобразить «Общая экономия» на странице «Корзина и оформление заказа» (Woocommerce). Я пытался использовать этот код, но он работает, только если в моей корзине есть хотя бы один предмет со скидкой:

function wc_discount_total_30() {
    global $woocommerce;
    $discount_total = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ( $_product->is_on_sale() ) {
            $regular_price = $_product->get_regular_price();
            $sale_price = $_product->get_sale_price();
            $discount = ($regular_price - $sale_price) * $values['quantity'];
            $discount_total += $discount;
        }
    }
    if ( $discount_total > 0 ) {
        echo '<tr class="cart-discount">
        <th>'. __( 'Saved', 'tsavedis' ) .'</th>
        <td data-title=" '. __( 'Saved', 'tsavedis' ) .' ">'
        . wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
        </tr>';
    }
}

// Hook our values to the Basket and Checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99); 

Но мне нужно рассчитать сумму скидки с учетом купона и количество товаров в корзине (есть ли в корзине товары со скидкой или нет). Вот мой код для отображения скидки в зависимости от количества товаров в корзине:

function woo_discount_total(WC_Cart $cart) {

    $woo_count_item = $cart->get_cart_contents_count(); // Number of items in the shopping cart

    if($woo_count_item >= 3 && $woo_count_item <= 4) {

        $discount = $cart->subtotal * 0.10; // 0.10 — 10%
        $cart->add_fee("10% discount for 3-4 items in the shopping cart", -$discount);

    } elseif($woo_count_item >= 5 && $woo_count_item <= 100) {

        $discount = $cart->subtotal * 0.20; // 0.20 — 20%
        $cart->add_fee("20% discount for 5 and more items in the shopping cart", -$discount);

    }

}

add_action("woocommerce_cart_calculate_fees" , "woo_discount_total");

Что я хочу отобразить:

Подытог корзины: $ 1000
Купон: - $ 50
10% скидка на 3 товара в корзине: - $ 100
Общая сумма заказа: $ 850
Сохранено: $ 150

Я думаю, что самый простой способ сделать это, чтобы вычесть «итоговую сумму заказа» из «промежуточной суммы корзины». Есть ли способ сделать это?

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