本文介绍了Woocommerce - 检索运输类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use following code on external script to retrieve all products (simple and variable):

    $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>";
    }

我需要使用自己的运输类别创建产品列表,但它不起作用.它向我显示了错误调用未定义的方法 WP_Post::get_shipping_class()".

I need to create a list of product with their own shipping class, but it doesn't works.It shows me the error "Call to undefined method WP_Post::get_shipping_class()".

怎么了?我该如何解决?

What is wrong? How can I fix it?

推荐答案

我已经修改了您的代码.尝试以下---

I have modified your code. Try following ---

$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>";
    }

这篇关于Woocommerce - 检索运输类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:37