Search code examples
phpwordpresswoocommerceshortcodehook-woocommerce

Change product meta query on Woocommerce shortcodes


I am working on a woocommerce store, In which I have to show products on location custom value. A product belongs to any of the location. I have completed the plugin almost 50%, products on shop page are getting filter perfectly but feature products, sale products, recent product etc. are not getting filter because they are generation from woocommerce products shortcodes.

Till now I added a custom filed in admin product page, and showing filter products on frontend on shop page. Now I want to filter products from shortcode.

I added the custom filed in product information page at admin site below is the code:

/* adding custom product field: location */ 
function add_custom_product_text_filed(){
global $woocommerce, $post;

$locatons = array('all' => 'All');
$locations_posts = get_posts(array('post_type' => 'location', 'numberposts' => -1));
foreach ($locations_posts as $loc_post) {
    $locatons[$loc_post->ID] = $loc_post->post_title;
}

echo '<div class="options_group">';

woocommerce_wp_select(
    array(
        'id' => '_location',
        'label' => __('Location'),
        'desc_tip' => true,
        'description'   => __('Enter the product location here'),
        'options' => $locatons,
    )
);
echo '</div>';
}

And the following is code of showing products based on location

/*Get Location Based Products*/
add_action( 'woocommerce_product_query', 'location_products' );
function location_products($q){
if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
    $meta_query = $q->get('meta_query');
    $meta_query[] = array(
        'key' => '_location',
        'value' => $_COOKIE['wc_location_product_id'],
        'compare' => '=',
    );
    $q->set('meta_query', $meta_query);
}
}

Now I want to change working of woocommerce products shortcode (sale products, feature products, recent products etc.) based on location custom filed because page bulider use shortcode to show products, but I don't get the way how I can change the way of shortcode functionality. Is there any hook or filter to complete this task, or any example that give an idea how to do this. Your kind help is appreciated. Thank you.


Solution

  • The following code using woocommerce_shortcode_products_query dedicated filter hook allow to change the product query on Woocommerce shortcodes:

    add_filter( 'woocommerce_shortcode_products_query', 'shortcode_products_query_on_location', 10, 3 );
    function shortcode_products_query_on_location( $query_args, $atts, $loop_name ){
        if (isset($_COOKIE['wc_location_product_id']) && !empty($_COOKIE['wc_location_product_id']) && !is_null($_COOKIE['wc_location_product_id']) && $_COOKIE['wc_location_product_id'] != 'all') {
    
            $query_args['meta_query'] = array( array(
                'key'     => '_location',
                'value'   => $_COOKIE['wc_location_product_id'],
                'compare' => '=',
            ) );
        }
        return $query_args;
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should work.