dongxie3352
2018-11-28 22:15
浏览 67
已采纳

Woocommerce格式价格显示购物车计算费用

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.

enter image description here

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条回答 默认 最新

  • duanao4503 2019-02-02 20:43
    已采纳

    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;
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题