2017-03-06 61 views
1

我使用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变量的值。

如何解决这个问题?

回答

0

在计算运费,增加一个会话变量与新的计算值这样的功能:

//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()->session->set('Econt_Express_Shipping_Method' , $extracost);  
     } 
    } 
} 

然后在计算运费功能使用WC会话变量设定值的成本如下图所示:

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' => WC()->session->get('Econt_Express_Shipping_Method'), 
     'taxes' =>false, 
     //'calc_tax' => 'per_item' 
    ); 

    // Register the rate 
    $this->add_rate($rate); 

} 

使用下面的JavaScript,在结帐时更新新的航运价值:

$('body').trigger('update_checkout', { update_shipping_method: true }); 

如果您在结帐时多次使用上述代码,您可能需要更改帐单地址或送货地址或国家/地区的值,以便能够更新您的价值。这也是我坚持和被迫假冒这些变化,以便Woocommerce可以更新我的运输价值...我仍然在寻找解决方案,虽然...大声笑:)

$("#billing_address_1").trigger("keypress").val(function(i,val){return val + 'a';}); 
相关问题