因此,我试图创建一种 woocommerce 运输方法,该方法采用购物车小计并按用户定义的购物车小计百分比收取运费。作为实现这个目标的第一步,我所做的基本上是这样的
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
));
}
谁能告诉我我做错了什么?
汪汪一只猫