I am trying to display the name of the box selected by my shipping method, in this case I am using this plugin for shipping over USPS: USPS powered by Woocommerce Services and Jetpack (https://wordpress.org/plugins/woocommerce-services/)
I want to show the name of the cardboard box selected by the shipping method on my cart-totals.php, almost like the debug that my shipping plugin does but without having to enable the debug, I want to do this to make some sort of quote system, I was able to pull the weights but I haven't had the same luck with the packaging names since they seem to be stored in an array and right now I don't have the necessary knowledge to pull that one but I'll get there!
So far thanks to @LoicTheAztec I was able to pull the raw data from the array using his suggested code:
// 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>';
I had to use output for the raw data because the previous loop wouldn't show the package name but it is there on the raw data, the raw data is the following:
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
)
)
)
At the end I just want to show the content of the [box_id] for each one of them, in this case I want to just show the "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
Instead try:
// 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>';