Conversation
supportI would like to test two versions of a page hero section on Woo category pages. I use an advanced custom field for the content that is rendered like so: function archive_header() { if (is_product_category()) { $cat = get_queried_object(); $custom_field = get_field('product_category_header', $cat); echo '<div class="fullwidth-banner cat-banner">' . $custom_field . '</div>'; do_action('woo_cat_rating', $cat); if (in_array($cat->slug, array('schimmelentferner', 'spruehgeraete'))) { echo '<p style="text-align: center;"><a class="button scroll-element" id="video" href="#produktvideo">Anwendungsvideo</a></p>'; } } } add_action('woocommerce_before_shop_loop', 'archive_header', 10); I could put the content in the function and use a PHP test. Can I somehow run a test by just changing the content of the custom field (WYSIWYG-Editor)? That would be much easier. Thank you. This topic was modified 1 month, 1 week ago by cutu234 .
You’d still need a PHP test to change the value of the field, right?
That’s exactly where I’m struggling. I’m not sure whether it would even be possible to create a test that changes the content of the custom field. I tried the custom post type test, but the custom fields are not available in the drop down. Just to clarify the setup: I created a custom field (full editor) for Woocommerce categories. This is on the ACF side. My snippets provides the rendering on the category page.
I’m not sure whether it would even be possible to create a test that changes the content of the custom field. I tried the custom post type test, but the custom fields are not available in the drop down. Yeah, no, I don’t think that’d be possible with the current set of tests. You’ll have to create a PHP test whether you like it or not. I think the only solution would be to create a PHP test that somehow looks something similar to this: // Unhook original hero section: remove_action('woocommerce_before_shop_loop', 'archive_header', 10); // Create new hero section: function archive_header_b() { if ( ! is_product_category() ) { return; } $cat = get_queried_object(); // Load different field: $custom_field = get_field( 'product_category_header_b', $cat ); echo '<div class="fullwidth-banner cat-banner">' . $custom_field . '</div>'; do_action( 'woo_cat_rating', $cat ); if (in_array($cat->slug, array('schimmelentferner', 'spruehgeraete'))) { echo '<p style="text-align: center;"><a class="button scroll-element" id="video" href="#produktvideo">Anwendungsvideo</a></p>'; } } add_action('woocommerce_before_shop_loop', 'archive_header', 10); and that’ll probably do the trick.
Hi David, don’t get me wrong: I absolutely love the PHP test feature. I just wanted to make sure that this would be the right approach. Now, I have a problem. I can’t save the following snippet for the variant. Not sure why: remove_action('woocommerce_before_shop_loop', 'archive_header', 10); function archive_header() { if (is_product_category()) { $cat = get_queried_object(); if ($cat->slug == 'gruenbelagentferner') { $custom_field = get_field('banner_fur_ab-test_quart', $cat); } else { $custom_field = get_field('product_category_header', $cat); } echo '<div class="fullwidth-banner cat-banner">' . $custom_field . '</div>'; do_action('woo_cat_rating', $cat); if (in_array($cat->slug, array('schimmelentferner', 'spruehgeraete'))) { echo '<p style="text-align: center;"><a class="button scroll-element" id="video" href="#produktvideo">Anwendungsvideo</a></p>'; } } } add_action('woocommerce_before_shop_loop', 'archive_header', 10); “Save and Preview” simply removes the snippet. What am I missing? In addition to that: The test scope would be return is_product_category('gruenbelagentferner'); , right? This reply was modified 1 month ago by cutu234 . This reply was modified 1 month ago by cutu234 .
I guess the problem is you’re defining a function ( archive_header ) that already exists in the global scope, which triggers an error. Rename your alternative function to something else: remove_action( 'woocommerce_before_shop_loop', 'archive_header' ); function archive_header_b_variant() { … } add_action( 'woocommerce_before_shop_loop', 'archive_header_b_variant' ); This reply was modified 1 month ago by David Aguilera .
Spot on, David. Should have thought of this myself. 🙂 Works just fine. Thanks, Mike
Glad I could help!
You’d still need a PHP test to change the value of the field, right?
That’s exactly where I’m struggling. I’m not sure whether it would even be possible to create a test that changes the content of the custom field. I tried the custom post type test, but the custom fields are not available in the drop down. Just to clarify the setup: I created a custom field (full editor) for Woocommerce categories. This is on the ACF side. My snippets provides the rendering on the category page.
I’m not sure whether it would even be possible to create a test that changes the content of the custom field. I tried the custom post type test, but the custom fields are not available in the drop down. Yeah, no, I don’t think that’d be possible with the current set of tests. You’ll have to create a PHP test whether you like it or not. I think the only solution would be to create a PHP test that somehow looks something similar to this: // Unhook original hero section: remove_action('woocommerce_before_shop_loop', 'archive_header', 10); // Create new hero section: function archive_header_b() { if ( ! is_product_category() ) { return; } $cat = get_queried_object(); // Load different field: $custom_field = get_field( 'product_category_header_b', $cat ); echo '<div class="fullwidth-banner cat-banner">' . $custom_field . '</div>'; do_action( 'woo_cat_rating', $cat ); if (in_array($cat->slug, array('schimmelentferner', 'spruehgeraete'))) { echo '<p style="text-align: center;"><a class="button scroll-element" id="video" href="#produktvideo">Anwendungsvideo</a></p>'; } } add_action('woocommerce_before_shop_loop', 'archive_header', 10); and that’ll probably do the trick.
Hi David, don’t get me wrong: I absolutely love the PHP test feature. I just wanted to make sure that this would be the right approach. Now, I have a problem. I can’t save the following snippet for the variant. Not sure why: remove_action('woocommerce_before_shop_loop', 'archive_header', 10); function archive_header() { if (is_product_category()) { $cat = get_queried_object(); if ($cat->slug == 'gruenbelagentferner') { $custom_field = get_field('banner_fur_ab-test_quart', $cat); } else { $custom_field = get_field('product_category_header', $cat); } echo '<div class="fullwidth-banner cat-banner">' . $custom_field . '</div>'; do_action('woo_cat_rating', $cat); if (in_array($cat->slug, array('schimmelentferner', 'spruehgeraete'))) { echo '<p style="text-align: center;"><a class="button scroll-element" id="video" href="#produktvideo">Anwendungsvideo</a></p>'; } } } add_action('woocommerce_before_shop_loop', 'archive_header', 10); “Save and Preview” simply removes the snippet. What am I missing? In addition to that: The test scope would be return is_product_category('gruenbelagentferner'); , right? This reply was modified 1 month ago by cutu234 . This reply was modified 1 month ago by cutu234 .
I guess the problem is you’re defining a function ( archive_header ) that already exists in the global scope, which triggers an error. Rename your alternative function to something else: remove_action( 'woocommerce_before_shop_loop', 'archive_header' ); function archive_header_b_variant() { … } add_action( 'woocommerce_before_shop_loop', 'archive_header_b_variant' ); This reply was modified 1 month ago by David Aguilera .
Spot on, David. Should have thought of this myself. 🙂 Works just fine. Thanks, Mike
Glad I could help!