Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fatal error with PRB buttons #4042

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions includes/payment-methods/class-wc-stripe-payment-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,35 +1467,45 @@ public function ajax_add_to_cart() {
define( 'WOOCOMMERCE_CART', true );
}

WC()->shipping->reset_shipping();
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wrapped the code inside the try...catch block, but GitHub thinks I changed more stuff here.

WC()->shipping->reset_shipping();

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

// First empty the cart to prevent wrong calculation.
WC()->cart->empty_cart();
if ( ! is_a( $product, 'WC_Product' ) ) {
/* translators: 1) The product Id */
throw new Exception( sprintf( __( 'Product with the ID (%1$s) cannot be found.', 'woocommerce-gateway-stripe' ), $product_id ) );
}

if ( ( 'variable' === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {
$attributes = wc_clean( wp_unslash( $_POST['attributes'] ) );
$product_type = $product->get_type();

$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation( $product, $attributes );
// First empty the cart to prevent wrong calculation.
WC()->cart->empty_cart();

WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
} elseif ( in_array( $product_type, $this->supported_product_types(), true ) ) {
WC()->cart->add_to_cart( $product->get_id(), $qty );
}
if ( ( 'variable' === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) {
$attributes = wc_clean( wp_unslash( $_POST['attributes'] ) );

WC()->cart->calculate_totals();
$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation( $product, $attributes );

$data = [];
$data += $this->build_display_items();
$data['result'] = 'success';
WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes );
} elseif ( in_array( $product_type, $this->supported_product_types(), true ) ) {
WC()->cart->add_to_cart( $product->get_id(), $qty );
}

// @phpstan-ignore-next-line (return statement is added)
wp_send_json( $data );
WC()->cart->calculate_totals();

$data = [];
$data += $this->build_display_items();
$data['result'] = 'success';

// @phpstan-ignore-next-line (return statement is added)
wp_send_json( $data );
} catch ( Exception $e ) {
wp_send_json( [ 'error' => wp_strip_all_tags( $e->getMessage() ) ] );
}
}

/**
Expand Down
Loading