我正在尝试显示我的送货方式所选框的名称,在这种情况下,我使用此插件在USPS上运送: USPS由Woocommerce Services和Jetpack(https://wordpress.org/plugins/woocommerce-services/)提供支持
我想在cart-totals.php上显示通过送货方式选择的纸板箱的名称,几乎就像我的送货插件所做的调试,但无需启用调试,我想这样做某种报价系统,我能够拉动重量,但我没有与包装名称相同的运气,因为它们似乎存储在一个阵列中,现在我还没有必要的知识拉那个,但我会到那里去!
到目前为止,感谢@LoicTheAztec,我能够使用他建议的代码从数组中提取原始数据:
// The chosen shipping method (string) - Output the Shipping method rate ID
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' )[0];
// The array of shipping methods enabled for the current shipping zone:
$shipping_methods = WC()->session->get('shipping_for_package_0')['rates'];
// Loop through the array
foreach ( $shipping_methods as $method_id => $shipping_rate ){
echo $shipping_rate->id . '<br>'; // combination of method_id + instance_id
echo $shipping_rate->method_id . '<br>'; // shipping method slug (string)
echo $shipping_rate->instance_id . '<br>'; // Numerical ID (string)
echo $shipping_rate->label . '<br>'; // Label name (string)
echo $shipping_rate->cost . '<br>'; // Cost (string)
echo $shipping_rate->taxes . '<br>'; // Cost taxes (array)
}
// OR output the raw data array (test)
echo '<pre>'; print_r( $shipping_methods ); echo '</pre>';
我不得不使用输出作为原始数据,因为前一个循环不会显示包名,但它在原始数据上,原始数据如下:
Array
(
[wc_services_usps:9:first_class_intl] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => wc_services_usps:9:first_class_intl
[method_id] => wc_services_usps
[instance_id] => 9
[label] => First-Class Package International Service
[cost] => 25.25
[taxes] => Array
(
)
)
[meta_data:protected] => Array
(
[wc_connect_packages] => Array
(
[0] => stdClass Object
(
[box_id] => 14x10x6
[length] => 14.5
[width] => 10.5
[height] => 6.75
[weight] => 2.565
[items] => Array
(
[0] => stdClass Object
(
[product_id] => 730
[length] => 1.5
[width] => 1.5
[height] => 4.5
[weight] => 0.28
[quantity] => 1
)
[1] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[2] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[3] => stdClass Object
(
[product_id] => 693
[length] => 3.25
[width] => 3.5
[height] => 3.25
[weight] => 0.28
[quantity] => 1
)
[4] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[5] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[6] => stdClass Object
(
[product_id] => 522
[length] => 3.25
[width] => 2.28
[height] => 7.75
[weight] => 0.135
[quantity] => 1
)
)
[id] => weight_0_14x10x6
[service_id] => first_class_intl
)
)
[Packaging] => 14x10x6
)
)
[wc_services_usps:9:pri_intl] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => wc_services_usps:9:pri_intl
[method_id] => wc_services_usps
[instance_id] => 9
[label] => Priority Mail International
[cost] => 42.10
[taxes] => Array
(
)
)
[meta_data:protected] => Array
(
[wc_connect_packages] => Array
(
[0] => stdClass Object
(
[box_id] => 14x10x6
[length] => 14.5
[width] => 10.5
[height] => 6.75
[weight] => 2.565
[items] => Array
(
[0] => stdClass Object
(
[product_id] => 730
[length] => 1.5
[width] => 1.5
[height] => 4.5
[weight] => 0.28
[quantity] => 1
)
[1] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[2] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[3] => stdClass Object
(
[product_id] => 693
[length] => 3.25
[width] => 3.5
[height] => 3.25
[weight] => 0.28
[quantity] => 1
)
[4] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[5] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[6] => stdClass Object
(
[product_id] => 522
[length] => 3.25
[width] => 2.28
[height] => 7.75
[weight] => 0.135
[quantity] => 1
)
)
[id] => weight_0_14x10x6
[service_id] => pri_intl
)
)
[Packaging] => 14x10x6
)
)
[wc_services_usps:9:pri_exp_intl] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => wc_services_usps:9:pri_exp_intl
[method_id] => wc_services_usps
[instance_id] => 9
[label] => Priority Mail Express International
[cost] => 56.35
[taxes] => Array
(
)
)
[meta_data:protected] => Array
(
[wc_connect_packages] => Array
(
[0] => stdClass Object
(
[box_id] => 14x10x6
[length] => 14.5
[width] => 10.5
[height] => 6.75
[weight] => 2.565
[items] => Array
(
[0] => stdClass Object
(
[product_id] => 730
[length] => 1.5
[width] => 1.5
[height] => 4.5
[weight] => 0.28
[quantity] => 1
)
[1] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[2] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[3] => stdClass Object
(
[product_id] => 693
[length] => 3.25
[width] => 3.5
[height] => 3.25
[weight] => 0.28
[quantity] => 1
)
[4] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[5] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[6] => stdClass Object
(
[product_id] => 522
[length] => 3.25
[width] => 2.28
[height] => 7.75
[weight] => 0.135
[quantity] => 1
)
)
[id] => weight_0_14x10x6
[service_id] => pri_exp_intl
)
)
[Packaging] => 14x10x6
)
)
[wc_services_usps:9:pri_intl_lg_box] => WC_Shipping_Rate Object
(
[data:protected] => Array
(
[id] => wc_services_usps:9:pri_intl_lg_box
[method_id] => wc_services_usps
[instance_id] => 9
[label] => Flat Rate: Priority Mail International
[cost] => 62.35
[taxes] => Array
(
)
)
[meta_data:protected] => Array
(
[wc_connect_packages] => Array
(
[0] => stdClass Object
(
[box_id] => large_flat_box
[length] => 12
[width] => 12
[height] => 5.5
[weight] => 1.895
[items] => Array
(
[0] => stdClass Object
(
[product_id] => 730
[length] => 1.5
[width] => 1.5
[height] => 4.5
[weight] => 0.28
[quantity] => 1
)
[1] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[2] => stdClass Object
(
[product_id] => 756
[length] => 1.76
[width] => 1.76
[height] => 3.75
[weight] => 0.22
[quantity] => 1
)
[3] => stdClass Object
(
[product_id] => 693
[length] => 3.25
[width] => 3.5
[height] => 3.25
[weight] => 0.28
[quantity] => 1
)
[4] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[5] => stdClass Object
(
[product_id] => 685
[length] => 2.75
[width] => 2.75
[height] => 5.25
[weight] => 0.38
[quantity] => 1
)
[6] => stdClass Object
(
[product_id] => 522
[length] => 3.25
[width] => 2.28
[height] => 7.75
[weight] => 0.135
[quantity] => 1
)
)
[id] => flat-priority_international_0_large_flat_box
[service_id] => pri_intl_lg_box
)
)
[Packaging] => Large Flat Rate Box
)
)
)
最后我只是想为每一个显示[box_id]的内容,在这种情况下我想只显示&#34; 14x10x6&#34;:
[meta_data:protected] => Array
(
[wc_connect_packages] => Array
(
[0] => stdClass Object
(
[box_id] => 14x10x6
[length] => 14.5
[width] => 10.5
[height] => 6.75
[weight] => 2.565
[items] => Array
答案 0 :(得分:0)
而是尝试:
// The chosen shipping method (string) - Output the Shipping method rate ID
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' )[0];
// The array of shipping methods enabled for the current shipping zone:
$shipping_methods = WC()->session->get('shipping_for_package_0')['rates'];
// Loop through the array
foreach ( $shipping_methods as $method_id => $shipping_rate ){
$rate_id = $shipping_rate->id;
$method_id = echo $shipping_rate->method_id;
$instance_id = echo $shipping_rate->instance_id;
$label = echo $shipping_rate->label;
$cost = $shipping_rate->cost;
$taxes = $shipping_rate->taxes;
// Get the meta data in an unprotected array
$meta_data = $shipping_rate->get_meta_data();
## ----- BELOW the data you are looking for ----- ##
$connect_packages = $meta_data['wc_connect_packages'][0];
$box_id = $connect_packages->box_id; // (string)
$length = $connect_packages->length; // (float)
$width = $connect_packages->width; // (float)
$height = $connect_packages->height; // (float)
$weight = $connect_packages->weight; // (float)
$items = $connect_packages->items; // (array of object)
}
// OR output the raw data array (test)
echo '<pre>'; print_r( $shipping_methods ); echo '</pre>';