skip to Main Content

I’m trying to change shipping method in shopware6 programmatically, but I’m not succeeding. I will not get any error or exception, there just will not be changed anything.

class:

<?php declare(strict_types=1);

namespace WtShippingCalcCoreCheckoutCart;

use JetBrainsPhpStormNoReturn;
use ShopwareCoreCheckoutCartCart;
use ShopwareCoreCheckoutCartCartBehavior;
use ShopwareCoreCheckoutCartCartProcessorInterface;
use ShopwareCoreCheckoutCartLineItemCartDataCollection;
use ShopwareCoreSystemSystemConfigSystemConfigService;
use ShopwareCoreSystemSalesChannelSalesChannelContext;
use WtShippingCalcServiceContactDatabaseShipping;
use ShopwareCoreFrameworkContext;
use ShopwareCoreSystemSalesChannelSalesChannelSalesChannelContextSwitcher;
use ShopwareCoreFrameworkValidationDataBagDataBag;

class ShippingProcessor implements CartProcessorInterface
{
    private SalesChannelContextSwitcher $contextSwitcher;
    private SystemConfigService $systemConfigService;
    private ContactDatabaseShipping $database;

    public function __construct(SalesChannelContextSwitcher $contextSwitcher, SystemConfigService $systemConfigService, ContactDatabaseShipping $database)
    {
        $this->contextSwitcher = $contextSwitcher;
        $this->systemConfigService = $systemConfigService;
        $this->database = $database;
    }

Any ideas, how to fix this issue?

3

Answers


  1. Chosen as BEST ANSWER

    first method:

        #[NoReturn] public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
    {
        /* !! ACHTUNG !! ACHTUNG !! !! ACHTUNG !! ACHTUNG !! !! ACHTUNG !! ACHTUNG !! !! ACHTUNG !! ACHTUNG !! !! ACHTUNG !! ACHTUNG !!
            Nicht vergessen: $_SESSION['itemsDuble'] nach dem Checkout/Bezahlvorgang ODER(!) dem Abbruch in den entsprechenden Subscriber
            unbedingt löschen!!
        */
       $original->
        $LaengeVorgabe = $this->systemConfigService->get('WtShippingCalc.config.laenge', null);
        $HoeheVorgabe = $this->systemConfigService->get('WtShippingCalc.config.hoehe', null);
        $BreiteVorgabe = $this->systemConfigService->get('WtShippingCalc.config.breite', null);
        $spezOneVorgabe = $LaengeVorgabe * $BreiteVorgabe * $HoeheVorgabe;
        $spezTwoVorgabe = $this->systemConfigService->get('WtShippingCalc.config.gewicht', null);
        if (session_status() === PHP_SESSION_NONE)
            session_start();
        //um der nachträglichen Hinzufügung von Artikeln in den Warenkorb gerecht zu werden, wird mit 2 SESSION-Arrays gearbeitet
        if (!empty($_SESSION['itemsDuble'])) {
            $orderGewicht = $_SESSION['items'][0] + $_SESSION['itemsDuble'][0];
            $orderLaenge = $_SESSION['items'][1] + $_SESSION['itemsDuble'][1];
            $orderBreite = $_SESSION['items'][2] + $_SESSION['itemsDuble'][2];
            $orderHoehe = $_SESSION['items'][3] + $_SESSION['itemsDuble'][3];
            $_SESSION['itemsDuble'][0] = $orderGewicht;
            $_SESSION['itemsDuble'][1] = $orderLaenge;
            $_SESSION['itemsDuble'][2] = $orderBreite;
            $_SESSION['itemsDuble'][3] = $orderHoehe;
        } else {
            $_SESSION['itemsDuble'] = $_SESSION['items'];
        }
        $spezOneOrder = $_SESSION['itemsDuble'][1] * $_SESSION['itemsDuble'][2] * $_SESSION['itemsDuble'][3];
        $spezTwoOrder = $_SESSION['itemsDuble'][0];
        //hole alle Shippingobjekte und packe sie in ein Array
        $shippingMethods = $this->database->readTableShipping(Context::createDefaultContext());
        foreach ($shippingMethods as $key => $value) {
            $arShippingId[] = $key;
            $arShippingObj[] = $value;
        }
        /*  in arShippingObj sind alle relevanten Shipping-Objekte. Je nach Abgleich soll das erste oder zweite Objekt verwendet
            werden! Trifft kein Kriterium zu, wird, sofern vorhanden, das dritte Objekt(Versandmethode) verwendet.
        */
        if (isset($arShippingId) && isset($arShippingObj) && count($arShippingId) == 2) {
            $this->DecideShippingFromTwo($spezOneOrder, $spezOneVorgabe, $spezTwoOrder, $spezTwoVorgabe, $arShippingObj, $context);
        } elseif (isset($arShippingId) && isset($arShippingObj) && count($arShippingId) > 2) {
            $this->DecideShippingFromMoreThanTwo($spezOneOrder, $spezOneVorgabe, $spezTwoOrder, $spezTwoVorgabe, $arShippingObj, $context);
        }
    }
    

  2. second method:

        #[NoReturn] private function assignShippingObject(object $shippingObject, SalesChannelContext $context): void
    {
        $folder = getcwd();
        $file = 'output.txt';
        $output = $folder . DIRECTORY_SEPARATOR . $file;
        $name = $shippingObject->getName();
        file_put_contents($output, "$name shipping has been used sending product", FILE_APPEND);
        file_put_contents($output, PHP_EOL, FILE_APPEND);
        $shippingId = $shippingObject->getId();
        $contextToken = $context->getToken();
        $dataBag = new DataBag([
            'id' => $shippingId,
        ]);
        $this->contextSwitcher->update($dataBag, $context);
        $salesChannels = $this->database->readTableSalesChannel(Context::createDefaultContext());
        // Prepare the data for updating sales channels
        foreach ($salesChannels as $salesChannel) {
            $shippingMethods = $salesChannel->getShippingMethods();
            $shippingMethodIds = [];
            foreach ($shippingMethods as $shippingMethod) {
                $shippingMethodIds[] = ['id' => $shippingObject->getId()];
            }
    
        }
        $salesChannelData[] = [
            'id' => $salesChannel->getId(),
            'shippingMethods' => $shippingMethodIds,
        ];
        $this->database->updateSalesChannel(Context::createDefaultContext(), $salesChannelData);
    }
    
    Login or Signup to reply.
  3. third method:

        #[NoReturn] private function DecideShippingFromMoreThanTwo(float $spezOne, int $spezVorgabeOne, float $spezTwo, int $spezVorgabeTwo, array $arShippingObj, SalesChannelContext $context): void
    {
        /*  spezOne mit spezVorgabeOne abgleichen; spezTwo mit spezVorgabeTwo abgleichen, um zu eriuieren, welche
            Versandmethode ausgewählt werden soll
        */
        foreach ($arShippingObj as $item) {
            if (str_contains(strtolower($item->getName()), 'weight')) {
                $shipingWeightObj = $item;
            } elseif (str_contains(strtolower($item->getName()), 'volume')) {
                $shipingVolumeObj = $item;
            } else {
                $shipingObjStandard = $item;
            }
        }
        //jetzt sind die ShippingObjekte aus dem Array gezogen und zugewiesen. Verwende Sie je nach Auswertung der Übergabeparameter
        if (isset($shipingVolumeObj) && $spezOne >= $spezVorgabeOne) {
            $shippingObject = $shipingVolumeObj;
        } elseif (isset($shipingWeightObj) && $spezTwo > $spezVorgabeTwo) {
            $shippingObject = $shipingWeightObj;
        } elseif (isset($shipingObjStandard)) {
            $shippingObject = $shipingObjStandard;
        }
        if (isset($shippingObject))
            $this->assignShippingObject($shippingObject, $context);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search