php [UPS运输方式]将UPS作为特定运输类别的运输选项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [UPS运输方式]将UPS作为特定运输类别的运输选项相关的知识,希望对你有一定的参考价值。

/**
 * Check the cart for specific classes, remove UPS Shipping method if they are present
 * 
 * REMOVE THE TOP <?php if there is no ?> before (or you have an error after adding this)
 * 
 * Add the code to your theme functions.php file
 */
add_filter( 'woocommerce_package_rates', 'unset_ups_shipping_method' , 10, 2 );
function unset_ups_shipping_method( $rates, $package ) {
 
    // Setup an array of shipping classes that do not allow UPS Shipping (@todo change this)
    $shippingclass_array = array( 'no-ups' );
 
    /**
     * loop through the cart looking for the products in the array above
     * and unset the Free shipping methods as necessary
     */ 
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );

        if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
            /**
             * Unset the shipping methods here
             */
            unset( $rates['ups:01'] );
            unset( $rates['ups:02'] );
            unset( $rates['ups:03'] );
            unset( $rates['ups:04'] );
            unset( $rates['ups:05'] );
            unset( $rates['ups:06'] );
            unset( $rates['ups:07'] );
            unset( $rates['ups:08'] );
            unset( $rates['ups:09'] );
            unset( $rates['ups:10'] );
            unset( $rates['ups:11'] );
            unset( $rates['ups:12'] );
            unset( $rates['ups:13'] );
            unset( $rates['ups:14'] );
            unset( $rates['ups:15'] );
 
            // The rates have been removed, no point in carrying on
            //break;
        }
    }
 
    // Return what's left of the $rates array
    return $rates;
}

以上是关于php [UPS运输方式]将UPS作为特定运输类别的运输选项的主要内容,如果未能解决你的问题,请参考以下文章

Magento 运输例外

计算用于运输的最佳包装箱尺寸的最佳方法是啥? [复制]

Shippo API 导入错误

根据运输类别和商品数量添加 Woocommerce 费用

WooCommerce 中特定运输类别、最低金额和地理定位国家/地区的购物车消息

在 WooCommerce 中隐藏特定运输方式的结帐运输字段部分