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

Firstname and Lastname not filled when Guest in the shipping Address When DeliveryInStore Used #19

Open
sebastien-kathmandu opened this issue May 23, 2019 · 2 comments
Labels

Comments

@sebastien-kathmandu
Copy link

sebastien-kathmandu commented May 23, 2019

Preconditions

Magento Version : EE 2.2.8

Module Store Delivery Version : 1.1.2

Environment : Developer and Production Mode

Third party modules :

  • Paypal Express
  • Cybersource
  • Afterpay

Steps to reproduce

  1. create an order as guest in the Store

Expected result

  1. The shipping address should have the first name and last name of the billing address

Actual result

  1. The shipping address has empty firstname and empty lastname
@Fdec Fdec added the bug label Sep 3, 2019
@Fdec
Copy link
Contributor

Fdec commented Sep 3, 2019

Hi @sebastien-kathmandu

Have you always the bug ? Can you tell me more ?
I can't reproduce. Thanks

@sebastien-kathmandu
Copy link
Author

Hi @Fdec,

Indeed, it is still an issue on our environment we created a plugin to for our payment method to fill the gap of these missing field.

the plugin look like this :

<?php
/**
 * Kathmandu StoreDelivery.
 *
 * @category  Mage
 *
 * @author    Sebastien MATHIEU <[email protected]>
 * @copyright 2019 Kathmandu Ltd (https://www.kathmandu.co.nz)
 */

namespace Kathmandu\StoreDelivery\Plugin\Checkout\Api;

use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;

/**
 * Class SavePaymentInformationPlugin.
 *
 * @magentoAppArea adminhtml
 * @codingStandardsIgnoreFile
 */
class SaveGuestPaymentInformationPlugin
{
    /**
     * @var CartRepositoryInterface
     */
    private $quoteRepository;

    /**
     * @var QuoteIdMaskFactory
     */
    private $quoteIdMaskFactory;

    /**
     * @param CartRepositoryInterface $quoteRepository    Retailer Repository
     * @param QuoteIdMaskFactory      $quoteIdMaskFactory Customer session
     */
    public function __construct(
        CartRepositoryInterface $quoteRepository,
        QuoteIdMaskFactory $quoteIdMaskFactory
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;
    }

    /**
     * @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
     * @param                                                                  $cartId
     * @param                                                                  $email
     * @param                                                                  $paymentMethod
     * @param                                                                  $billingAddress
     *
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    // @codingStandardsIgnoreStart
    public function beforeSavePaymentInformation(//AndPlaceOrder
        \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
        $cartId,
        $email,
        $paymentMethod,
        $billingAddress
    ) {
        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->get($quoteIdMask->getQuoteId());
        $shippingAddress = $quote->getShippingAddress();

        $this->modifyShippingAddress($shippingAddress, $billingAddress);

        return;
    }

    /**
     * @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
     * @param                                                                  $cartId
     * @param                                                                  $email
     * @param                                                                  $paymentMethod
     * @param                                                                  $billingAddress
     *
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function beforeSavePaymentInformationAndPlaceOrder(
        \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
        $cartId,
        $email,
        $paymentMethod,
        $billingAddress
    ) {
        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->get($quoteIdMask->getQuoteId());
        $shippingAddress = $quote->getShippingAddress();

        $this->modifyShippingAddress($shippingAddress, $billingAddress);

        return;
    }

    /**
     * @param $shippingAddress
     * @param $billingAddress
     */
    private function modifyShippingAddress($shippingAddress, $billingAddress)
    {
        if (!is_null($billingAddress)) {
            if (!$shippingAddress->getFirstname() && $billingAddress->getFirstname()
            ) {
                $shippingAddress->setFirstname($billingAddress->getFirstname());
            }
            if (!$shippingAddress->getLastname() && $billingAddress->getLastname()
            ) {
                $shippingAddress->setLastname($billingAddress->getLastname());
            }
            if (!$shippingAddress->getTelephone() && $billingAddress->getTelephone()) {
                $shippingAddress->setTelephone($billingAddress->getTelephone());
            }
            $shippingAddress->save();
        }
    }
}

adding this to etc.xml :

    <type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
        <plugin name="kathmandu_store_delivery_save_guest_payment_information" type="Kathmandu\StoreDelivery\Plugin\Checkout\Api\SaveGuestPaymentInformationPlugin"/>
    </type>

of course something similar need to be done for Magento\Checkout\Api\PaymentInformationManagementInterface as the customer can be Guest or loggedIn.

Hope it help you guys to find a fix in your module itself.

Cheers,

PS: Some payment method can need specific plugin or sometime patch, Think about Paypal, clearpay, ...

Sebastien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants