0

Im having problems with wc_get_products meta query. Im trying to exclude products that have clearance or express in the SKU and the SKUs are formatted like so; (not my choice something the company decided many moons ago!)

My Club - Express - Item My Club - Clearance - Item Another Club - Standard - Item

Below are my args, please could someone tell me what's wrong with them?

    'post_status' => array('publish', 'pending', 'draft', 'future', 'private'),
    'parent' => array( 13, 14, 15 ),
    'limit' => 1000,
    'orderby' => 'id',
    'order'   => 'asc',
    'type' => array( 'simple', 'variable' ),
    'meta_query' => array(
                    'relation' => 'and',
                        array(
                            'relation'  => 'or',
                                array(
                                'key' => '_sku',
                                'value' => 'clearance',
                                'compare' => 'NOT LIKE',
                                ),
                            ),
                        array(
                            'relation' => 'or',
                                array(
                                'key' => '_sku',
                                'value' => 'fxpe',
                                'compare' => 'NOT LIKE',
                                ),
                            ),
                        )
);

I have also completed the woocommerce steps (at bottom of page) Adding Custom Parameter Support which looks like this:

function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['_sku'] ) ) {
        $query['meta_query'][] = array(
            'key' => '_sku',
            'value' => esc_attr( $query_vars['_sku'] ),
        );
    }

    return $query;
 }

 add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );

I've also tried reversing the meta query and changing the compare to like or != but nothing works or seems to affect the query.

Any help really appreciate.

Thanks

UPDATE-------------------------------

I made a major typo in the initial post, to confirm I am using wc_get_products no wp_query

I have been trying a few variations of the meta query but so far nothing is working...

Just using a single meta query doesn't seem to work either

'meta_query' => array(
                        'key' => '_sku',
                        'value' => 'express',
                        'compare' => '!=',  // also tried 'NOT LIKE'

                        ),

Becuase of the inital confusion with the query I will mark Howard E's question correct as it is for wp_query and repost witht he correct information.

blackhill24
  • 422
  • 14
  • 30
  • Does your query provide expected results when you drop the meta query? – Howard E Feb 11 '20 at 13:06
  • the correct results are returned but they include products with the 'express' and 'clearance' sku's. and yes if the meta query is removed everything works as normal – blackhill24 Feb 11 '20 at 13:50

1 Answers1

2

Try updating your meta_query. You're starting by saying "OR" for the relation of both clearance and express also.

'meta_query' => array(
    'relation' => 'AND',
    array(
        'relation'  => 'OR',
            array(
            'key' => '_sku',
            'value' => 'clearance',
            'compare' => 'NOT LIKE',
            ),
        ),
    array(
        'relation' => 'OR',
            array(
            'key' => '_sku',
            'value' => 'express',
            'compare' => 'NOT LIKE',
            ),
        ),
    )

* To deal with Updated Question *

function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['_sku'] ) ) {
        $query['meta_query'][] =  array(
        array(
                'relation'  => 'or',
                    array(
                    'key' => '_sku',
                    'value' => 'fxpe',
                    'compare' => 'NOT LIKE',
                    ),
                ),
            array(
                'relation' => 'or',
                    array(
                    'key' => '_sku',
                    'value' => 'clearance',
                    'compare' => 'NOT LIKE',
                    ),
                ),
            );
    }

    return $query;
 }

Then just use _sku in the product query with a value...

$products = wc_get_products(array(
     'post_status' => array('publish', 'pending', 'draft', 'future', 'private'),
    'parent' => array( 13, 14, 15 ),
    'limit' => 1000,
    'orderby' => 'id',
    'order'   => 'asc',
    'type' => array( 'simple', 'variable' ),
    '_sku' => true,
));

adding this filter will however filter any instance of _sku in the WC query. You may want to add additional conditionals if needed.

Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Sorry i spoke too soon! the query is still ignoring the meta query. all results are loading including what i want excluded... – blackhill24 Feb 11 '20 at 15:41
  • What are these SKU's like? – Howard E Feb 11 '20 at 15:56
  • all characters eg. "My Club - Category - Item", not the best i know but due to the powers at be it must be like this – blackhill24 Feb 11 '20 at 16:04
  • ive also tried stripping it down to just one meta query which doesnt seem to work either, all results returned including the excluded ones. – blackhill24 Feb 11 '20 at 16:05
  • did you try changing both relations to "And"? – Howard E Feb 11 '20 at 16:35
  • I have tried what you said and it made no difference but I've made a stupid error in calling this a wp_query when in fact its a wc_get_products query which does affect the meta query. I have updated the post with all the info if you're interested in taking a look? Thanks – blackhill24 Feb 12 '20 at 11:50
  • 1
    Look at the bottom of this page: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query `$products = wc_get_products( array( 'customvar' => 'somevalue' ) );` You are not using `meta_query` in yorur `wc_get_products` – Howard E Feb 13 '20 at 10:37