我使用Woocommerce装运方法的自定义插件。该插件运行一个扩展了WC_Shipping_ME方法的新类,但问题在于“成本”价格是在其他函数在场外的类中计算的。Woocommerce结帐页上的自定义装运方法的类函数
所以,在目前的I类有:
public function calculate_shipping($package=array()) {
/* $this->add_rate(array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => 0,
)); */
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 0,
'taxes' =>false,
//'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate($rate);
}
类外,在另一个文件中,我有一个功能,计算运输成本:
//add shipping cost to checkout total
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->shipping;
$wc_econt = new WC_Econt_Shipping_Method;
if (is_checkout() && (bool)$wc_econt->inc_shipping_cost == TRUE) {
if(!isset($_SESSION)){
session_start();
}
//write_log('session econt customer shipping cost:'.$_SESSION['econt_shipping_cost']);
$extracost = (isset($_SESSION['econt_shipping_cost']) ? $_SESSION['econt_shipping_cost'] : 0);
if ($extracost != '0') {
WC()->cart->add_fee(__('Econt Express Shipping Method','woocommerce-econt'), $extracost);
}
}
}
如果只是改变0到$ extracode的类功能,它不会更新结帐页面上的运费:
public function calculate_shipping($ package = array()){
/* $this->add_rate(array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => $extracost,
)); */
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $extracost,
'taxes' =>false,
//'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate($rate);
}
我猜这个类在woo_add_cart_fee函数之前运行,这就是为什么它不能得到$ extracost变量的值。
如何解决这个问题?