【问题标题】:How to create a Woocommerce Product Filter with AJAX?如何使用 AJAX 创建 Woocommerce 产品过滤器?
【发布时间】:2020-06-02 08:36:23
【问题描述】:

我想使用 AJAX 为 Woocommerce 创建自己的产品过滤器,我不想使用任何插件。但是有一个问题,我不知道如何通过所选过滤器获取产品,或者我不知道我的过滤器将如何应用于产品。

例如,如果我想为评分设置一个过滤器。

HTML

<select id="filter_ratings">
<option value="5">5 star</option>
<option value="4">4 star</option>
<option value="3">3 star</option>
<option value="2">2 star</option>
<option value="1">1 star</option>
</select>

jQuery

jQuery('#filter_ratings').change(function() {
        var values = jQuery(this).val();
        jQuery.ajax({
            url: ajax_object.ajax_url, 
            method: 'POST',
            data: {opc: values, action: 'filter_product_by_ratings', dataType: "json"},
            success: function(result){
                //Should I get all the products with the current filter?
                console.log('Filter Successful: ' + result);
            },
            error:function (xhr, ajaxOptions, thrownError) {
                console.log('Filter ERROR STATUS: ' + xhr.status);
                console.log('Filter Error: ' + thrownError);
            } 
        });
    });

function.php

add_action('wp_enqueue_scripts', 'add_js_scripts'); 
function add_js_scripts(){
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
}

//call this function by ajax
function filter_product_by_ratings(){
   //what now?
   //How can I apply the current filter?
   //Should I fetch all the products by the current filter?
   //What about for other attributes like size, color, etc?
}

是否有关于如何在 Woocommerce 中按代码对产品应用过滤器的指南?

【问题讨论】:

  • 使用 ajax 过滤产品需要先用 ajax 加载产品...

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

首先,您将遇到ajax_object 为undefined 或Object not found 的错误,因为您没有正确本地化它。

查看您的 ajax url,ajax_object 和 ajax_url,在您的函数中,您将其本地化为 ajax_object 和 ajaxurl。您只是错过了 ajax URL 中的下划线,它应该是 ajax_object.ajaxurl。

有关如何正确本地化的信息,请参阅下面的链接。 https://developer.wordpress.org/reference/functions/wp_localize_script/

您的问题是如何过滤产品,您可以使用WP_Query,然后根据您的需要更改参数。例如,由于您只想获取产品,因此 post_type 应该是 product 并且其他参数将根据您的需要而有所不同,例如 orderby、ratings 等。

这是您的function.php 中缺少的代码示例。

function filter_products(){

    $select_opc = $_POST['opc']; //here is your post data, you can use its value to filter the products

    $args = array('post_type' => 'product','posts_per_page' => 12); //change this arguments depending on your needs
    $loop = new WP_Query( $args );
    $products = '';
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            $products .= wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
            $products = 'No products found';
    }
    echo $products;
    exit;

}

得到ajax的结果后,你就可以在你的前端,例如你的ajax上替换产品的内容。

//on ajax success
....
$('.products').html(result); 

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 2013-12-05
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多