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

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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样