【问题标题】:Display Total minus Shipping Cost on Cart在购物车上显示总计减去运费
【发布时间】:2021-08-12 18:19:50
【问题描述】:

我试图在购物车上回显一个数字: 总计 - 优惠券 - 运费

$first_number = $woocommerce->cart->total;
$second_number = $woocommerce->cart->get_cart_shipping_total;
$sum_total = $first_number - $second_number;
echo $sum_total;

并收到此错误:警告:遇到一个非数字值。

更新

我得到了购物车的总数并删除了运费。现在完美运行!

global $woocommerce;
$cart_total = $woocommerce->cart->total;
$current_shipping_method_cost = $woocommerce->cart->get_shipping_total();
$sum_total = (float)$cart_total - (float)$current_shipping_method_cost;

【问题讨论】:

  • 感谢我的解决方案帮助您。

标签: php wordpress woocommerce


【解决方案1】:

我们遇到的问题是一行 $woocommerce->cart->get_cart_shipping_total;

"get_cart_shipping_total" 返回带有货币符号的格式化运费(例如 $5.00),这就是减法运算返回错误的原因。

你应该替换

$second_number = $woocommerce->cart->get_cart_shipping_total

$second_number = $woocommerce->cart->get_shipping_total();

完整的工作代码是:

add_action( 'woocommerce_calculate_totals', 'woo_update_subtotal' );  
function woo_update_subtotal() {

   $cart_subtotal = WC()->cart->subtotal;
   $current_shipping_method_cost = WC()->cart->get_shipping_total();

   $sum_total = (float)$cart_subtotal - (float)$current_shipping_method_cost;
WC()->cart->set_subtotal($sum_total);

   return $sum_total;
}

===========================

===========================

add_action( 'woocommerce_calculate_totals', 'woo_update_subtotal' );  
function woo_update_subtotal() {
    global $woocommerce;
    $cart_subtotal = $woocommerce->cart->subtotal;
    $current_shipping_method_cost = $woocommerce->cart->get_shipping_total();
    
    $sum_total = (float)$cart_subtotal - (float)$current_shipping_method_cost;
    $woocommerce->cart->set_subtotal($sum_total);

    return $sum_total;
}

【讨论】:

  • 非常感谢,您帮我找到了解决方案,但是代码输出错误的价格我不知道原因。
  • 当我在我的 wp 网站上测试它返回当前价格时没有任何问题
  • 首先检查 $cart_subtotal 和 $current_shipping_method_cost。您是否正确或不喜欢 echo $current_shipping_method_cost。
猜你喜欢
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多