php 访问Woocommerce 3中的运费 meta数据

fhity93d  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(98)

我试图显示我的航运方法所选择的框的名称,在这种情况下,我使用这个插件航运超过美国邮政:美国邮政由Woocommerce服务和Jetpack(https://wordpress.org/plugins/woocommerce-services/)提供技术支持
我想在cart-totals.php上显示通过运输方式选择的纸板箱的名称,几乎就像我的运输插件所做的调试一样,但不必启用调试,我想这样做是为了建立某种报价系统,我能够拉的重量,但我还没有同样的运气与 Package 名称,因为他们似乎是存储在一个数组,现在我不'我没有必要的知识去拉那个,但我会做到的!
到目前为止,多亏了@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]的内容,在本例中,我只想显示“14 x10 x6”:

[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
djmepvbi

djmepvbi1#

请尝试:

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

相关问题