我在我的WooCommercestore 里有一个免费的送货柜台.它通过以下代码实现:

// free shipping counter
add_action( 'woocommerce_before_cart', 'ml_free_shipping_cart_notice' );
  
function ml_free_shipping_cart_notice() {
  
   $min_amount = 150; //change this to your free shipping threshold
   
   $current = WC()->cart->subtotal;
  
   if ( $current < $min_amount ) {
      $added_text = 'Do darmowej przesyłki brakuje jeszcze ' . wc_price( $min_amount - $current ) . '!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Kontynuuj zakupy', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
  
}



// ************************************************************************************




// free shipping info after 150pln
add_action( 'woocommerce_before_cart', 'free_shipping_info' );
  
function free_shipping_info() {
  
   $min_amount = 150; //change this to your free shipping threshold
   
   $current = WC()->cart->subtotal;
  
   if ( $current >= $min_amount ) {
      $added_text = 'Przekroczyłeś 150zł! Twoje zamówienie wyślemy za darmo! :) Wybierz dowolny sposób wysyłki!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( $added_text );
      wc_print_notice( $notice, 'notice' );
   }
  
}

第一部分在购物车页面上显示计数器-如果购物车数量低于150pln,它将显示客户需要花费多少钱才能获得免费送货.如果客户超过150pln,代码的第二部分表示现在免费送货.

But there is a problem if customer uses discount code. Example, like on the attached screen enter image description here

  1. 购物车中的总金额=164,68pln,因此允许免费送货.
  2. 客户添加了15%的折扣代码,因此总数现在是139,98 PLN,在这种情况下,送货还不是免费的. 在这种情况下,柜台应该再次显示他需要花费多少(S)才能获得免费送货.

我想这是微不足道的简单,但不知道如何修改代码,以工作的预期.

推荐答案

要始终使用购物车小计(包括税和折扣),请添加以下函数:

// Utility function: Get cart subtotal including taxes and discounts
function cart_subtotal_incl_tax_with_discount() {
    return (float) WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax();
}

And replace, in your code, both WC()->cart->subtotal with:
cart_subtotal_incl_tax_with_discount()

Php相关问答推荐

通过激活模板在数据库中创建表

如何在搜索域名时重定向到登陆页?

PHP邮件表单无法识别重音/特殊字符

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

PHP-转义字符串内的双反斜杠

PHP测试字符串的所有POST值

Laravel Nova不使用数组键作为筛选器中的选项值

如何在execute语句中使用存储过程参数?

WooCommerce在收据页面获取产品下载URL

是否有条件判断是否发布了另一个Wordpress页面?

强制客户在注册时 Select WooCommerce角色

在WooCommerce中以编程方式添加特定产品后,将价格设置为零

PHP向API发送curl请求

使用用户元数据填写 woocommerce checkout 字段

添加或删除优惠券后自动刷新 WooCommerce checkout 表单字段

Google Sheets PHP API - 添加多个选项卡时出现问题

在WooCommerce可变产品中显示所选变体的自定义字段

WooCommerce - 调用功能ID不正确.不应直接访问订单属性

在 DomPDF 和 Laravel 中使用内联 css

使用 --exclude-dir 选项从 PHP 执行 grep 时出现问题