首页 > 解决方案 > 在 WooCommerce 中获取产品的运输类别名称和运输成本

问题描述

我正在尝试在 WooCommerce 单一产品页面上显示产品的运输类别名称和运输统一费率。我使用了下面的代码:

$shipping_class_id = $product->get_shipping_class_id();
$shipping_class= $product->get_shipping_class();
$fee = 0;

if ($shipping_class_id) {
   $flat_rates = get_option("woocommerce_flat_rates");
   $fee = $flat_rates[$shipping_class]['cost'];
}

$flat_rate_settings = get_option("woocommerce_flat_rate_settings");
echo 'Shipping cost: ' . ($flat_rate_settings['cost_per_order'] + $fee);

我可以使用此代码而不是标签获取运输类别 ID 和运输类别 slug。我也无法获得特定航运等级的成本,即航运区域部分定义的 5 欧元统一费率。

期待您的回复。

标签: phpwordpresswoocommercerateshipping-method

解决方案


1)获取运输类别标签名称(来自产品):

运输类别已注册为一个术语,因此您可以使用以下方法轻松获取产品运输类别标签:

$shipping_class_id   = $product->get_shipping_class_id();
$shipping_class_term = get_term($shipping_class_id, 'product_shipping_class');

if( ! is_wp_error($shipping_class_term) && is_a($shipping_class_term, 'WP_Term') ) {
    $shipping_class_name  = $shipping_class_term->name;
}

2)运输方式:

由于运输方式基于客户运输地点,如果您有多个运输区域,您无法真正为产品获得您想要的特定运输方式。

此外,根据您所有不同的运输设置,运输成本是从多种可能性的购物车包裹中计算出来的。

现在,如果您只有一个运输区域,并且您想要定位特定的运输方式并获取运输方式标题及其大致成本(如在设置中),请根据产品税级尝试以下操作:

// HERE set your targeted shipping Zone type (method ID)
$targeted_shipping_method_id = 'flat_rate';

// The product shipping class ID
$product_class_id = $product->get_shipping_class_id();

$zone_ids = array_keys( array('') + WC_Shipping_Zones::get_zones() );

// Loop through Zone IDs
foreach ( $zone_ids as $zone_id ) {
    // Get the shipping Zone object
    $shipping_zone = new WC_Shipping_Zone($zone_id);
    // Get all shipping method values for the shipping zone
    $shipping_methods = $shipping_zone->get_shipping_methods( true, 'values' );

    // Loop through Zone IDs
    foreach ( $shipping_methods as $instance_id => $shipping_method ) {
        // Shipping method rate ID
        $rate_id = $shipping_method->get_rate_id();

        // Shipping method ID
        $method_id = explode( ':', $rate_id);
        $method_id = reset($method_id);

        // Targeting a specific shipping method ID
        if( $method_id === $targeted_shipping_method_id ) {
            // Get Shipping method title (label)
            $title = $shipping_method->get_title();
            $title = empty($title) ? $shipping_method->get_method_title() : $title;

            // Get shipping method settings data
            $data = $shipping_method->instance_settings;

            ## COST:

            // For a defined shipping class
            if( isset($product_class_id) && ! empty($product_class_id) 
            && isset($data['class_cost_'.$product_class_id]) ) {
                $cost = $data['class_cost_'.$product_class_id];
            }
            // For no defined shipping class when "no class cost" is defined
            elseif( isset($product_class_id) && empty($product_class_id) 
            && isset($data['no_class_cost']) && $data['no_class_cost'] > 0 ) {
                $cost = $data['no_class_cost'];
            } 
            // When there is no defined shipping class and when "no class cost" is defined
            else {
                $cost = $data['cost'];
            }

            // Testing output
            echo '<p><strong>'.$title.'</strong>: '.$cost.'</p>';
        }
    }
}

现在,如果您想访问运输方式数据


推荐阅读