【发布时间】:2020-08-18 21:34:36
【问题描述】:
因此,我尝试创建一种 woocommerce 运输方法,该方法采用购物车小计并收取用户定义的购物车小计百分比作为运费。作为朝着这个目标迈出的第一步,我做的ehat基本上是这样的
class Subtotal_Percentage_Method extends WC_Shipping_Method {
// to store percentage
private $percentage_rate
// constructor that handles settings
// here is where i start calculation
public function calculate_shipping($packages = array()) {
$cost = $this->percentage_rate * 1000;
add_rate(array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
));
}
}
这个有效。但是,当我将 calculate_shipping 方法更改为在计算中使用购物车小计时,它不起作用
public function calculate_shipping($packages = array()) {
$subtotal = WC()->cart->subtotal;
$cost = $subtotal * $this->percentage_rate / 100;
add_rate(array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
));
}
谁能告诉我我做错了什么?
【问题讨论】:
标签: php wordpress woocommerce calculation shipping-method