WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Searching product-level attributes in WooCommerce

[Resolved] Searching product-level attributes in WooCommerce

  • WooCommerce allows two types of product attributes: global attributes and product-level attributes. Global attributes are a taxonomy, and product-level attributes are post meta. I’ve set up Relevanssi to search global attributes, but can’t get it to search product-level attributes. I think this is because product-level attributes aren’t post meta belonging to posts of type “product.” They belong to posts of type “product_variation,” which aren’t public.

    Lots of shops use product-level attributes and it would be fantastic to be able to search them — I would suggest returning the parent product when a particular product_variation is found to contain the search term. More details below:

    Product-level attributes get saved in a post meta field called attribute_[attribute name], for example, attribute_color. When these attributes are used to create product variations, this post meta field belongs to a CPT of type product_variation. Relevanssi can index this field and I can see the values in the Relevanssi table in the database (with a 1 in the customfield column), but no results get returned when I search for a word that appears only in a product-level attribute. For example, I have a color called ‘aquamarine’ and I can see it in the Relevanssi table, but no products get returned when I search using that word.

    product_variation is registered as follows:

    register_post_type( "product_variation",
    			apply_filters( 'woocommerce_register_post_type_product_variation',
    				array(
    					'label'        => __( 'Variations', 'woocommerce' ),
    					'public'       => false,
    					'hierarchical' => false,
    					'supports'     => false
    				)
    			)
    		);

    I’ve hooked into the filter to make public = true and explicitly set exclude_from_search = false, but it doesn’t help.

    Is there any hope of Relevanssi returning products that have variations that match the search term?

    https://wordpress.org/plugins/relevanssi/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    @msaari

    You can, for example, add code that adds the variation meta fields to the parent post.

    add_filter('relevanssi_content_to_index', 'rlv_add_variations', 10, 2);
    function rlv_add_variations($content, $post) {
        $variations = get_children(array('post_parent' => $post->ID, 'post_type' => 'product_variation'));
        if (!empty($variations)) {
            foreach ($variations as $variation) {
                $color = get_post_meta($variation->ID, 'attribute_color', true);
                $content .= " $color";
            }
        }
        return $content;
    }

    Something like that.

    Oh, that’s clever! It worked perfectly.

    This is my first time using Relevanssi and I’m really enjoying the control it gives me over indexing and search results. Thanks for a great plugin and great support!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Searching product-level attributes in WooCommerce’ is closed to new replies.