PluginUs.Net - Business Tools for WooCommerce and WordPress

[realize your idea - make your dreams come true]

Support Forum

You need to log-in to create request (topic) to the support

Extension "Color 2" unable to find products for some colors

The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page please
If you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.

Hi Pablo,

A client has webshop with WooCommerce + WPML. At first we used the default Color extension to show product colors in shop filter, however we learned from the color extension that it is not compatible and implemented the"Color 2" version instead. For most part it seems to work perfectly fine, however when actually using it to filter the colors it behaves weird as it is able to find the products for some colors and many others it doesn't find any at all.

What is interesting however is that before I changed the Color to the"Color 2" extension the filter worked fine.

Kind regards,

Hello

I checked  your  site/

It looks like all your products are hidden - https://c2n.me/4bM1kf4.png

Even if I do not apply the filter, the store page is empty - https://c2n.me/4bM1mWQ.png

Please  fix it and  do a test

I disabled this  third-party plugin - https://c2n.me/4bM1qsX.png

 

Hi there,

After quite a few hours of scraping my brain I managed to work out a configuration where it works more or less fine with our webshop setup. The only issue that can't be solved is that terms can't be hidden when"empty" because"empty" would in this case be defined by the presence and content of custom price meta keys. There's also inherently an issue with this setup, which is that if we check against stock of"variations", we also check against parent products, which is what has been mentioned before in topic https://pluginus.net/support/topic/woof-filter-not-excluding-variations as"variations" here technically don't really exist as products.

To help other people that might encounter the same issues, i'll note down here my findings and how to make it work with the plugins;

  • "WooCommerce Show Single Variations by Iconic" to show variations as individual products in shop
  • "WooCommerce Price Based on Country" for prices per country
  • "WOOF - WooCommerce Products Filter" to filter the variations with respect to country based prices
  • WPML

The"woof_get_meta_query" filter filters the product query used to count terms in the filter. Products filtered here result in the WOOF filter showing less options. The"woocommerce_product_query" action then again filters the product query that passed through"woof_get_meta_query". Products filtered here have no impact on the WOOF filter other than hiding more products that WOOF filter fetched.

It is important that color2 ext is set to"OR" in"Logic of filtering", not"AND".

add_filter('woof_get_meta_query', 'woof_hide_products_from_shop');
function woof_hide_products_from_shop($meta_query)
{
    if (isset($_GET['pa_color'])) {
        $color_term = explode(',', $_GET['pa_color']);
        foreach ($color_term as &$term)
            $term = sanitize_key($term);

        // Without an unset($term), $value is still a reference to the last item
        unset($term);

        $meta_query[] = array(
            'key'       => 'attribute_pa_color',
            'value'     => $color_term,
            'compare'   => 'IN'
        );
    }

    // Some variations might be unlinked from color and thus"any color". This means
    //    the variation will be be shown for all colors filtered.
    //
    //! Not necessary because already explicitly checking against attribute_pa_color
    // $meta_query[] = array(
    //     'key'       => 'attribute_pa_color',
    //     'value'     => '',
    //     'compare'   => '!='
    // );

    // It is not possible to check against variation price regardless whether it is custom meta key or not
    //   because it will rule out the parent product as well.
    //
    // When the parent product is ruled out it's variations will be ignored as well. Meaning we can not
    //   only just ignore the parent products yet show the variations in this hook.
    //
    // Price meta keys thus have to be checked in woocommerce_product_query, not woof_get_meta_query

    return $meta_query;
};

add_action('woocommerce_product_query', 'hide_products_from_shop', 20);
function hide_products_from_shop($q)
{
    if (is_admin()) return $q;

    $meta_query = $q->get('meta_query');

    $meta_query[] = array(
        'relation' => 'AND',
    );

    // Out of stock check cant be in woof_get_meta_query because then it ignores entire
    //   parent product with all variations included, it has to be afterwards hidden in this hook
    $meta_query[] = array(
        'key'       => '_stock_status',
        'value'     => 'outofstock',
        'compare'   => 'NOT IN'
    );

    // If the plugin pricing based on country product meta is unset for the current selected shipping country zone
    //    then make sure to hide it to prevent pricing defaulting to _regular_price
    if (function_exists('WCPBC') && $current_zone = WCPBC()->current_zone) {
        $zone_id = $current_zone->get_id();
        $key = '_' . $zone_id . '_regular_price';

        $meta_query[] = array(
            'key' => $key,
            'value' => 0,
            'compare' => '!=',
            'type' => 'numeric'
        );
    }

    // Don't show products (variable and variations) with exact price 0 or 999999
    $meta_query[] = array(
        'key' => '_price',
        'value' => [0, 999999],
        'compare' => '!=',
        'type' => 'numeric'
    );

    $q->set('meta_query', $meta_query);
}

 

To make variations colors work with WPML make sure to use"Color2" as documented at https://products-filter.com/extencion/color/

 

Google keyword search phrases;

  • "WOOF WooCommerce Products Filter how to filter variations by color"
  • "WOOF WooCommerce Products Filter unable to hide variations out of stock"
  • "WOOF WooCommerce Products Filter variations out of stock still showing"
  • "WOOF WooCommerce Products Filter filter variations country based pricing"
  • "WOOF WooCommerce Products Filter filter variations how to pricing based on country"

Hello

Thank  you  for  cooperation