woocommerce / woocommerce-paypal-payments

https://wordpress.org/plugins/woocommerce-paypal-payments/
GNU General Public License v2.0
62 stars 47 forks source link

Line totals are occasionally missing from PPC Simulate Cart requests (3524) #2493

Open jimjasson opened 1 month ago

jimjasson commented 1 month ago

Describe the Bug

When PayPal is active on a store and Smart Buttons are enabled for the Single Product page and the Cart, then two AJAX requests run when viewing the single product page of a Simple Product:

Markup on 2024-08-07 at 13:58:04

If someone hooks into the woocommerce_cart_shipping_packages hook and inspect the cart items there, they would notice that in one of the requests the line totals are set and in another request they are not set, causing an inconsistency:

1/2 requests (missing line totals):

Markup on 2024-08-07 at 14:01:29

2/2 requests:

Markup on 2024-08-07 at 14:01:45

To Reproduce

  1. Enable only WooCommerce and PayPal Payments.
  2. Enable Smart buttons for the Single Product page and the Cart.
  3. Install this snippet:
add_filter( 'woocommerce_cart_shipping_packages', 'test_paypal_add_to_cart' );

function test_paypal_add_to_cart( $packages ) {

    $cart = WC()->cart->cart_contents;

    foreach( $cart as $cart_item ) {
        $logger = wc_get_logger();

        $logger->log( 'info', $cart_item , array( 'source' => 'test_paypal_simple' ) );
    }
    return $packages;
}
  1. Create a Simple Product.
  2. Open an incognito window.
  3. View the Simple Product.
  4. View the logs and check that the line totals are missing from some requests.

Expected Behavior

When woocommerce_cart_shipping_packages all calculations must have been completed and cart items should consistently have all line totals.

Actual Behavior

In some requests, the line totals are missing from the cart items.

Environment

Clean WordPress install with only WooCommerce and PayPal Payments active.

jimjasson commented 1 month ago

Possible workaround: Turn off the cart simulation: https://github.com/woocommerce/woocommerce-paypal-payments/wiki/Actions-and-Filters#modify-cart-simulation-behavior.