【问题标题】:WooCommerce: Get SKUs of all product variationsWooCommerce:获取所有产品变体的 SKU
【发布时间】:2020-04-08 09:11:27
【问题描述】:

我有一个产品列表,应该在它自己的行中显示变化。 到目前为止,该代码运行良好。

但我不知道如何显示变体的 SKU。

这是我当前的代码:

$args = [
    'status'    => array('publish', 'draft'),
    'orderby'   => 'name',
    'order'     => 'ASC',
    'limit'     => -1,
];
$vendor_products = wc_get_products($args);

$list_array = array();

foreach ($vendor_products as $key => $product) {

    if ($product->get_type() == "variable") {

        foreach ($product->get_variation_attributes() as $variations) {
            foreach ($variations as $variation) {

                $list_array[] = array(
                    'SKU'      => $product->get_sku(),
                    'Name'     => $product->get_title() . " - " . $variation,
                );

            }
        }

    } else {

        $list_array[] = array(
            'SKU'      => $product->get_sku(),
            'Name'     => $product->get_title(),
        );


    }
}

return $list_array;

我尝试显示产品属性,还尝试获取变体的 ID。 什么都不适合我?!

有没有简单的方法来获取变体 SKU?

【问题讨论】:

  • wc_get_products() 仅适用于“产品”类型,不适用于 product_variation 类型
  • 啊,好的,有没有其他方法可以在我的 foreach 循环中访问 SKU?
  • 所以您想从可变产品中获取所有变体 sku,因为您正在查询所有“产品”postype?
  • 是的,有点。我需要变体的 SKU,而不是来自父产品的 SKU。我已经在标题中添加了变体,但无法获得任何其他内容,例如 ID 或 SKU

标签: php wordpress woocommerce sku product-variations


【解决方案1】:

您最好使用WC_Product_Variable get_children() 方法,例如:

$args = [
    'status'    => array('publish', 'draft'),
    'orderby'   => 'name',
    'order'     => 'ASC',
    'limit'     => -1,
];
$vendor_products = wc_get_products($args);

$list_array = array();

foreach ($vendor_products as $key => $product) {

    if ( $product->is_type( "variable" ) ) {

        foreach ( $product->get_children( false ) as $child_id ) {
            // get an instance of the WC_Variation_product Object
            $variation = wc_get_product( $child_id ); 

            if ( ! $variation || ! $variation->exists() ) {
                continue;
            }

            $list_array[] = array(
                'SKU'      => $variation->get_sku(),
                'Name'     => $product->get_name() . " - " . $child_id,
            );
        }

    } else {

        $list_array[] = array(
            'SKU'      => $product->get_sku(),
            'Name'     => $product->get_name(),
        );

    }

}

return $list_array;

甚至还有一些其他可用的方法,例如get_available_variations() (在查看其源代码时使用get_children() 方法)。它应该更好地工作......


定位具有不同于“publish"”的其他帖子状态的可变产品的产品变体。

在这种情况下,您应该使用自定义 WP_Query 替换 get_children() 方法,该方法将处理其他帖子状态,如下所示:

$statuses = array('publish', 'draft');

// Args on the main query for WC_Product_Query
$args = [
    'status'    => $statuses,
    'orderby'   => 'name',
    'order'     => 'ASC',
    'limit'     => -1,
];

$vendor_products = wc_get_products($args);

$list_array = array();

foreach ($vendor_products as $key => $product) {

    if ($product->get_type() == "variable") {

        // Args on product variations query for a variable product using a WP_Query
        $args2 = array( 
            'post_parent' => $product->get_id(), 
            'post_type'   => 'product_variation', 
            'orderby'     => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ), 
            'fields'      => 'ids', 
            'post_status' => $statuses, 
            'numberposts' => -1, 
        ); 

        foreach ( get_posts( $args2 ) as $child_id ) {
            // get an instance of the WC_Variation_product Object
            $variation = wc_get_product( $child_id ); 

            if ( ! $variation || ! $variation->exists() ) {
                continue;
            }

            $list_array[] = array(
                'SKU'      => $variation->get_sku(),
                'Name'     => $product->get_name() . " - " . $child_id,
            );
        }

    } else {

        $list_array[] = array(
            'SKU'      => $product->get_sku(),
            'Name'     => $product->get_name(),
        );

    }

}

return $list_array;

这次您将获得可变产品的所有产品变体。

供参考:Product -> Get Children doesn't return all variations #13469

【讨论】:

  • 谢谢!这很好用。我尝试与get_available_variations() 合作,但不知道该怎么做... ;)
  • 嗯,不,还有一个问题。使用您的代码,输出中不会包含所有产品。有什么限制吗?只喜欢已发布的产品或其他东西?
  • 是的,可能是......所以在你的情况下,你应该更好地使用 WP 查询,而不是同时使用帖子类型 product 和 product_variation... WP_Query 比 WC_Query 灵活得多,WC_Query 只允许很少的参数和产品帖子类型。
  • 啊,好吧,我不知道该怎么做 :-) 但我发现,产品没有变体(存在问题),如果变体不活跃,它可能会发生:@ 987654324@ 所以我想我可以使用上面的代码。
  • @Cray 我已经根据你提到的内容更新了我的答案……如果你愿意,试试吧。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 2020-10-17
  • 2022-06-13
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多