How to add meta query and modify WooCommerce product using all import plugin function

How to add meta query and modify WooCommerce product using all import plugin function

Dec 5, 2018 | WordPress

In this tutorial, I have write code to add custom meta query in main query on archive product page using WooCommerce all import plugin functions.

If you are using paid All Import WooCommerce plugin with meta field option then you can use below code to add meta query custom filter.

How to use All Import plugin hook : pmxi_saved_post

Description
This hook is called after WP All Import creates or updates a post. It is generally used if you need to perform further actions on the imported data, like serialize it, use an API for geocoding coordinates, use it for some other purpose like comment generation, etc.

Here is link to add custom function in all import plugin’s import.

Use below code in custom PHP function editor of all import plugin.

add_action('pmxi_saved_post', 'post_saved', 10, 1);

function post_saved($id){
  $x = get_post_meta($id, 'woo_linked', true);

     $args = array(
       'post_type' => 'product',
       'meta_query' => array(
           array(
               'key' => 'woo_linked',
               'value' => $x
           ),
           array(
                'key' => '_visibility',
                'value' => 'show',
           )
       ),
       'fields' => 'ids'
     );
     // perform the query
     $vid_query = new WP_Query( $args );
     $vid_ids = $vid_query->posts;

     // do something if the meta-key-value-pair exists in another post
     if ( !empty( $vid_ids ) ) {
         update_post_meta($id, '_visibility', 'hidden');
     }else{
        update_post_meta($id, '_visibility', 'show');
     }
}

How to change what products show up in the shop loop… or any other of the shop archives ?

WooCommerce builds a custom query for products in its WC_Query class by hooking into the classic pre_get_posts hook and changing WordPress’s query parameters to get the desired products.

Pre_get_posts filter causes menu items to disappear- We added if condition to take care of menu items, it only hits the page’s main query.

Now add below meta query filter code in your theme functions.php file and modify the WooCommerce Product Query.

function av_featured_show( $query ) {
  if( $query->is_main_query() && is_category() ) {
    $meta_query = $query->get('meta_query');
    $meta_query[] = array(
      'key' => '_visibility',
      'value' => 'show',
      'compare' => '=',
    );
    $query->set('meta_query', $meta_query);
  }
 
}
add_action( 'pre_get_posts', 'av_featured_show' );

 

Thank you for being here, Please share your feedback in below comment section.

Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

1 Comment

  1. AffiliateLabz

    Great content! Super high-quality! Keep it up! 🙂

    Reply

Leave a Reply