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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/workflows/php-code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:

permissions:
pull-requests: write
contents: read

jobs:
test:
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.3.0 - xxxx-xx-xx =
* Fix - Fixes a possible fatal error when a product added to the cart cannot be found (with Payment Request Buttons).
* Dev - Adds a new README.md file to the plugin with specific development-focused instructions.
* Add - Implements the Single Payment Element feature for the new checkout experience on the block checkout page.
* Dev - Additional replacements for payment method constant values on the backend.
Expand Down
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
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.3.0 - xxxx-xx-xx =
* Fix - Fixes a possible fatal error when a product added to the cart cannot be found (with Payment Request Buttons).
* Dev - Adds a new README.md file to the plugin with specific development-focused instructions.
* Add - Implements the Single Payment Element feature for the new checkout experience on the block checkout page.
* Dev - Additional replacements for payment method constant values on the backend.
Expand Down
Loading