WooCommerce Checkout页面上的自定义运输方法的类功能



我将自定义插件用于WooCommerce运输方法。该插件运行了一个扩展WC_SHIPPT_METHOD的新类,但是问题是在其他功能中计算出"成本"价格。

所以在当前类中有:

        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,则不会更新结帐页面上的运输成本:

公共功能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 varible的值。

如何解决此问题?

在计算运输成本的功能中,与您的新计算值添加会话变量:

//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';}); 

最新更新