【问题标题】:Woocommerce - Retrieve shipping classWoocommerce - 检索运输类
【发布时间】:2017-09-18 07:59:55
【问题描述】:

我在外部脚本上使用以下代码来检索所有产品(简单和可变):

    $args = array(
        'post_type'   => array('product', 'product_variation'),
        'numberposts' => -1,
        'post_status' => 'publish',
    );
    $shop_products = get_posts( $args );
    foreach ($shop_products as $item) {
        echo $item->ID.": shipping class is -> ".$item->get_shipping_class()."<br>";
    }

我需要创建一个包含自己的运输类别的产品列表,但它不起作用。 它向我显示了错误“Call to undefined method WP_Post::get_shipping_class()”。

怎么了?我该如何解决?

【问题讨论】:

    标签: woocommerce product shipping


    【解决方案1】:

    我修改了你的代码。尝试关注---

    $args = array(
            'post_type'   => array('product', 'product_variation'),
            'numberposts' => -1,
            'post_status' => 'publish',
        );
        $shop_products = get_posts( $args );
        foreach ($shop_products as $item) {
            $product = wc_get_product($item->ID);
            echo $item->ID.": shipping class is -> ".$product->get_shipping_class()."<br>";
        }
    

    【讨论】:

    • 嗨爱丽丝。 :-( 不幸的是,您的解决方案对我不起作用。我使用 Wordpress 4.7.4 和 Woocommerce 2.6.14。我也尝试过:foreach ($shop_products as $item) { $product = new WC_Product($item- >ID); echo $item->ID.": shipping class is -> ".$product->get_shipping_class()."
      "; } 但它也不起作用。
    • 好的。我已经解决了。这是我的错,因为我传递了“$item->ID”而不是“$item->ID”。您的解决方案有效。谢谢爱丽丝。 :-)
    猜你喜欢
    • 2019-12-18
    • 2015-01-03
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多