Skip to content

Commit

Permalink
Release 6.5 04.09.2024
Browse files Browse the repository at this point in the history
fix: Add support for Custom Products extension
  • Loading branch information
lukasz.brodowski committed Sep 4, 2024
1 parent 552c9cd commit 5dbebdc
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions src/Service/Easy/CheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Nets\Checkout\Service\Easy\Api\EasyApiService;
use Nets\Checkout\Service\Easy\Api\Exception\EasyApiException;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Order\OrderEntity;
Expand All @@ -24,6 +26,7 @@
use Shopware\Core\System\StateMachine\StateMachineRegistry;
use Shopware\Core\System\StateMachine\Transition;
use Shopware\Storefront\Framework\Routing\Router;
use Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand Down Expand Up @@ -366,6 +369,43 @@ private function collectRequestParams(SalesChannelContext $salesChannelContext,
return $data;
}

/**
* @param LineItem|OrderLineItemEntity $item
*/
private function isItemFromCustomProductExtension(Struct $item): bool
{
if (!class_exists(CustomizedProductsCartDataCollector::class)) {
return false;
}

return $item->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE;
}

/**
* @param LineItem|OrderLineItemEntity $item
*/
private function getReferenceName(Struct $item): string
{
$defaultReturn = $item->getId();
if ($item instanceof OrderLineItemEntity) {
$defaultReturn = $item->getProductId() ?? $item->getId();
}

if (!method_exists($item, 'getPayload')) {
return $defaultReturn;
}

$payload = $item->getPayload();
if (isset($payload['productNumber'])) {
return $payload['productNumber'];
}

return $defaultReturn;
}

/**
* @param Cart|OrderEntity $cartOrderEntityObject
*/
private function getOrderItems(Struct $cartOrderEntityObject, SalesChannelContext $salesChannelContext = null): array
{
$display_gross = true;
Expand All @@ -380,21 +420,24 @@ private function getOrderItems(Struct $cartOrderEntityObject, SalesChannelContex

$items = [];
$sumAmount = 0;
$parentIds = [];

foreach ($cartOrderEntityObject->getLineItems() as $item) {

$taxes = $this->getRowTaxes($item->getPrice()
->getCalculatedTaxes());

$quantity = $item->getQuantity();

if ($cartOrderEntityObject instanceof Cart) {
$ref_name = $item->getId();

if (method_exists($item, 'getpayload')) {
$payload = $item->getpayload();
$ref_name = $this->getReferenceName($item);
$name = $this->stringFilter($item->getLabel());

if (isset($payload['productNumber'])) {
$ref_name = $payload['productNumber'];
}
if ($this->isItemFromCustomProductExtension($item)) {
$customItem = $item->getChildren()->firstWhere(fn(LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE);
$ref_name = $this->getReferenceName($customItem);
$name = $customItem->getLabel();
}

$product = $item->getPrice()->getUnitPrice();
Expand All @@ -413,7 +456,7 @@ private function getOrderItems(Struct $cartOrderEntityObject, SalesChannelContex

$items[] = [
'reference' => $ref_name,
'name' => $this->stringFilter($item->getLabel()),
'name' => $name,
'quantity' => $quantity,
'unit' => 'pcs',
'unitPrice' => $unitPrice,
Expand All @@ -427,16 +470,23 @@ private function getOrderItems(Struct $cartOrderEntityObject, SalesChannelContex
}

if ($cartOrderEntityObject instanceof OrderEntity) {
$product = $item->getUnitPrice();
if ($item->getParentId() === null && $this->isItemFromCustomProductExtension($item)) {
$parentIds[] = $item->getId();
}

$ref_name = $item->getProductId() ?? $item->getId();
if (in_array($item->getParentId(), $parentIds)) {
continue;
}

if (method_exists($item, 'getpayload')) {
$payload = $item->getpayload();
$product = $item->getUnitPrice();

if (isset($payload['productNumber'])) {
$ref_name = $payload['productNumber'];
}
$ref_name = $this->getReferenceName($item);
$name = $this->stringFilter($item->getLabel());

if ($this->isItemFromCustomProductExtension($item)) {
$customItem = $cartOrderEntityObject->getLineItems()->firstWhere(fn(OrderLineItemEntity $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE && $lineItem->getParentId() === $item->getId());
$ref_name = $this->getReferenceName($customItem);
$name = $customItem->getLabel();
}

if ($display_gross) {
Expand All @@ -453,7 +503,7 @@ private function getOrderItems(Struct $cartOrderEntityObject, SalesChannelContex

$items[] = [
'reference' => $ref_name,
'name' => $this->stringFilter($item->getLabel()),
'name' => $name,
'quantity' => $quantity,
'unit' => 'pcs',
'unitPrice' => $unitPrice,
Expand Down

0 comments on commit 5dbebdc

Please sign in to comment.