Woocommerce: get a list for all sku product

怎甘沉沦 提交于 2019-12-11 03:59:17

问题


I would to get in a <li> list all sku there are in an ecommerce. I belive that I should use foreach cycle, but I don't know how to recall the selection of all products.

Thanks for all reply, and sorry for my bad english. Greatings


回答1:


Please try this. Let me know if this works perfectly ;)

$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
query_posts( $args );
if( have_posts() ):
    echo '<ul>';
    while ( have_posts() ) : the_post();
        echo '<li>'. $product->get_sku() . '</li>';
    endwhile;
    echo '</ul>';
endif;

Cheers!




回答2:


Never, ever use query_posts. It is only for the main query loop and should never be modified. There are many, many other ways of grabbing the posts you want. For example:

$args = array(
    'post_type' => 'product', 
    'posts_per_page' => -1
);

$wcProductsArray = get_posts($args);

if (count($wcProductsArray)) {
    foreach ($wcProductsArray as $productPost) {
        $productSKU = get_post_meta($productPost->ID, '_sku', true);
        $productTitle = get_the_title($productPost->ID);

        echo '<li>' . $productSKU . '</li>';
    }
}

It's worth noting that this method will only fetch simple products, and will not include product variations. If you want to grab those too, you'll need to update the $args to 'post_type' => array('product', 'product_variation').



来源:https://stackoverflow.com/questions/21858431/woocommerce-get-a-list-for-all-sku-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!