【问题标题】:Woocommerce - list all pricesWoocommerce - 列出所有价格
【发布时间】:2013-11-20 09:35:47
【问题描述】:

我正在尝试编写一些 php 来显示 Woocommerce 中所有当前价格(包括变化)的列表。

我对 php 很陌生,所以我不太确定自己在做什么。

我一直在使用此代码输出所有页面和产品永久链接的列表:

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

效果很好。但我试着像这样调整它:

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

但这显然行不通。它只是输出一个帖子类型的列表。

谁能帮我把它改编成一个脚本,该脚本将输出产品名称列表及其相关价格,包括变体?非常感谢,我要去学习php了!

【问题讨论】:

  • 在改编代码时你想达到什么目的?
  • 你好,danyo。我想要实现的是所有 Woocommerce 产品及其价格(包括产品“变体”)的输出列表,以便我可以使用此信息为 Google PLA 创建准确的数据馈送。最终,我想创建一个脚本,以正确的格式为数据馈送提取所有信息,但这目前有点超出我的水平。第一个代码示例完美地创建了所有 URL(“永久链接”)的列表,所以我想适应包含价格也不会太难。 “可变”产品(两个价格,一个 URL)可能是这里的绊脚石

标签: php wordpress woocommerce


【解决方案1】:

这是你需要的:

<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>

【讨论】:

  • 嗨,利亚姆。对不起,我无法让它工作。我现在实际上正在使用一个插件来创建数据馈送,所以没什么大不了的。无论如何谢谢:)
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 2020-11-06
  • 2016-01-19
  • 1970-01-01
  • 2022-11-25
相关资源
最近更新 更多