<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start.
* You can find more information about us on https://bitbag.io and write us an email on hello@bitbag.io.
*/
declare(strict_types=1);
namespace NieMehrStreichenTheme\Subscriber;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
use Shopware\Core\Checkout\Cart\CartPersister;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
class AdditionalProductsBasedOnWeight implements EventSubscriberInterface
{
private CartService $cartService;
public function __construct(
CartService $cartService,
LineItemFactoryRegistry $factory,
CartPersister $cartPersister,
SalesChannelContextService $salesChannelContextService
) {
$this->cartService = $cartService;
$this->factory = $factory;
$this->persister = $cartPersister;
$this->salesChannelContextService = $salesChannelContextService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'checkoutConfirmPageLoaded',
];
}
public function checkoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$palletsAreInsideOfOrder = false;
$totalWeight = 0;
$palletsTotalQuantity = 0;
$salesChannelContext = $event->getSalesChannelContext();
$cart = $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $salesChannelContext);
foreach($cart->getLineItems() as $item){
$referenceId = $item->getReferencedId();
if($referenceId != "af4cb7dfb30e471fa583ca2a39d41b0e"){
$deliveryInformation = $item->getDeliveryInformation();
if(method_exists($deliveryInformation, "getWeight")){
$totalWeight += $deliveryInformation->getWeight()*$item->getQuantity();
}
} else {
$palletsTotalQuantity += $item->getQuantity();
$palletsLineItemID = $item->getId();
$palletsAreInsideOfOrder = true;
}
}
$shippingMethodId = current($cart->getDeliveries()->getElements()[0]->getShippingMethod()->getPrices()->getElements())->getShippingMethodId();
if($shippingMethodId != "7afc682a2dc44c42813b95f51789b5ea"){ // If its classic shipping (not pickup)
if($totalWeight >= 24){
$countPallets = (int)ceil(($totalWeight/750));
if($palletsAreInsideOfOrder == true){
if($palletsTotalQuantity != $countPallets){
$this->cartService->changeQuantity($cart, $palletsLineItemID, $countPallets, $salesChannelContext);
header("Location: /checkout/confirm");
exit;
}
} else {
$lineItem = $this->factory->create([
'type' => "product",
'referencedId' => 'af4cb7dfb30e471fa583ca2a39d41b0e',
'quantity' => 10
], $salesChannelContext);
$cart->add($lineItem);
$this->persister->save($cart, $salesChannelContext);
header("Location: /checkout/confirm");
exit;
}
} else {
if($palletsAreInsideOfOrder == true){
$cart->remove($palletsLineItemID);
$this->persister->save($cart, $salesChannelContext);
header("Location: /checkout/confirm");
exit;
}
}
} else {
//Delete if shipping method is pick-up and pallets are inside of a order
if($palletsAreInsideOfOrder == true){
$cart->remove($palletsLineItemID);
$this->persister->save($cart, $salesChannelContext);
header("Location: /checkout/confirm");
exit;
}
}
}
}