WIP... at least now we have working override of shipping costs

This commit is contained in:
Harald Welte 2022-06-24 21:07:01 +02:00
parent 94950fd8f0
commit f7ce5ab683
5 changed files with 181 additions and 7 deletions

View File

@ -1,8 +1,18 @@
{
"name": "swag/plugin-skeleton",
"description": "Skeleton plugin",
"name": "sysmocom/shopware6-shipcloud-live",
"description": "shipcloud live quote pluging",
"version": "0.0.1",
"type": "shopware-platform-plugin",
"license": "MIT",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Harald Welte",
"role": "main developer"
}
],
"require": {
"shopware/core": "^6."
},
"autoload": {
"psr-4": {
"SmcShipcloudLive\\": "src/"
@ -11,8 +21,8 @@
"extra": {
"shopware-plugin-class": "SmcShipcloudLive\\SmcShipcloudLive",
"label": {
"de-DE": "Skeleton plugin",
"en-GB": "Skeleton plugin"
"de-DE": "shipcloud live quote plugin",
"en-GB": "shipcloud live quote plugin"
}
}
}

View File

@ -0,0 +1,52 @@
<?php declare(strict_types=1);
namespace SmcShipcloudLive\Core\Checkout\Cart\Delivery;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
use Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator;
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
use Shopware\Core\Checkout\Cart\Tax\PercentageTaxRuleBuilder;
use Shopware\Core\Checkout\Shipping\Aggregate\ShippingMethodPrice\ShippingMethodPriceCollection;
use Shopware\Core\Checkout\Shipping\Aggregate\ShippingMethodPrice\ShippingMethodPriceEntity;
use Shopware\Core\Checkout\Shipping\Cart\Error\ShippingMethodBlockedError;
use Shopware\Core\Checkout\Shipping\Exception\ShippingMethodNotFoundException;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
use Shopware\Core\Framework\Util\FloatComparator;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class SmcDeliveryCalculator implements DeliveryCalculator
{
private $priceCalculator;
private $percentageTaxRuleBuilder;
public function __construct(
QuantityPriceCalculator $priceCalculator,
PercentageTaxRuleBuilder $percentageTaxRuleBuilder
) {
$this->priceCalculator = $priceCalculator;
$this->percentageTaxRuleBuilder = $percentageTaxRuleBuilder;
}
public function calculate(CartDataCollection $data, Cart $cart, DeliveryCollection $deliveries, SalesChannelContext $context): void
{
foreach ($deliveries as $delivery) {
//$this->calculateDelivery($data, $cart, $delivery, $context);
$costs = $this->calculateShippingCosts($delivery->getShippingMethod(),
new PriceCollection([new Price(Defaults::CURRENCY, 23, 0, false)]),
$delivery->getPositions()->getLineItems(),
$context);
$delivery->setShippingCosts($costs);
}
}
}

View File

@ -0,0 +1,65 @@
<?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);
}
}

View File

@ -10,5 +10,20 @@
<label>Active</label>
<label lang="de-DE">Aktiviert</label>
</input-field>
<input-field type="text">
<name>apiKey</name>
<label>shipcloud.io API key</label>
</input-field>
<input-field type="text">
<name>sandboxApiKey</name>
<label>Sandbox shipcloud.io API key</label>
</input-field>
<input-field type="bool">
<name>useSandbox</name>
<label>Use the sandbox environment</label>
<defaultValue>true</defaultValue>
</input-field>
</card>
</config>
</config>

View File

@ -6,5 +6,37 @@
<services>
<!-- <service id="SmcShipcloudLive\Core\Checkout\Cart\Delivery\SmcDeliveryCalculator" decorates="Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator"> -->
<!-- <argument type="service" id ="Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator.inner" /> -->
<!--
</service>
-->
<!-- brute-force replacement, no decoration -->
<!--
<service id="Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator" class="SmcShipcloudLive\Core\Checkout\Cart\Delivery\SmcDeliveryCalculator">
<argument type="service" id="Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator"/>
<argument type="service" id="Shopware\Core\Checkout\Cart\Tax\PercentageTaxRuleBuilder"/>
</service>
-->
<!-- processor in addition to the existing one -->
<!--
<service id="SmcShipcloudLive\Core\Checkout\Cart\Delivery\SmcDeliveryProcessor">
<tag name="shopware.cart.processor" priority="-5001"/>
<tag name="shopware.cart.collector" priority="-5001"/>
</service>
-->
<!-- brute-force replacement of existing processor, no decoration -->
<service id="Shopware\Core\Checkout\Cart\Delivery\DeliveryProcessor" class="SmcShipcloudLive\Core\Checkout\Cart\Delivery\SmcDeliveryProcessor">
<argument type="service" id="Shopware\Core\Checkout\Cart\Delivery\DeliveryBuilder"/>
<argument type="service" id="Shopware\Core\Checkout\Cart\Delivery\DeliveryCalculator"/>
<argument type="service" id="shipping_method.repository"/>
<tag name="shopware.cart.processor" priority="-5000"/>
<tag name="shopware.cart.collector" priority="-5000"/>
</service>
</services>
</container>
</container>