最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wordpress - Change initial selection filter query with hook - Stack Overflow

matteradmin4PV0评论

I am use Woocomerce with filter YITH WooCommerce Ajax Product Filter. I need create separate page OnSale. On page displayed only goods with discount and filter should apply this by default. I tried different solutions:

  • Plugin - OnSale Page for WooCommerce
  • Edit functions.php wiht Hook:
function action_woocommerce_product_query( $q ) {
    if ( is_admin() ) return;
    if ( isset( $_GET['onsale'] ) ) {
        if ( $_GET['onsale'] == 1 ) {
            $product_ids_on_sale = wc_get_product_ids_on_sale();
            $q->set( 'post__in', $product_ids_on_sale );
        }
    }
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 1 );
  • I drew the page in Elementor with Wigdet DataSource - Sale... Work but slow

Main problem - when filtering, initial selection is reset and filter displays all products that match filter rule. I read and thought a lot. I realized that initial selection values ​​need to be set in filter settings. I dug into plugin code and found hook - yith_wcan_query_vars_to_merge.

/**
* APPLY_FILTERS: yith_wcan_query_vars_to_merge
*
* Filters list of query vars to add to the query. - Фильтрует список переменных запроса, которые можно добавить в запрос.
*
* @param array $query_vars List of query vars
*
* @return array
*/
    $query_vars = apply_filters( 'yith_wcan_query_vars_to_merge', $this->get_query_vars() );

Can someone please tell me how to use it correctly? Set constant selection...

function XXX( $this->get_query_vars ) {
    $this->query_vars = array( ??? )
};
add_filter('yith_wcan_query_vars_to_merge', 'XXX');

What should be add to archive? In what format?

Post a comment

comment list (0)

  1. No comments so far