hyva-themes / magento2-react-checkout

Highly Customizable Checkout for Magento 2, Built with React.
BSD 3-Clause "New" or "Revised" License
179 stars 53 forks source link

Set default selected payment & shipment method #169

Closed Morgy93 closed 3 years ago

Morgy93 commented 3 years ago

Hello,

I'd love to have some easy way to set the default selected payment and shipping method.

rajeev-k-tomy commented 3 years ago

@Morgy93 Not clear what you are actually looking for. To set a payment method, you require a billing address set to the quote. To set the shipping method, you required to have a valid shipping address against the quote. So this is clearly a custom behaviour and hence you should develop it your own

Morgy93 commented 3 years ago

@progammer-rkt There's actually a checked={method.code === paymentValues.code} already, see https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/components/paymentMethod/components/PaymentMethodList.jsx#L53

I'd love to be able to provide default values here. So upon first rendering check method.code === defaultValue (for instance cashondelivery)

Same for shipping: https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/components/shippingMethod/components/ShippingMethodList.jsx#L54

rajeev-k-tomy commented 3 years ago

paymentValues and selectedMethod are basically formik values. To give an initial value in them, then you need to update the initialValues available in the FormManager component.

For example, in the payment section you need to give the initial value here: https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/components/paymentMethod/components/PaymentMethodFormManager.jsx#L14-#L16

Morgy93 commented 3 years ago

For example, in the payment section you need to give the initial value here: https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/components/paymentMethod/components/PaymentMethodFormManager.jsx#L14-#L16

Awesome, that works! Thanks!

But it would be great to have this easily configurable. Now to achieve this you have to clone the repo, make changes, build, publish a custom version and use that one. And afterwards you're on your own with upstream changes. For such a simple config that's bonkers.

rajeev-k-tomy commented 3 years ago

@Morgy93 Hyvä Checkout is not intended to just plugin and boom it works. In almost all cases, you want to customize it. So you should actually fork it and use the fork in your site. This way you can apply your changes to the forked version.

Another approach would be set this up in the app/code directory where you are again able to customize it.

A better way for applying customization would be considered for a future version if the repo. Till that time, any of the above two approach should be the way you use Hyvä Checkout.

The given scenario is cannot be considered to be a generic case. This is a custom behaviour. So this must be a customization work specifically done for your site.

Morgy93 commented 3 years ago

@progammer-rkt What is there against having Magento config values for these fields? Like hyva_checkout/payment/default_code and hyva_checkout/shipping/default_code?

I saw your PR with translation strings fetched from Magento, so that should work with the config as well.

rajeev-k-tomy commented 3 years ago

Since the default payment method or shipping method is not part of the Magento, we don't want to consider it in this module.

We are actually passing the Magento configuration via https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/ViewModel/CheckoutConfigProvider.php#L65

So you can pass your custom configuration via that file (or via a plugin on that method).

rajeev-k-tomy commented 3 years ago

@Morgy93 I hope it helped out! Closing this issue now

Morgy93 commented 3 years ago

@progammer-rkt I just upgraded to the latest 1.0.0-rc and now it does not work anymore 😞

hyva_checkout_default_payment

It still is checked upon load, but gets unchecked automatically real quick.

Any help?

rajeev-k-tomy commented 3 years ago

@Morgy93 I feel like when the graphql response came back, it got reset. So provide a default value at this line https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/api/cart/fetchGuestCart/modifier.js#L91 would work probably.

For that change selectedPaymentMethod variable at https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/api/cart/fetchGuestCart/modifier.js#L79

const selectedPaymentMethod = _get(cartData, 'selected_payment_method') || { code: '<default_payment_method_code>' };
Morgy93 commented 3 years ago

For that change selectedPaymentMethod variable at https://github.com/hyva-themes/magento2-hyva-checkout/blob/main/src/reactapp/src/api/cart/fetchGuestCart/modifier.js#L79

const selectedPaymentMethod = _get(cartData, 'selected_payment_method') || { code: '<default_payment_method_code>' };

@progammer-rkt Sadly it does not work, it still gets unchecked. Any other workaround?