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.
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!
Here is what you need: