Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › CSS classes based on Woo product type in the result box
- This topic has 4 replies, 2 voices, and was last updated 2 years ago by
Monolab.
-
AuthorPosts
-
May 27, 2024 at 12:40 pm #48309
Monolab
ParticipantDear 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,
DanielMay 27, 2024 at 1:07 pm #48312Ernest Marcinko
KeymasterHi,
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.
May 28, 2024 at 5:33 pm #48332Monolab
ParticipantIs 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,
DanielMay 29, 2024 at 8:49 am #48334Ernest Marcinko
KeymasterYes, 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_typefunction can be stubbed with post meta check for the product type, then it might be a bit more efficient, as thewc_get_productis 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.May 29, 2024 at 6:28 pm #48341Monolab
ParticipantThanks, dear Ernest, I’ll try this way!
-
AuthorPosts
- You must be logged in to reply to this topic.