在 WooCommerce 中获取产品的运输类别名称和运输成本

ham*_*rom 3 php wordpress rate woocommerce shipping-method

我正在尝试在 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);
Run Code Online (Sandbox Code Playgroud)

我可以使用此代码获取运输类别 ID 以及运输类别 slug,但不能获取标签。我也无法获得该特定运输等级的费用,该费用是运输区域部分中定义的 5 欧元的统一费率。

期待您的回复。

Loi*_*tec 6

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;
}
Run Code Online (Sandbox Code Playgroud)

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>';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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