Woocommerce format price display of cart calculated fees

别等时光非礼了梦想. 提交于 2019-12-25 03:18:16

问题


I am manually modifying the price of the cart total (to £10) when there are 4 items in the cart and none of the items are from the category: christmas.

This essentially gives the user a discount + "Free Shipping". However, the total does not match up and it can be confusing to the user as shipping is still shown as charged.

Is there a way to manually apply a dummy "Free Shipping Applied -£3.00" to the cart based on these conditions? Just to make it clearer for the user?

I understand that the hook I am using currently is to modify the price manually, but not for formatted price display.

add_filter( 'woocommerce_calculated_total', 'calculated_total', 10, 2 );
function calculated_total( $total, $cart ) {
    $taster_count = 4;
    $item_count   = $cart->get_cart_contents_count();
    $chistm_count = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( ! has_term( 'christmas', 'product_cat', $cart_item['product_id'] ) ) {
            $chistm_count += $cart_item['quantity'];
        }
    }
    if( $taster_count == $item_count && $chistm_count == $taster_count  )

        //HERE TRY TO DISPLAY A DUMMY FREE SHIPPING MESSAGE
        $discount = 3.00;
        $cart->add_fee( 'Taster Box Offer! Free Shipping', -$discount);

        return 10;
    return $total;
}

回答1:


Figured out how to solve this. I manually added a woocommerce_cart_calculate_fee of $3.00 then to minus this off the total to act as a "negative fee" i.e. A discount.

    add_filter( 'woocommerce_cart_calculate_fees', 'add_recurring_postage_fees2', 10, 1 );

function add_recurring_postage_fees2(WC_Cart $cart ) {

  $items_count  = WC()->cart->get_cart_contents_count();

    if ( $items_count == 4 ) {
        $discount = 3.00;
        $cart->add_fee( 'Taster Box Free Shipping', -$discount);
        return;
    }
}


来源:https://stackoverflow.com/questions/53528929/woocommerce-format-price-display-of-cart-calculated-fees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!