Skip to content

Commit

Permalink
Move addition of SKU & vendor fields to hydration class (#263)
Browse files Browse the repository at this point in the history
* remove sku from getCartLineItems

* remove empty vendor field

* add required sku and vendor fields to items on hydrate

* Add sku & vendor fields to items on hydrate
  • Loading branch information
nicolenorman authored May 31, 2024
1 parent 1798d82 commit 235ac77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 28 additions & 1 deletion Model/Order/HydrateOrderFromQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bold\Checkout\Api\Order\HydrateOrderFromQuoteInterface;
use Bold\Checkout\Model\Order\Address\Converter;
use Bold\Checkout\Model\Quote\GetCartLineItems;
use Magento\Catalog\Model\ProductFactory;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address\ToOrderAddress;

Expand Down Expand Up @@ -45,22 +46,30 @@ class HydrateOrderFromQuote implements HydrateOrderFromQuoteInterface
*/
private $quoteToOrderAddressConverter;

/**
* @var ProductFactory
*/
private $productFactory;

/**
* @param ClientInterface $client
* @param GetCartLineItems $getCartLineItems
* @param Converter $addressConverter
* @param ToOrderAddress $quoteToOrderAddressConverter
* @param ProductFactory $productFactory
*/
public function __construct(
ClientInterface $client,
GetCartLineItems $getCartLineItems,
Converter $addressConverter,
ToOrderAddress $quoteToOrderAddressConverter,
ProductFactory $productFactory,
) {
$this->client = $client;
$this->getCartLineItems = $getCartLineItems;
$this->addressConverter = $addressConverter;
$this->quoteToOrderAddressConverter = $quoteToOrderAddressConverter;
$this->productFactory = $productFactory;
}

/**
Expand All @@ -85,9 +94,12 @@ public function hydrate(CartInterface $quote, string $publicOrderId): ResultInte
return $sum + $discountLine['value'];
});

$cartItems = $this->getCartLineItems->getItems($quote);
$formattedCartItems = $this->formatCartItems($cartItems);

$body = [
'billing_address' => $this->addressConverter->convert($billingAddress),
'cart_items' => $this->getCartLineItems->getItems($quote),
'cart_items' => $formattedCartItems,
'taxes' => $this->getTaxLines($totals['tax']['full_info']),
'discounts' => $discounts,
'fees' => $fees,
Expand Down Expand Up @@ -195,4 +207,19 @@ private function getFeesAndDiscounts(array $totals): array

return [$fees, $discounts];
}

/**
* @param array $cartItems
* @return array
*/
private function formatCartItems(array $cartItems): array
{
foreach ($cartItems as &$item) {
$cartItem = $this->productFactory->create()->load($item['id']);
$item['sku'] = $cartItem->getSku();
$item['vendor'] = '';
}

return $cartItems;
}
}
2 changes: 0 additions & 2 deletions Model/Quote/GetCartLineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ private function getLineItem(CartItemInterface $item): array
{
return [
'id' => (int)$item->getProduct()->getId(),
'sku' => $item->getSku(),
'quantity' => $this->extractLineItemQuantity($item),
'title' => $this->getLineItemName($item),
'product_title' => $this->getLineItemName($item),
Expand All @@ -108,7 +107,6 @@ private function getLineItem(CartItemInterface $item): array
'requires_shipping' => !$item->getProduct()->getIsVirtual(),
'line_item_key' => (string)$item->getId(),
'price' => $this->getLineItemPrice($item),
'vendor' => '',
];
}

Expand Down

0 comments on commit 235ac77

Please sign in to comment.