Woocommerce – list all prices

I’m trying to write some php to display a list of all current prices (including variations) in Woocommerce.

I’m very new to php so I’m not too sure what I’m doing.

Read More

I’ve been using this code to output a list of all page and product permalinks:

<?php

include "wp-load.php";

$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
/*
global $wpdb;
$posts = $wpdb->get_results("
    SELECT ID,post_type,post_title
    FROM {$wpdb->posts}
    WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item')
");
*/

header('Content-type:text/plain');
foreach($posts as $post) {
    switch ($post->post_type) {
        case 'revision':
        case 'nav_menu_item':
            break;
        case 'page':
            $permalink = get_page_link($post->ID);
            break;
        case 'post':
            $permalink = get_permalink($post->ID);
            break;
        case 'attachment':
            $permalink = get_attachment_link($post->ID);
            break;
        default:
            $permalink = get_post_permalink($post->ID);
            break;
    }
    echo "n{$post->post_type}t{$permalink}t{$post->post_title}";
}

Which works very well. But I’ve tried to adapt it like this:

<?php

include "wp-load.php";

$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
/*
global $wpdb;
$posts = $wpdb->get_results("
    SELECT ID,post_type,post_title
    FROM {$wpdb->posts}
    WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item')
");
*/

header('Content-type:text/plain');
foreach($posts as $post) {
    switch ($post->post_type) {
        case 'revision':
    global $product;
        return $name.' '.$product->get_price_html();

    }
    echo "n{$post->post_type}t{$permalink}t{$post->get_price_html}";
}

But this is obviously not working. It just outputs a list of post types.

Can anyone help me adapt this to a script that will output a list of product names with their associated prices, including variations?? Many thanks in advance, I’m off to study php!

Related posts

Leave a Reply

1 comment

  1. Here is what you need:

    <ul>
        <?php
        foreach (get_posts('post_type=product&post_status=publish') as $product) {
            $product = get_product($product->ID);
            if ($product->product_type == "variation")
                continue;
    
            if ($product->product_type = "variable") {
                ?><li><?php echo $product->post->post_title . ": " . $product->get_price_html(); ?>
                    <ul><?php foreach($product->get_children() as $child) { ?>
                        <li><?php $child_product = $product->get_child($child->variation_id); 
                                  echo $child_product->post->post_title . ": " . $product->get_price_html(); ?>
                        </li><?php } ?>
                    </ul>
                </li><?php
            } else {
                ?><li><?php
            }
        }
        ?>
    </ul>