Как рассчитать корзину в плагине?Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как рассчитать корзину в плагине?

Сообщение Anonymous »

Я работаю над плагином Shopware 6, чтобы добавить некоторые параметры, добавляя продукт в корзину. Пользователь может выбрать некоторые параметры продукта и при нажатии «Добавить в корзину» идентификаторы выбранных параметров отправляются вместе с новой линейкой на бэкэнд. Там я добавляю все параметры в детстве в LineItem в Cartdatacollector . Дети добавляются в корзину, здесь у меня нет проблем. p>
Я попробовал этот картон < /code>, и он не работает: < /p>
class AdditionalPriceCartProcessor implements CartProcessorInterface
{
private QuantityPriceCalculator $calculator;

public function __construct(
QuantityPriceCalculator $calculator
) {
$this->calculator = $calculator;
}

public function process(
CartDataCollection $data,
Cart $original,
Cart $toCalculate,
SalesChannelContext $context,
CartBehavior $behavior
): void {
$prices = new PriceCollection();

foreach ($toCalculate->getLineItems() as $lineItem) {
if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
$prices->add($lineItem->getPrice());
continue;
}

if (!$lineItem->hasPayloadValue('HMnetAdditionalPricesAreSet')) {
$prices->add($lineItem->getPrice());
continue;
}

$tempOriginalCart = new Cart('temp-original', $original->getToken());
$tempCalculateCart = new Cart('temp-calculate', $original->getToken());

$tempCalculateCart->setLineItems(
$lineItem->getChildren()
);

$this->processChildren($data, $tempOriginalCart, $tempCalculateCart, $context, $behavior, $prices);

$lineItem->setChildren(
$tempCalculateCart->getLineItems()
);
}

$newCartPrice = $this->sum($original->getPrice(), $prices);
$toCalculate->setPrice($newCartPrice);
}

private function processChildren(
CartDataCollection $data,
Cart $original,
Cart $toCalculate,
SalesChannelContext $context,
CartBehavior $behavior,
PriceCollection $prices
): void {
foreach ($toCalculate->getLineItems() as $lineItem) {
// ensure we calculate only the items added via this plugin
...

$priceDefinition = $lineItem->getPriceDefinition();
$calculated = $this->calculator->calculate($priceDefinition, $context);

$lineItem->setPrice($calculated);
$prices->add($calculated);
}
}

private function sum(CartPrice $original, PriceCollection $prices): CartPrice
{
$total = $original->getTotalPrice();

foreach ($prices as $price) {
$total += $price->getTotalPrice();
}

return new CartPrice(
$total,
$total,
$total,
$original->getCalculatedTaxes(),
$original->getTaxRules(),
$original->getTaxStatus()
);
}
}


Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-plugin
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»