Skip to content

Commit

Permalink
Merge pull request #55 from zans-laksa/1809-checkout-duplicate-address
Browse files Browse the repository at this point in the history
Checkout Address Duplicate Backend
  • Loading branch information
alfredsgenkins authored Jan 19, 2021
2 parents afc028b + a3ea690 commit 86e0b87
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Model/Resolver/SetBillingAddressOnCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart as SetBillingAddressOnCartModel;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Mutation resolver for setting payment method for shopping cart
Expand All @@ -39,22 +40,28 @@ class SetBillingAddressOnCart implements ResolverInterface
/** @var CheckCartCheckoutAllowance */
protected $checkCartCheckoutAllowance;

/** @var CustomerRepositoryInterface */
protected $customerRepository;

/**
* @param GetCartForUser $getCartForUser
* @param CartManagementInterface $cartManagement
* @param SetBillingAddressOnCartModel $setBillingAddressOnCart
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
* @param CustomerRepositoryInterface $customerRepository
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
SetBillingAddressOnCartModel $setBillingAddressOnCart,
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
CheckCartCheckoutAllowance $checkCartCheckoutAllowance,
CustomerRepositoryInterface $customerRepository
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->setBillingAddressOnCart = $setBillingAddressOnCart;
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
$this->customerRepository = $customerRepository;
}

/**
Expand All @@ -73,8 +80,12 @@ public function resolve(
throw new GraphQlInputException(__('Required parameter "billing_address" is missing'));
}
$billingAddress = $args['input']['billing_address'];
$customerAddressId = isset($billingAddress['customer_address_id'])
? $billingAddress['customer_address_id']
: null;
$sameAsShipping = !!(isset($args['input']['same_as_shipping']) && $args['input']['same_as_shipping']);

$storeId = (int) $context->getExtensionAttributes()->getStore()->getId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();

$customerId = $context->getUserId();
if ($guestCartId !== '') {
Expand All @@ -84,7 +95,24 @@ public function resolve(
}

$this->checkCartCheckoutAllowance->execute($cart);
$this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
$billingAddressIsSaved = false;

if ($sameAsShipping && $customerAddressId) {
$customer = $cart->getCustomer();
$billingAddressId = $customer->getDefaultBilling();

if (!$billingAddressId) {
$customer->setDefaultBilling((string)$customerAddressId);
$this->customerRepository->save($customer);
$billingAddressIsSaved = true;
} else {
unset($billingAddress['customer_address_id']);
}
}

if (!$billingAddressIsSaved) {
$this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
}

return [
'cart' => [
Expand Down
1 change: 1 addition & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Mutation {
input S_SetBillingAddressOnCartInput {
guest_cart_id: String
billing_address: BillingAddressInput!
same_as_shipping: Boolean
}

input S_SetPaymentMethodOnCartInput {
Expand Down

0 comments on commit 86e0b87

Please sign in to comment.