Guest User

Untitled

a guest
May 31st, 2022
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * digitvision
  5.  *
  6.  * @category  digitvision
  7.  * @package   Shopware\Plugins\DvsnSetConfigurator
  8.  * @copyright (c) 2020 digitvision
  9.  */
  10.  
  11. namespace Dvsn\SetConfigurator\Core\Checkout\Promotion\Cart\Discount\ScopePackager;
  12.  
  13. use Dvsn\SetConfigurator\Service\Cart\LineItemFactoryServiceInterface;
  14. use Shopware\Core\Checkout\Cart\Cart;
  15. use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemQuantity;
  16. use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemQuantityCollection;
  17. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  18. use Shopware\Core\Checkout\Cart\Price\Struct\PriceDefinitionInterface;
  19. use Shopware\Core\Checkout\Cart\Rule\LineItemScope;
  20. use Shopware\Core\Checkout\Promotion\Cart\Discount\DiscountLineItem;
  21. use Shopware\Core\Checkout\Promotion\Cart\Discount\DiscountPackage;
  22. use Shopware\Core\Checkout\Promotion\Cart\Discount\DiscountPackageCollection;
  23. use Shopware\Core\Checkout\Promotion\Cart\Discount\DiscountPackager;
  24. use Shopware\Core\Framework\Rule\Rule;
  25. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  26.  
  27. class CartScopeDiscountPackager extends DiscountPackager
  28. {
  29.     private DiscountPackager $cartScopeDiscountPackager;
  30.  
  31.     public function __construct(
  32.         DiscountPackager $cartScopeDiscountPackager
  33.     ) {
  34.         $this->cartScopeDiscountPackager = $cartScopeDiscountPackager;
  35.     }
  36.  
  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     public function getResultContext(): string
  41.     {
  42.         return $this->cartScopeDiscountPackager->getResultContext();
  43.     }
  44.  
  45.     /**
  46.      * {@inheritDoc}
  47.      */
  48.     public function getDecorated(): DiscountPackager
  49.     {
  50.         return $this->cartScopeDiscountPackager;
  51.     }
  52.  
  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     public function getMatchingItems(DiscountLineItem $discount, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
  57.     {
  58.         // get parent items for default products
  59.         $matchingItems = $this->cartScopeDiscountPackager->getMatchingItems($discount, $cart, $context);
  60.  
  61.         // get our configurator items
  62.         $configuratorItems = $cart->getLineItems()->filterType(LineItemFactoryServiceInterface::CONFIGURATOR_LINE_ITEM_TYPE);
  63.  
  64.         // we have none?
  65.         if ($configuratorItems->count() === 0) {
  66.             // return default
  67.             return $matchingItems;
  68.         }
  69.  
  70.         // our items to add here
  71.         $items = [];
  72.  
  73.         // check every configuration
  74.         foreach ($configuratorItems as $lineItem) {
  75.             // create a default product item to make it work with the default packager
  76.             $productLineItem = new LineItem(
  77.                 $lineItem->getId(),
  78.                 LineItem::PRODUCT_LINE_ITEM_TYPE,
  79.                 $lineItem->getReferencedId(),
  80.                 $lineItem->getQuantity()
  81.             );
  82.  
  83.             // set it up
  84.             $productLineItem->setPrice($lineItem->getPrice());
  85.             $productLineItem->setQuantityInformation($lineItem->getQuantityInformation());
  86.             $productLineItem->setDeliveryInformation($lineItem->getDeliveryInformation());
  87.             $productLineItem->setPayload($lineItem->getPayload());
  88.             $productLineItem->setGood($lineItem->isGood());
  89.             $productLineItem->setPriceDefinition($lineItem->getPriceDefinition());
  90.             $productLineItem->setRemovable($lineItem->isRemovable());
  91.             $productLineItem->setRequirement($lineItem->getRequirement());
  92.             $productLineItem->setStackable($lineItem->isStackable());
  93.  
  94.             // are the discount rules valid?!
  95.             if (!$this->isRulesFilterValid($productLineItem, $discount->getPriceDefinition(), $context)) {
  96.                 // they arent
  97.                 continue;
  98.             }
  99.  
  100.             // in order to make the promotions work we need to make it stackable
  101.             $productLineItem->setStackable(true);
  102.  
  103.             // add to the items
  104.             $items[] = new LineItemQuantity($productLineItem->getId(), $productLineItem->getQuantity());
  105.         }
  106.  
  107.         // do we have any items that matched?
  108.         if ($items !== []) {
  109.             // add them
  110.             $matchingItems->add(new DiscountPackage(new LineItemQuantityCollection($items)));
  111.         }
  112.  
  113.         // and return all
  114.         return $matchingItems;
  115.     }
  116.  
  117.     /**
  118.      * ...
  119.      *
  120.      * @param LineItem                 $item
  121.      * @param PriceDefinitionInterface $priceDefinition
  122.      * @param SalesChannelContext      $context
  123.      *
  124.      * @return bool
  125.      */
  126.     private function isRulesFilterValid(LineItem $item, PriceDefinitionInterface $priceDefinition, SalesChannelContext $context): bool
  127.     {
  128.         if (!\method_exists($priceDefinition, 'getFilter')) {
  129.             return true;
  130.         }
  131.  
  132.         $filter = $priceDefinition->getFilter();
  133.         if (!$filter instanceof Rule) {
  134.             return true;
  135.         }
  136.  
  137.         $scope = new LineItemScope($item, $context);
  138.         return $filter->match($scope);
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment