【问题标题】:Get all products from english version - WooCommerce & WPML获取英文版的所有产品 - WooCommerce & WPML
【发布时间】:2014-10-17 12:37:19
【问题描述】:

我正在开发一个与 WordPress 数据库配合使用的 Web 应用程序。在我的网站(与 WooCommerce 和 WPML 一起使用)中,我有两种语言的产品 - 英语(第 1 种)、塞尔维亚语(第 2 种)。我编写了一个 SQL 代码,从数据库中获取产品,包括它们的常规价格和销售价格等,但我只想获取 lang = en 的产品(英文版的产品)。问题是我不知道如何获得它们。

简单的 SQL:

$sql = 'SELECT * from `wp_posts` WHERE post_type = product';


这是从数据库中选择的方法:

// SELECT
public function select (
            $table_1 = ' wp_posts ', 
            $table_2 = ' wp_postmeta ', 
            $rows = ' t1.id, t1.post_title, guid, post_type ', 
            $where = ' t1.post_status = "publish" AND t1.post_type = "product" OR t1.post_type = "product_variation" ', 
            $groupby = ' t1.ID, t1.post_title '
        ) {

    // Connect to database and set charset
    $this->connect();
    $this->conn->set_charset('utf8');


    // Published products
    $sql = "SELECT $rows,
            max(case when meta_key = '_regular_price' then t2.meta_value end) AS price, 
            max(case when meta_key = '_sale_price' then t2.meta_value end) AS sale,
            max(case when meta_key = 'attribute_colors' then t2.meta_value end) AS colors,
            max(case when meta_key = 'attribute_number' then t2.meta_value end)
            FROM $table_1 AS t1
            INNER JOIN $table_2 AS t2 ON ( t1.ID = t2.post_id ) 
            WHERE $where
            GROUP BY $groupby";

    $result = mysqli_query($this->conn, $sql);
    $published_products_count = mysqli_num_rows($result);



    // Trashed products
    $trashed_sql = "SELECT post_status FROM `wp_posts`
                    WHERE post_status = 'trash'";

    $trashed_result = mysqli_query($this->conn, $trashed_sql);
    $trashed_products_count = mysqli_num_rows($trashed_result);


    // If results -> show them
    if ($result) {

        printf( "\t\t" . "<p>There are <strong>%d published</strong> products and <strong>%d trashed</strong>.</p>", $published_products_count, $trashed_products_count);

    $table = '
    <table class="pure-table pure-table-bordered">
        <thead>
            <tr>
                <th>Row. №</th>
                <th>ID</th>
                <th>Product</th>
                <th>Regular price</th>
                <th>Sale price</th>
                <th>Type</th>
                <th>Edit</th>
            </tr>
        </thead>';

        $row_number = 1;

        while ($row = $result->fetch_assoc()) {

            $table .= '
            <tr>
                <td>' . $row_number++ . '</td>
                <td>' . $row["id"] . '</td>
                <td><a href="http://www.enroll.rs/wp-admin/post.php?post=' . $row["id"] . '&action=edit" target="_blank">' . $row["post_title"] . '</a></td>
                <td class="price">' . $row["price"] . '</td>
                <td class="sale">' . $row["sale"] . '</td>
                <td>' . $row["post_type"] . '</td>
                <td><a href="edit_form.php?id=' . $row["id"] . '">Edit</a></td>
            </tr>';
        }

    $table .= '
    </table>';

        echo $table;
    }

    // If no results
    else {
        echo 'There isn't any products';
    }
}

希望有人帮助我!

提前致谢! :)

附:该应用程序不是基于 WordPress!

【问题讨论】:

  • 你试过 $sql = 'SELECT * from wp_posts WHERE post_type = product' AND language = 'en';或者您也可以尝试 $sql = 'SELECT * from wp_posts WHERE language IN('en') 而不查看示例输出和表结构。
  • 不,我从未尝试过使用 language = 'en',因为在 wp_posts 表中没有名为 'language' 的列。
  • 你能展示一下你的表结构吗?
  • 我刚刚编辑了帖子。

标签: php mysql woocommerce wpml


【解决方案1】:

WPML 插件创建了几个新表来管理多语言,而不影响标准的 WP 帖子表。具体来说,它会创建wp_icl_translations 表,用于存储帖子和语言之间的关系。

您需要在查询中添加两个新的 JOIN 语句:

JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type = 'post_product' JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1

然后,您可以像这样向 WHERE 语句添加新条件:

AND t.language_code='en'

希望这会有所帮助。 问候。

【讨论】:

    猜你喜欢
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2015-06-12
    • 2019-09-03
    • 2017-04-01
    相关资源
    最近更新 更多