【发布时间】:2018-04-09 20:59:18
【问题描述】:
我正在尝试显示通过我的运输方式选择的盒子的名称,在这种情况下,我使用此插件通过 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] 的内容,在这种情况下我只想显示“14x10x6”:
[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
【问题讨论】:
-
"Woocommerce Services" 如果您不添加插件的链接,说明您的配置的所有相关详细信息和设置......请记住,您的运输配置非常具体,没有人可以猜测、可视化或重现应该包含在某处的相关数据……这个问题对于社区来说是不清楚和无法解决的……
-
嗯,你给我的代码正在运行,我可以看到完整的数组,我想要的信息就在那个数组中,我不知道,现在仍然不知道不知道怎么拉它。无论如何,谢谢你,我很抱歉没有把我的问题说得足够清楚。
-
我的回答永远不会完全回答这种问题,因为我无法重现你的配置......我的回答是为了帮助你找到数据(已经有效) i> 在那种情况下,这个答案很好而且很有帮助,应该被你接受......现在你可以编辑你的问题,添加你得到的原始数据输出并解释你需要得到什么......这样我可以帮助你......但是这需要清楚,有足够的细节......
-
已编辑,再次为不够具体表示歉意。
标签: php wordpress woocommerce cart shipping