dongtuo5611 2014-10-17 12:37
浏览 21
已采纳

从英文版获得所有产品 - WooCommerce&WPML

I'm developing a web application that works with WordPress database. In my website,(which works with WooCommerce and WPML) I have products in two languages - english (1st), serbian(2nd). I wrote an SQL code that get the products from database, with their regular and sale prices and etc., but I want to get ONLY products where lang = en (the products from english version). The problem is I don't know how to get them.

Simple SQL:

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


This is the method that SELECT from database:

// 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';
    }
}

I hope somebody help me!

Thanks in advance! :)

P.S. The application is not based on WordPress!

  • 写回答

1条回答 默认 最新

  • dpziir0079 2015-03-11 12:21
    关注

    WPML plugin creates several new tables to manage multilang, not affecting the standard WP posts tables. Specifically, it creates the wp_icl_translations table, where it stores relations between posts and languages.

    You need to add two new JOIN statements to your query:

    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
    

    Then, you can add a new condition to your WHERE statement like this:

    AND t.language_code='en'
    

    Hope this helps. Regards.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题