Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取WooCommerce中产品的装运类别名称和装运成本_Php_Wordpress_Woocommerce_Rate_Shipping Method - Fatal编程技术网

Php 获取WooCommerce中产品的装运类别名称和装运成本

Php 获取WooCommerce中产品的装运类别名称和装运成本,php,wordpress,woocommerce,rate,shipping-method,Php,Wordpress,Woocommerce,Rate,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

我试图在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);
我可以用这个代码获得shipping类id和shipping类slug,但不能得到标签。我也不能得到的费用,特别是航运类,这是一个统一的费率为5欧元的定义在航运区一节

期待您的回复。

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>';
        }
    }
}
//此处设置目标装运区类型(方法ID)
$targeted_shipping_method_id='统一费率';
//产品装运类别ID
$product_class_id=$product->get_shipping_class_id();
$zone_id=array_key(数组(“”)+WC_Shipping_Zones::get_Zones());
//循环通过区域ID
foreach($zone\u id作为$zone\u id){
//获取shipping Zone对象
$shipping\u zone=新WC\u shipping\u zone($zone\u id);
//获取装运区域的所有装运方法值
$shipping_methods=$shipping_zone->get_shipping_methods(true,'values');
//循环通过区域ID
foreach($instance\u id=>$shipping\u方法的shipping\u方法){
//装运方法费率ID
$rate_id=$shipping_method->get_rate_id();
//装运方法ID
$method\u id=explode(“:”,$rate\u id);
$method\u id=重置($method\u id);
//以特定的运送方法ID为目标
如果($method\u id===$targeted\u shipping\u method\u id){
//获取装运方法标题(标签)
$title=$shipping_method->get_title();
$title=空($title)?$shipping\u method->get\u method\u title():$title;
//获取配送方式设置数据
$data=$shipping\u method->instance\u设置;
##费用:
//对于已定义的装运类别
如果(设置($product\U class\U id)和($product\U class\U id)为空($product\U class\U id)
&&isset($data['class\U cost.'$product\U class\U id])){
$cost=$data['class_cost.'$product_class_id];
}
//定义“无类别成本”时,无定义的装运类别
elseif(isset($product\U class\U id)和空($product\U class\U id)
&&isset($data['no_class_cost'])和&$data['no_class_cost']>0){
$cost=$data['no_class_cost'];
} 
//当没有定义装运类别且定义了“无类别成本”时
否则{
$cost=$data['cost'];
}
//测试输出
回显“”.$title.:“.$cost.

”; } } }

现在,如果您想访问配送方式数据

非常感谢您的回答-已经关注了好几天了!!