【问题标题】:Access Shipping rates meta data in Woocommerce 3在 Woocommerce 3 中访问运费元数据
【发布时间】: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


【解决方案1】:

改为尝试:

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

【讨论】:

  • 我得到了关于每批货物的信息,但没有得到我唯一想要的盒子的名称,我得到了带有测试回声的完整数组,并且我能够在 foreach ( $shipping_methods ->wc_connect_packages 作为 $box_id => $shipping_rate ){ echo $shipping_rate->box_id 。 '
    '; // 运输方式 slug (string) }
  • @DanielMonsalve 由于您似乎使用了一些额外的运输插件或 Woocommerce 中不默认的东西,如果您没有在问题中详细解释清楚,我无法帮助您本身... 我的代码只是向您展示了理解和获取存储在 Woocommerce 会话中的可用运输数据的方法... stackOverFlow 中的任何人都无法猜测您的自定义运输箱或其他任何东西
  • 您好@LoicTheAztec,我在主要问题上说我使用“Woocommerce Services”作为运输方式。 “Woocommerce Services”实际上是 Woocommerce 的一个插件,可将自身连接到 Jetpack 以获取实时 USPS 运费、标签和税收计算。
  • @Daniel 好的,这次你知道了方法和数据
  • 非常感谢!!现在它可以工作了!,我只需要删除一个额外的右括号并添加一些分号,瞧!
猜你喜欢
  • 2019-03-16
  • 2019-03-25
  • 2018-10-27
  • 2017-09-21
  • 2020-03-21
  • 2016-12-22
  • 2017-09-27
  • 2021-06-20
  • 2019-08-06
相关资源
最近更新 更多