• Hi,

    I’v just created a PHP test for testing the new (and completely useless) EU GRAN label. It’s really a great idea to have this gigantic image right under the purchase button.

    First, I tried this:

    add_action( 'woocommerce_review_order_after_submit', function () {
    	echo '<img src="' . esc_url( get_stylesheet_directory_uri() . '/img/gewaehrleistungslabel_de.svg' ) . '" alt="EU-Hinweis zur gesetzlichen Gewährleistung" style="display:block;width:100%;max-width:520px;height:auto;margin-top:2em">';
    } );

    That didn’t work.

    This, on the other hand, did work:

    add_action( 'woocommerce_after_checkout_form', function () {
    	$img = '<img id="gw-label" src="' . esc_url( get_stylesheet_directory_uri() . '/img/gewaehrleistungslabel_de.svg' ) . '" alt="EU-Hinweis zur gesetzlichen Gewährleistung" style="display:block;width:100%;max-width:520px;height:auto;margin-top:2em">';
    
    	echo '<script>
    	jQuery( function ( $ ) {
    		var img = ' . wp_json_encode( $img ) . ';
    		function placeLabel() {
    			if ( ! $( "#gw-label" ).length ) {
    				$( "#place_order" ).after( img );
    			}
    		}
    		$( document.body ).on( "updated_checkout", placeLabel );
    		placeLabel();
    	} );
    	</script>';
    } );

    The problem seems to be that the scope was limited to the checkout page which didn’t work well with the checkout AJAX:

    function is_test_active(): bool {
      return function_exists( 'is_checkout' ) && is_checkout() && ! is_wc_endpoint_url( 'order-received' ) && ! is_wc_endpoint_url( 'order-pay' );
    }

    Testing the checkout is a pretty common task. Maybe, you could find a better solution for this.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author David Aguilera

    (@davilera)

    What if you create a special template that will be used on the checkout page only, create another template with the GARAN label, and run a template test?

    Thread Starter cutu234

    (@cutu234)

    Unfortunately, this is harder than it looks. The “Germanized” plugin (which we absolutely need) overrides the WooCommerce template “payment.php.” So we’d have to override the override, which I’d rather avoid. It’s not even clear if that would work at all. I just tried to test it on my staging server, but I wasn’t successful. Where do I need to place the template for the test, so that I can select it for th test? I couldn’t find any information about this either here or in the documentation.

    Plugin Author David Aguilera

    (@davilera)

    Templates are usually part of your theme, so that’s where you should add them. But I understand that, in your specific setup, this may not be feasible.

    So what about a JavaScript test? A simple JS snippet that runs on the checkout page only (or wherever you want it to run) and adds the banner wherever you want. Simple and fast.

    Thread Starter cutu234

    (@cutu234)

    A JS test would probably fix the issue. Good idea. Right now, the test is running perfectly. I just thought this might be a very common problem that Nelio users could run into.

    Just a general question: Does this problem occur in most tests that are limited to the checkout page when a hard-coded template can’t be used?

    Sorry to bother you again, but I still haven’t fully understand how template tests work. I copied the Woocommerce template file /woocommerce/checkout/payment.php to my child theme, but I can’t see the template in the dropdown. How to I make templates available? In the past I used two product template files that were copied directly in the child folder with header like so:

    * Template Name: Nab-1
    * Template Post Type: product

    But how would I do this with the payment.php?

    Plugin Author David Aguilera

    (@davilera)

    Just a general question: Does this problem occur in most tests that are limited to the checkout page when a hard-coded template can’t be used?

    I honestly don’t know. I just assumed you were using a regular page with the appropriate checkout block in it, that’s why I mentioned the template test.

    It looks like WooCommerce’s payment.php template behaves differently from regular WordPress templates. Because of that, it can’t currently be tested in the same way as a standard template with a Template Test, and supporting this specific case would require some additional work.

    Thread Starter cutu234

    (@cutu234)

    Actually, I do. I don’t use Gutenberg, but the good old checkout shortcode. Not sure, whether this makes a difference. I’ve just rechecked it without the scope limited to the checkout. That works perfectly fine without additional javascript.

    My understanding is that the AJAX request doesn’t carry the nab parameter, if the scope is limited to the checkout page. So Nelio doesn’t know which variant to load in that context and falls back to the control version (or doesn’t apply the test at all).

    On the initial page load, variant B is rendered correctly and the image appears below the button for just one second. Right after that, WooCommerce fires its usual AJAX request (?wc-ajax=update_order_review) to refresh the order review and payment section. Germanized also refreshes its own order submit fragment (.wc-gzd-order-submit) in that request. The fragments returned by this AJAX call are rendered without the variant, so they replace the markup and the image disappears.

    I wouldn’t make such a big deal out of it. But testing just the checkout is a pretty common task. Expanding the scope to the cart page wouldn’t help, since we use a sidebar cart so that users can go directly to the checkout without ever visiting the cart page.

    To make it work without js I would have to remove any limits in the scope section which would use a lot of quota.

    • This reply was modified 4 days, 15 hours ago by cutu234.
    • This reply was modified 4 days, 15 hours ago by cutu234.
    • This reply was modified 4 days, 15 hours ago by cutu234.
    • This reply was modified 4 days, 15 hours ago by cutu234.
    Plugin Author David Aguilera

    (@davilera)

    Actually, I do. I don’t use Gutenberg, but the good old checkout shortcode. Not sure, whether this makes a difference.

    And doesn’t this page has a page template in it? If it does, that’s the template I was talking about that can be tested. But I’m wondering: is this page using payment.php or is payment.php only used for the checkout form itself?

    Thread Starter cutu234

    (@cutu234)

    Unfortunately, it is much more complicated. The Woocommerce template itself uses many hooks that call other templates. Some plugins, like Germanized, override templates or just a hook in a template. Yes, I could add some content to the page after the checkout shortcode. But this is very limited in terms of position and layout. In this specific case it would be almost acceptable. For other tests it just doesn’t work.

    Actually, this is great about Woocommerce. You can change almost everything without touching a template. I have thousands of custom elements, but very few modified templates. This makes updates a LOT easier.

    • This reply was modified 4 days, 12 hours ago by cutu234.
    • This reply was modified 4 days, 12 hours ago by cutu234.
    • This reply was modified 4 days, 12 hours ago by cutu234.
    • This reply was modified 4 days, 12 hours ago by cutu234.
    Plugin Author David Aguilera

    (@davilera)

    Then I think your first approach (i.e. running a PHP test) is actually a pretty good idea. I guess the only thing missing in your first attempt is identifying the hook(s) you also need so that, when the checkout page is updated via AJAX, it also contains the info you want.

    With that said, it’s worth noting that, for small/simple changes, a JavaScript test will be probably faster—JS tests don’t require any redirection to load the appropriate variant, saving some precious time.

Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.