根据WooCommerce中的运输类别自定义计算的运输成本

时间:2019-04-01 16:08:59

标签: php wordpress object woocommerce checkout

昨晚将Woocommerce更新到最新版本3.5.7后,如果在结帐页面上将国家从美国切换到加拿大,则结帐将冻结。如果我们返回购物车页面,则会显示错误。错误详细信息在此问题的末尾。是什么原因造成的?我还复制了下面的代码片段,导致出现此错误。具体来说,行 $originalDate = $yourDateVariable; $newDate = date("d-m-Y", strtotime($originalDate)); ,但是我看不到任何问题。

我已经测试过的内容
1-我首先想到在运输标签上使用花括号会损坏,但这不是问题,已经在演示站点上进行了测试。
2-仅当购物车中有特定的送货方式时才会发生这种情况,对于其他送货方式则不会发生,如下面的屏幕投射视频所突出显示。


编辑#1:

我与Woocommerce支持人员进行了交谈,他们说我们正在使用旧的模板版本,因此我们必须对其进行更新。我现在正在更新模板,但也怀疑无法解决问题。有什么想法吗?

屏幕录像:
https://screencast-o-matic.com/watch/cqfVoTZckd

$label = $method->get_label();中引起问题的代码:

cart-shipping.php

错误:

foreach ( $available_methods as $method ) : ?> 
                    <li>
                        <?php
                            $a = (int)$method->cost;
                            $b = $method->id;
                            $label = $method->get_label(); /* this line is causing the error but I don't see any issue with it */
                            if ($a === 0 && $b != "legacy_local_pickup"):
                                printf( '<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />
                                <label for="shipping_method_%1$d_%2$s">Standard (Ships in 10-12 work days): <span class="woocommerce-Price-amount amount">Free</span></label>',
                                $index, sanitize_title( $method->id ), esc_attr( $method->id ), checked( $method->id, $chosen_method, false ) );
                            elseif ($b == "legacy_local_pickup"):
                                printf( '<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />
                                <label for="shipping_method_%1$d_%2$s">%5$s</label>',
                                $index, sanitize_title( $method->id ), esc_attr( $method->id ), checked( $method->id, $chosen_method, false ), $label );    
                            else:
                                printf( '<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />
                                <label for="shipping_method_%1$d_%2$s">%5$s</label>',
                                $index, sanitize_title( $method->id ), esc_attr( $method->id ), checked( $method->id, $chosen_method, false ), wc_cart_totals_shipping_method_label( $method ) );   
                            endif;

                            do_action( 'woocommerce_after_shipping_rate', $method, $index );
                        ?>
                    </li>
                <?php endforeach; 

编辑#2:

在登台现场进行测试后,我找到了真正的原因。这是创建问题的代码。问题是,Fatal error: Uncaught Error: Call to undefined method stdClass::get_label() in .../themes/genesis-sample/woocommerce/cart/cart-shipping.php:35 Stack trace: #0 .../plugins/woocommerce/includes/wc-core-functions.php(211): include() #1 .../plugins/woocommerce/includes/wc-cart-functions.php(233): wc_get_template('cart/cart-shipp...', Array) #2 .../plugins/woocommerce/templates/cart/cart-totals.php(48): wc_cart_totals_shipping_html() #3 .../plugins/woocommerce/includes/wc-core-functions.php(211): include('...') #4 .../plugins/woocommerce/includes/wc-template-functions.php(1922): wc_get_template('cart/cart-total...') #5 .../wp-includes/class-wp-hook.php(286): woocommerce_cart_totals('') in .../themes/genesis-sample/woocommerce/cart/cart-shipping.php on line 35 仅适用于美国,不适用于加拿大,因此,当客户切换到加拿大时,这段代码会出错。对于加拿大,可用的送货方式为$method_id5

$method_id6 = 'flat_rate:6';

对此有何想法?

1 个答案:

答案 0 :(得分:1)

更新#1: (基于您的第二次修改)

请尝试以下操作:

add_filter( 'woocommerce_package_rates', 'custom_shipping_rates_based_on_shipping_class', 11, 2 );
function custom_shipping_rates_based_on_shipping_class( $rates, $package ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE define your shipping class to find
    $class = array(206);

    // HERE define the shipping method to change rates for
    $shipping_rate_ids = array('flat_rate:5', 'flat_rate:6')

    // Initialising
    $found = false;
    $item_price = $item_qty = $rush_fee = $item_total = 0;

    // Loop through cart items
    foreach( $package['contents'] as $cart_item_key => $cart_item ) {
        $item_shipping_class_id = $cart_item['data']->get_shipping_class_id();

        if( in_array( $item_shipping_class_id, $class ) ){
            $item_price += $cart_item['data']->get_price(); // Sum line item prices that have target shipping class
            $item_qty += $cart_item['quantity']; // Sum line item prices that have target shipping class
            $item_total = $item_price * $item_qty;
            $rush_fee = $item_total * 0.2;
            $found = true;  // Target shipping class found
        } 
    }

    if( $found ) {
        // Loop through shipping rates
        foreach( $rates as $rate_key => $rate ) {
            if( in_array($rate_key, $shipping_rate_ids) ) {
                if( $item_total > 0 && $item_total < 200 ) {
                    if($rush_fee < 25) {
                        $rates[$rate_key]->cost = 25 + 18.99;
                    } else {
                        $rates[$rate_key]->cost = $rush_fee + 18.99;
                    }
                }
                elseif ( $item_total > 200 ) {
                    if($rush_fee < 25) {
                        $rates[$rate_key]->cost = 25;
                    } else {
                        $rates[$rate_key]->cost = $rush_fee;
                    }
                }
            }
        }
    }
    return $rates;
}

代码在活动子主题(或主题)的function.php文件中。经过测试,可以正常工作。

  

刷新运输缓存: (必需)

     
      
  1. 此代码已保存在活动主题的function.php文件中。
  2.   
  3. 购物车是空的
  4.   
  5. 在运输区域设置中,禁用/保存任何运输方式,然后启用“后退” /保存。
  6.   

初始答案。

由于$methodstdClass实例对象而不是WC_Shipping_Rate实例对象,因此未为其定义方法get_label() (不存在) 您可以使用属性label

所以您只需要替换模板:

$label = $method->get_label();

通过

$label = $method->label; 

这应该停止此错误并解决您的问题。