CSS classes based on Woo product type in the result box

Home Forums Product Support Forums Ajax Search Pro for WordPress Support CSS classes based on Woo product type in the result box

This topic contains 4 replies, has 2 voices, and was last updated by Monolab Monolab 1 month, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #48309
    Monolab
    Monolab
    Participant

    Dear Support,

    We use 2 different type of Woocommerce products:
    – normal products
    – external products ( https://woocommerce.com/document/managing-products/add-product/#adding-an-external-affiliate-product )

    We want to add dedicated CSS class for external products in the result box for the corresponding items.

    So far found asp_result_css_class the hook, however this would require fetching the product for each result item, since the Woo product post object is unavailable at this point.
    Is there any hook earlier where the Woo product object (WC_Product) is available and somehow we could add a CSS class or any flag that could be checked in “asp_result_css_class”?

    Thanks,
    Daniel

    #48312
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I’m afraid it is the only way. The plugin does not fetch the product item at any point, so this information is never available during the execution.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #48332
    Monolab
    Monolab
    Participant

    Is there any point where the post itself is fetched?
    I understand that the plugin does not fetch Woo products (WC_Product), but does it fetch the post object?
    (or the plugin just works from the index, completely bypass WP classes and objects?)

    Because if the post is available at any point, then we may extra the product type from the post meta (Woo stores product type there)

    Thanks,
    Daniel

    #48334
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Yes, exactly like that – but that would not help either, because the post object does not contain the metadata information. The post meta needs to be fetched either way separately.

    According to the docs, somethins like this is the way:

    add_filter('asp_result_css_class', function($css, $id, $result){
    	if (isset($result->post_type) && $result->post_type == 'product') {
    		$product = wc_get_product($id);
    		if ( $product->is_type('external') ) {
    			$css .= ' my-css-class-name';
    		}
    	}
    	return $css;
    });

    But if the is_type function can be stubbed with post meta check for the product type, then it might be a bit more efficient, as the wc_get_product is then not neccessary, saving some queries. I’m not sure how WooCommerce stores the types, but I guess it’s post metadata, so it’s probably doable.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #48341
    Monolab
    Monolab
    Participant

    Thanks, dear Ernest, I’ll try this way!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.