<?php
declare(strict_types=1);
namespace AmMacTradeCustomPlugins\Core\Checkout\Promotion\Cart;
use AmMacTradeCustomPlugins\Subscriber\MacTradeSubscriber;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartBehavior;
use Shopware\Core\Checkout\Cart\CartProcessorInterface;
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ProcessorCustom implements CartProcessorInterface
{
private QuantityPriceCalculator $QPCalculator;
private SystemConfigService $systemConfigService;
public function __construct(
QuantityPriceCalculator $QPCalculator,
SystemConfigService $systemConfigService
)
{
$this->QPCalculator = $QPCalculator;
$this->systemConfigService = $systemConfigService;
}
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
{
$config = $this->systemConfigService->get('AmMacTradeCustomPlugins.config');
$configIdealoFeedManagement = $config['idealoFeed'] ?? false;
if ($configIdealoFeedManagement) {
$lineItems = $toCalculate->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
$productsOriginal = $original->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
if (@$_SESSION['CREATED'] && (time() - $_SESSION['CREATED'] > MacTradeSubscriber::SESSION_EXPIRE_TIME)) {
// session started more than 2 minutes ago
unset($_SESSION['sw-idealo-feed-product-id']);
unset($_SESSION['sw-platform-url']);
unset($_SESSION['CREATED']);
// session_regenerate_id(true); // change session ID for the current session and invalidate old session ID
}
foreach ($lineItems as $lineItem) {
// Session data for product
$comparisonSite = @$_SESSION['sw-idealo-feed-product-id'];
if (@$comparisonSite && sizeof($comparisonSite) > 0 && in_array($lineItem->getReferencedId(), $comparisonSite)) {
// $originalUnitPrice = @$lineItem->getPayload()['customFields']['product_custom_fields_idealo'] ? $lineItem->getPayload()['customFields']['product_custom_fields_idealo'] : $lineItem->getPrice()->getUnitPrice();
$originalUnitPrice = $lineItem->getPayload()['customFields']['product_custom_fields_idealo'] ?? $lineItem->getPrice()->getUnitPrice();
} else {
$originalUnitPrice = $lineItem->getPrice()->getUnitPrice();
}
// build new price definition
$definition = new QuantityPriceDefinition(
$originalUnitPrice,
$lineItem->getPrice()->getTaxRules(),
$lineItem->getPrice()->getQuantity()
);
// build CalculatedPrice over calculator class for overwritten price
$QPCalculated = $this->QPCalculator->calculate($definition, $context);
// set new price into line item
$lineItem->setPrice($QPCalculated);
$lineItem->setPriceDefinition($definition);
}
foreach ($productsOriginal as $product) {
// Session data for product
$comparisonSite = @$_SESSION['sw-idealo-feed-product-id'];
if (@$comparisonSite && sizeof($comparisonSite) > 0 && in_array($product->getReferencedId(), $comparisonSite)) {
// $originalUnitPrice = @$product->getPayload()['customFields']['product_custom_fields_idealo'] ? $product->getPayload()['customFields']['product_custom_fields_idealo'] : $product->getPrice()->getUnitPrice();
$originalUnitPrice = $product->getPayload()['customFields']['product_custom_fields_idealo'] ?? $product->getPrice()->getUnitPrice();
} else {
$originalUnitPrice = $product->getPrice()->getUnitPrice();
}
// build new price definition
$definition = new QuantityPriceDefinition(
$originalUnitPrice,
$product->getPrice()->getTaxRules(),
$product->getPrice()->getQuantity()
);
// build CalculatedPrice over calculator class for overwritten price
$QPCalculated = $this->QPCalculator->calculate($definition, $context);
// set new price into line item
$product->setPrice($QPCalculated);
$product->setPriceDefinition($definition);
}
}
}
}