src/Controller/OrderItemController.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Entity\Order\OrderItem;
  5. use App\Service\OrderPricesRecalculator;
  6. use Sylius\Bundle\OrderBundle\Controller\OrderItemController as OrderItemControllerBase;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. class OrderItemController extends OrderItemControllerBase
  13. {
  14.     public function getCart(KernelInterface $kernel){
  15.         $entityManager $this->getDoctrine()->getManager();
  16.         $cart $this->getContext()->getCart();
  17.         $items $cart->getItems();
  18.     
  19.         $data = [
  20.             'item_count' => $cart->getTotalQuantity(),
  21.             'total' => null,
  22.             'items' => []
  23.         ];
  24.     
  25.         $filesystem = new Filesystem();
  26.         $service_center_id = isset($_COOKIE['partnerId']) ? $_COOKIE['partnerId'] : null;
  27.         $priceDir $kernel->getProjectDir().'/private/service_center/prices/';
  28.         $prices_file $priceDir.$service_center_id.'.json';
  29.         $prices = [];   
  30.         if ($filesystem->exists($prices_file)) {
  31.             $dataPrices json_decode(file_get_contents($prices_file), true);
  32.             foreach ($dataPrices as $sku => $_data) {
  33.                 $practicedPrice = isset($_data['practicedPriceTTC']) && !empty($_data['practicedPriceTTC']) ? $_data['practicedPriceTTC'] * 100 null;
  34.                 $vendorPrice = isset($_data['vendorPriceTTC']) && !empty($_data['vendorPriceTTC']) ? $_data['vendorPriceTTC'] * 100 null;
  35.               
  36.                 $prices[$sku] = $practicedPrice !== null $practicedPrice $vendorPrice
  37.             }
  38.         }
  39.     
  40.         /** @var OrderItem[] $items */
  41.         foreach ($items as $item){
  42.             $variantCode $item->getVariant()->getCode();
  43.     
  44.             // Find and update the ChannelPricing entity
  45.             $channelPricings $item->getVariant()->getChannelPricings();
  46.             $channelPricing $channelPricings->get('default');
  47.             // Check if a custom price exists for this variant
  48.             if (isset($prices[$variantCode])) {
  49.                 $customPrice $prices[$variantCode];
  50.                 $item->setUnitPrice($customPrice);
  51.                 $channelPricing->setPrice($customPrice); 
  52.             } else {
  53.                 $item->setUnitPrice($channelPricing->getOriginalPrice());
  54.                 $channelPricing->setPrice($channelPricing->getOriginalPrice());
  55.             }
  56.             $entityManager->persist($channelPricing);
  57.             $entityManager->flush();
  58.     
  59.             $product $item->getProduct();
  60.     
  61.             $data['items'][] = [
  62.                 'id' => $item->getId(),
  63.                 'quantity' => $item->getQuantity(),
  64.                 'price' => $item->getUnitPrice(),
  65.                 'quotation' => $product->getRequire_quotation(),
  66.                 'in_stock' => $item->getVariant()->isInStock(),
  67.                 'product_title' => $item->getProductName(),
  68.                 'sku' => $variantCode
  69.             ];
  70.         }
  71.         $data['total'] = $cart->getTotal();
  72.         $cartManager $this->getCartManager();
  73.         $cartManager->persist($cart);
  74.         $cartManager->flush();
  75.     
  76.         return $this->json($data);
  77.     } 
  78.     public function clearCart(){
  79.         $cart $this->getContext()->getCart();
  80.         $cart->clearItems();
  81.         $cartManager $this->getCartManager();
  82.         $cartManager->persist($cart);
  83.         $cartManager->flush();
  84.         return $this->json([]);
  85.     }
  86.     public function refreshCart(OrderPricesRecalculator $orderPricesRecalculatorKernelInterface $kernel){
  87.         $cart $this->getContext()->getCart();
  88.         $orderPricesRecalculator->process($cart);
  89.         $cartManager $this->getCartManager();
  90.         $cartManager->persist($cart);
  91.         $cartManager->flush();
  92.         return $this->getCart($kernel);
  93.     }
  94.     public function updateCart(Request $requestKernelInterface $kernel){
  95.         $cart $this->getContext()->getCart();
  96.         $data json_decode($request->getContent(), true);
  97.         if (!isset($data['line']) || !isset($data['quantity'])) {
  98.             return new JsonResponse(['error' => 'Invalid data'], 400);
  99.         }
  100.         $itemId = (int)$data['line'];
  101.         $quantity = (int)$data['quantity'];
  102.         $orderItemQuantityModifier $this->container->get('sylius.order_item_quantity_modifier');
  103.         $entityManager $this->container->get('doctrine.orm.entity_manager');
  104.         $items $cart->getItems();
  105.         if (!$items[$itemId]) {
  106.             throw new NotFoundHttpException('Cart item not found.');
  107.         }
  108.         $orderItemQuantityModifier->modify($items[$itemId], $quantity);
  109.         $entityManager->flush();
  110.         return $this->getCart($kernel);
  111.     }
  112. }