繁体   English   中英

编辑 SQL 查询以仅包含由变量设置的术语中的产品

[英]Edit SQL query to only include products from a term set by a variable

各位程序员朋友们好

我正在使用 Enovathemes 产品搜索插件,但需要对我遇到砖墙的查询进行小的编辑。

问题如何编辑 sql 查询,以便它根据自定义字段的值设置的自定义分类术语过滤产品?

摘要: B2B 系统根据用户个人资料中的自定义字段值限制用户看到的产品,该值等于自定义分类术语。 因此,我需要将搜索结果限制为仅与他们的个人资料中的自定义字段值匹配的术语中的产品。

这就是我在显示产品时获取用户配置文件上的自定义字段值并将其与查询中的自定义分类术语匹配的方式。

$current_user = wp_get_current_user();
$country = get_the_author_meta( 'country', $current_user->ID );
$term = get_term_by('slug', $country, 'product_country');

我试过什么:

我试图在 sql 查询中指定术语(通过 $country),但这不是我的强项,在阅读了代码和大量教程后我放弃了。 现在我有编码器块。

我已经构建了一个 WP_Query,它可以完美运行,但不在 sql 查询中。

这是我正在使用的 Enovathemes 代码。 我已经定制了它以满足我的需求,但基本原理是相同的。

 function search_product() {

        global $wpdb, $woocommerce;

        if (isset($_POST['keyword']) && !empty($_POST['keyword'])) {

            $keyword = $_POST['keyword'];

            if (isset($_POST['category']) && !empty($_POST['category'])) {

                $category = $_POST['category'];

                $querystr = "SELECT DISTINCT * FROM $wpdb->posts AS p
                LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
                INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
                INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
                WHERE p.post_type IN ('product')
                AND p.post_status = 'publish'
                AND x.taxonomy = 'product_cat'
                AND (
                    (x.term_id = {$category})
                    OR
                    (x.parent = {$category})
                )
                AND (
                    (p.ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value LIKE '%{$keyword}%'))
                    OR
                    (p.post_content LIKE '%{$keyword}%')
                    OR
                    (p.post_title LIKE '%{$keyword}%')
                )
                ORDER BY t.name ASC, p.post_date DESC;";

            } else {
                $querystr = "SELECT DISTINCT $wpdb->posts.*
                FROM $wpdb->posts, $wpdb->postmeta
                WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
                AND (
                    ($wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value LIKE '%{$keyword}%')
                    OR
                    ($wpdb->posts.post_content LIKE '%{$keyword}%')
                    OR
                    ($wpdb->posts.post_title LIKE '%{$keyword}%')
                )
                AND $wpdb->posts.post_status = 'publish'
                AND $wpdb->posts.post_type = 'product'
                ORDER BY $wpdb->posts.post_date DESC";
            }

            $query_results = $wpdb->get_results($querystr);

            if (!empty($query_results)) {

                $output = '';

                foreach ($query_results as $result) {

                    $price      = get_post_meta($result->ID,'_regular_price');
                    $price_sale = get_post_meta($result->ID,'_sale_price');
                    $currency   = get_woocommerce_currency_symbol();
                    $sku   = get_post_meta($result->ID,'_sku');

                    $output .= '<li>';
                        $output .= '<a href="'.get_post_permalink($result->ID).'">';
                            $output .= '<div class="product-image">';
                                $output .= '<img src="'.esc_url(get_the_post_thumbnail_url($result->ID,'thumbnail')).'">';
                            $output .= '</div>';
                            $output .= '<div class="product-data">';
                                $output .= '<div class="shado-12 product-title"><strong>Title:</strong> '.$result->post_title.'</div>';
                                if (!empty($sku)) {
                                    $output .= '<div class="shado-12 product-sku"><strong>SKU:</strong> '.$sku[0].'</div>';
                                }
                                if (!empty($price)) {
                                    $output .= '<div class="shado-12 product-price"><strong>Price:</strong> ';
                                        $output .= $currency;
                                        $output .= '<span class="regular-price">'.$price[0].'</span>';
                                        if (!empty($price_sale)) {
                                            $output .= '<span class="sale-price">'.$price_sale[0].'</span>';
                                        }
                                    $output .= '</div>';
                                }

                            $output .= '</div>';
                            $output .= '</a>';
                    $output .= '</li>';
                }

                if (!empty($output)) {
                    echo $output;
                }
            }
        }

        die();
    }
    add_action( 'wp_ajax_search_product', 'search_product' );
    add_action( 'wp_ajax_nopriv_search_product', 'search_product' );

万一有类似问题的人到达我的帖子。 我能够通过将 .slug 添加到查询并使用变量作为它的值来解决这个问题 - 完美地工作。

if (true) {

$current_user = wp_get_current_user();
$country = get_the_author_meta( 'country', $current_user->ID );

$querystr = "SELECT DISTINCT * FROM $wpdb->posts AS p
LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
WHERE p.post_type IN ('product')
AND p.post_status = 'publish'
AND x.taxonomy = 'product_country'
AND t.slug = '$country'
AND (
    (p.ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value LIKE '%{$keyword}%'))
    OR
    (p.post_content LIKE '%{$keyword}%')
    OR
    (p.post_title LIKE '%{$keyword}%')
)
ORDER BY t.name ASC, p.post_date DESC;";

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM