Skip to content

Commit

Permalink
Fix Mix and Match Products on product WooPay Express Checkout Button (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alefesouza authored Oct 4, 2023
1 parent 5448262 commit c001813
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-add-woopay-woocommerce-mix-and-match-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Add Mix and Match Products support on WooPay.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const useExpressCheckoutProductHandler = ( api, isProductPage = false ) => {

let data = {
product_id: productId,
qty: document.querySelector( '.quantity .qty' ).value,
quantity: document.querySelector( '.quantity .qty' ).value,
};

if ( variation && ! bundleForm ) {
Expand Down Expand Up @@ -169,6 +169,7 @@ const useExpressCheckoutProductHandler = ( api, isProductPage = false ) => {
};

const bundleForm = document.querySelector( '.bundle_form' );
const mixAndMatchForm = document.querySelector( '.mnm_form' );
const variationForm = document.querySelector( '.variations_form' );

if ( bundleForm ) {
Expand All @@ -179,6 +180,14 @@ const useExpressCheckoutProductHandler = ( api, isProductPage = false ) => {
'woocommerce-product-bundle-hide',
disableAddToCartButton
);
} else if ( mixAndMatchForm ) {
// eslint-disable-next-line no-undef
jQuery( mixAndMatchForm )
.on(
'wc-mnm-display-add-to-cart-button',
enableAddToCartButton
)
.on( 'wc-mnm-hide-add-to-cart-button', disableAddToCartButton );
} else if ( variationForm ) {
// eslint-disable-next-line no-undef
jQuery( variationForm )
Expand All @@ -198,6 +207,17 @@ const useExpressCheckoutProductHandler = ( api, isProductPage = false ) => {
'woocommerce-product-bundle-hide',
disableAddToCartButton
);
} else if ( mixAndMatchForm ) {
// eslint-disable-next-line no-undef
jQuery( mixAndMatchForm )
.off(
'wc-mnm-display-add-to-cart-button',
enableAddToCartButton
)
.off(
'wc-mnm-hide-add-to-cart-button',
disableAddToCartButton
);
} else if ( variationForm ) {
// eslint-disable-next-line no-undef
jQuery( variationForm )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export const WoopayExpressCheckoutButton = ( {
if ( isAddToCartDisabled ) {
alert(
window.wc_add_to_cart_variation_params
.i18n_make_a_selection_text
?.i18n_make_a_selection_text ||
__(
'Please select all required options to continue.',
'woocommerce-payments'
)
);
return;
}
Expand Down
10 changes: 5 additions & 5 deletions includes/class-wc-payments-woopay-button-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ public function ajax_add_to_cart() {
WC()->shipping->reset_shipping();

$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : false;
$qty = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] );
$quantity = ! isset( $_POST['quantity'] ) ? 1 : absint( $_POST['quantity'] );
$product = wc_get_product( $product_id );
$product_type = $product->get_type();

// First empty the cart to prevent wrong calculation.
WC()->cart->empty_cart();

$is_add_to_cart_valid = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $qty );
$is_add_to_cart_valid = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

if ( ! $is_add_to_cart_valid ) {
// Some extensions error messages needs to be
Expand All @@ -245,11 +245,11 @@ public function ajax_add_to_cart() {
$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation( $product, $attributes );

WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
WC()->cart->add_to_cart( $product->get_id(), $quantity, $variation_id, $attributes );
}

if ( 'simple' === $product_type || 'subscription' === $product_type || 'bundle' === $product_type ) {
WC()->cart->add_to_cart( $product->get_id(), $qty );
if ( in_array( $product_type, [ 'simple', 'subscription', 'bundle', 'mix-and-match' ], true ) ) {
WC()->cart->add_to_cart( $product->get_id(), $quantity );
}

WC()->cart->calculate_totals();
Expand Down

0 comments on commit c001813

Please sign in to comment.