magento2: Guest can't use Paypal Express in cart

Preconditions (*)

  1. Magento 2.3.2
  2. Paypal Express checkout enabled
  3. Paypal In-Context mode enabled
  4. Paypal Show in cart enabled
  5. Paypal guest checkout enabled

Steps to reproduce (*)

  1. Guest user, first time on the shop (no localStorage or session)
  2. Add any product to cart
  3. Click Paypal button to start express checkout

Expected result (*)

  1. Popup to login to Paypal opens

Actual result (*)

  1. Error message To check out, please sign in with your email address. appears and checkout is not possible

The problem can also be reproduced by clearing local storege in cart and refreshing the page. The issue is here. customerData.get('cart') is empty when this code runs for guests or users with no localStorage.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

I resolved it by adding a subscriber to cart section if initial value is undefined. If it is undefined it means localstorage is probably empty and sections did not finish loading yet. In magento/module-paypal/view/frontend/web/js/in-context/button.js:

initialize: function (config, element) {
            var cart = customerData.get('cart'),
                customer = customerData.get('customer');

            this._super();
            this.renderPayPalButtons(element);
            this.declinePayment = !customer().firstname && !cart().isGuestCheckoutAllowed;

            // BEGIN CHANGE
            if (cart().isGuestCheckoutAllowed === undefined) {
                cart.subscribe(function (updatedCart) {
                    this.declinePayment = !customer().firstname && !cart().isGuestCheckoutAllowed;
                    return updatedCart;
                }.bind(this));
            }
            // END CHANGE

            return this;
        }