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

问题描述 投票:0回答:1

我正试图在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欧元的统一费率。

期待您的回复。

woocommerce shipping rate shipping-method
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.