SmcShipcloudLive/SmcShipcloudLive/src/Core/Checkout/Cart/Delivery/SmcDeliveryProcessor.php

66 lines
2.6 KiB
PHP

<?php declare(strict_types=1);
namespace SmcShipcloudLive\Core\Checkout\Cart\Delivery;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartBehavior;
use Shopware\Core\Checkout\Cart\CartDataCollectorInterface;
use Shopware\Core\Checkout\Cart\CartProcessorInterface;
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Checkout\Shipping\Cart\Error\ShippingMethodBlockedError;
use Shopware\Core\Checkout\Cart\Delivery\DeliveryBuilder;
use Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
class SmcDeliveryProcessor implements CartProcessorInterface, CartDataCollectorInterface
{
protected $builder;
protected $deliveryCalculator;
protected $shippingMethodRepository;
public function __construct(DeliveryBuilder $builder, DeliveryCalculator $deliveryCalculator,
EntityRepositoryInterface $shippingMethodRepository)
{
$this->builder = $builder;
$this->deliveryCalculator = $deliveryCalculator;
$this->shippingMethodRepository = $shippingMethodRepository;
}
public static function buildKey(string $shippingMethodId): string
{
return 'shipping-method-' . $shippingMethodId;
}
public function collect(CartDataCollection $data, Cart $original, SalesChannelContext $context, CartBehavior $behavior): void
{
//$original->addErrors( new ShippingMethodBlockedError((string) "Foobar collect"));
/* ensure we have at least one shipping method key to avoid ShippingMethodNotFoundException */
$default_ship = $context->getShippingMethod();
$default_ship_id = $default_ship->getId();
$key = self::buildKey($default_ship_id);
$data->set($key, $default_ship);
}
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
{
/* below steps 1:1 from original shopware DeliveryProcessor */
$deliveries = $this->builder->build($toCalculate, $data, $context, $behavior);
$delivery = $deliveries->first();
/* custom computation of costs */
$costs = new CalculatedPrice(23, 42, new CalculatedTaxCollection(), new TaxRuleCollection());
$delivery->setShippingCosts($costs);
/* below steps 1:1 from original shopware DeliveryProcessor */
$this->deliveryCalculator->calculate($data, $toCalculate, $deliveries, $context);
$toCalculate->setDeliveries($deliveries);
}
}