app/Plugin/Synplgbase/Service/PurchaseFlow/Processor/DeliveryAdvanceValidator.php line 40

Open in your IDE?
  1. <?php
  2. namespace Plugin\Synplgbase\Service\PurchaseFlow\Processor;
  3. use Eccube\Entity\ItemInterface;
  4. use Eccube\Service\PurchaseFlow\InvalidItemException;
  5. use Eccube\Service\PurchaseFlow\PurchaseContext;
  6. use Eccube\Service\PurchaseFlow\ItemValidator;
  7. use Plugin\Synplgbase\Repository\ConfigRepository;
  8. use Eccube\Service\CartService;
  9. use Eccube\Repository\ProductClassRepository;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. class DeliveryAdvanceValidator extends ItemValidator
  12. {
  13.     /**
  14.      * @var ConfigRepository
  15.      */
  16.     protected $configRepository;
  17.     /**
  18.      * @var CartService
  19.      */
  20.     protected $cartService;
  21.      /**
  22.      * @var ProductClassRepository
  23.      */
  24.     protected $productClassRepository;
  25.     /**
  26.      * @var ItemInterface
  27.      */
  28.     private $target_item;
  29.     public function __construct(ContainerInterface $containerInterface)
  30.     {
  31.         $this->configRepository $containerInterface->get(ConfigRepository::class)->get();
  32.         $this->cartService $containerInterface->get(CartService::class);
  33.         $this->productClassRepository $containerInterface->get(ProductClassRepository::class);
  34.     }
  35.     protected function validate(ItemInterface $itemPurchaseContext $context)
  36.     {
  37.         if(!$item->isProduct()){
  38.             return;
  39.         }
  40.         if(!$this->configRepository->getDeliveryAdvanceOn()){
  41.             return;
  42.         }
  43.         if(is_null($this->cartService->getCart())){
  44.             return;
  45.         }
  46.    
  47.         $delivery_advance_value_total 0;        
  48.         $arrCartItems $this->cartService->getCart()->getCartItems();
  49.         
  50.         foreach($arrCartItems as $value){
  51.             $delivery_advance_value_total += $value->getProductClass()->getDeliveryAdvanceValue() * $value->getQuantity();            
  52.             if(!is_null($this->configRepository->getDeliveryAdvanceCart()) && $delivery_advance_value_total $this->configRepository->getDeliveryAdvanceCart()){
  53.                 $this->target_item=$value;
  54.                 throw new InvalidItemException('カートに追加できるのは'.floatval($this->configRepository->getDeliveryAdvanceCart()).$this->configRepository->getDeliveryAdvanceUnit().'までです。この商品はカートに追加されません。');                
  55.             }
  56.         }
  57.     }
  58.     protected function handle(ItemInterface $itemPurchaseContext $context)
  59.     {
  60.         $qantity $this->target_item->getQuantity();
  61.         $this->target_item->setQuantity($qantity-1);
  62.     }
  63. }