Skip to content

Square Gift Card field renders twice on the classic checkout when the checkout updates while the field is loading #581

Open

Description

@qasumitbagthariya

Description

On the classic (shortcode) checkout, the Square Gift Card field can render twice. The second box has no "Apply" button, so shoppers see a stray input they can't use. It happens when a single extra update_checkout fires while the page loads. Delivery-date plugins do this, for example.

This is the same timing problem as the duplicate credit card field in SQUARE-355 (PR #571). That PR guards only the credit card form and doesn't touch the gift card code, so the gift card box still duplicates on its branch.

Steps to reproduce

  1. Use a store with Square enabled and a classic checkout page (/old-checkout/ on my test site).
  2. Add this snippet as an mu-plugin. It does nothing unless the URL has ?ted_mode=onupdate:
add_action( 'wp_footer', function () {
	if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || ! isset( $_GET['ted_mode'] ) ) {
		return;
	}
	?>
	<script>
	jQuery( function ( $ ) {
		$( document.body ).one( 'updated_checkout', function () {
			$( document.body ).trigger( 'update_checkout' );
		} );
	} );
	</script>
	<?php
}, 100 );
  1. Add a product to the cart and open /old-checkout/?ted_mode=onupdate.
  2. Wait for the page to finish loading and run this in the browser console:
document.querySelectorAll('iframe[name^="gift-card"]').length

Expected

1, with a single "Have a Square Gift Card?" box and its Apply button.

Actual

2. Two gift card boxes are visible, and the second has no Apply button.

Results

URL Gift card iframes
/old-checkout/ (no flag) 1
/old-checkout/?ted_mode=onupdate, trunk (56e0f0770), 3 of 3 runs 2
Same URL on the SQUARE-355 branch (05060d835) 2

Likely cause

src/js/frontend/wc-square-gift-card.js runs init() on every updated_checkout (line 39). init() awaits initializeGiftCard(), which awaits payments.giftCard() and then giftCard.attach(...) (lines 68 and 166-175). Nothing records that a build is already in progress, so a second updated_checkout during that await starts another build and mounts a second gift card into the same wrapper. init() also registers a new update_checkout listener each time it runs. The remove flow (onRemoveGiftCard, line 278) rebuilds the card in a similar way.

PR #571 fixes the credit card form with a "build in progress" flag and a queued refresh. The gift card form could use the same approach.

Environment

  • Chrome 156 on macOS, Square sandbox
  • Square for WooCommerce 5.5.2, WooCommerce 11.1.1, WordPress 7.1.4
  • Classic checkout

Related

SQUARE-355, PR #571.

image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions