2016-08-29 30 views
3

我們目前正在開發一個基於ERP的網上商店與Sylius。其中一項功能是客戶可以選擇多種尺寸和數量,並將其添加到購物車中。Sylius加入購物車通過CartItemController給出0,00總計

通常情況下,Sylius只能爲基於請求的ItemResolver使用一個變體。我們試圖覆蓋CartItemController,以便我們能夠循環請求變量並將所有項目添加到購物車。

我們嘗試使用此代碼:

try { 
     // When we have multiple 
     $reqSize  = $request->request->get('size'); 
     $reqQuantity = $request->request->get('quantity'); 
     $reqProductID = $request->request->get('product_id'); 
     $reqColorID = $request->request->get('variant_color_id'); 

     if (null !== $reqSize && null !== $reqQuantity && null !== $reqProductID && null !== $reqColorID && count($reqSize) === count($reqQuantity)) { 
      $provider   = $this->get('sylius.cart_provider'); // Implements the CartProviderInterface. 
      $currentCart   = $provider->getCart(); 
      $priceCalculator  = $this->get('sylius.price_calculator'); 
      $availabilityChecker = $this->get('sylius.availability_checker'); 

      $productRepo = $this->get('sylius.repository.product'); 
      $variantRepo = $this->get('sylius.repository.product_variant'); 
      $sizeRepo = $this->get('jartazi.repository.sizegrid_size'); 
      $colorRepo = $this->get('jartazi.repository.color'); 

      $product = $productRepo->find(intval($reqProductID)); 
      $color = $colorRepo->find(intval($reqColorID)); 

      for ($i = 0; $i < count($reqSize); $i++) { 
       $size  = $sizeRepo->find(intval($reqSize[$i])); 
       $variant = $variantRepo->findOneBy(['object' => $product, 'size' => $size, 'color' => $color]); 
       $quantity = intval($reqQuantity[$i]); 

       if (null === $variant) { 
        throw new ItemResolvingException('Selected item is out of stock.'); 
       } 


       if (null !== $product && null !== $color && null !== $size && null !== $variant) { 
        // Make a cart item 
        $item = $this->get('sylius.factory.cart_item')->createNew(); 

        $item->setSize($size); 
        $item->setVariant($variant); 
        $item->setQuantity($quantity); 

        $context = ['quantity' => $quantity]; 

        if (null !== $customer = $cart->getCustomer()) { 
         $context['groups'] = $customer->getGroups()->toArray(); 
        } 

        $item->setUnitPrice($priceCalculator->calculate($variant, $context)); 

        // Check for equal products 
        foreach ($currentCart->getItems() as $cartItem) { 
         if ($cartItem->equals($item)) { 
          $quantity += $cartItem->getQuantity(); 
          break; 
         } 
        } 

        if (!$availabilityChecker->isStockSufficient($variant, $quantity)) { 
         throw new ItemResolvingException('Selected item is out of stock.'); 
        } 

        $event = new CartItemEvent($cart, $item); 

        // Update models 
        $eventDispatcher->dispatch(SyliusCartEvents::ITEM_ADD_INITIALIZE, $event); 
        $eventDispatcher->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($cart)); 
        $eventDispatcher->dispatch(SyliusCartEvents::CART_SAVE_INITIALIZE, $event); 

        // Write flash message 
        $eventDispatcher->dispatch(SyliusCartEvents::ITEM_ADD_COMPLETED, new FlashEvent()); 
       } 
      } 

      return $this->redirectAfterAdd($configuration); 
     } 
    } catch (ItemResolvingException $exception) { 
     // Write flash message 
     $eventDispatcher->dispatch(SyliusCartEvents::ITEM_ADD_ERROR, new FlashEvent($exception->getMessage())); 

     return $this->redirectAfterAdd($configuration); 
    } 

但是,當我們添加一個到購物車,購物車總停留0,00

我們是否失去了一些東西,纔能有添加時正確總計沒有ItemResolver的CartItem?

在此先感謝。

回答

0

按照Coudenysj的規定,您可能需要在您的代碼中的某處調用OrderItemQuantityModifier。

我想你需要手動調用修改設置

$this->get('sylius.order_item_quantity_modifier')->modify($item, $quantity); 

// $item->setQuantity($quantity); 

其實看來你的數量之前只需要調用的修改功能,並更新量爲正確的值

我因爲這個原因,我自己添加到購物車的代碼有很多麻煩。我最終修改了CartItemType。但是現在我確實需要能夠一次添加多個到購物車。所以如果這能解決你的問題,我會非常感興趣。

請發佈更新

1

是,由於採用這種服務,我們能夠正確地修改數量和單位價格。謝謝,我們顯然忽略了這項服務。我們對CartItem($item->setQuantity($quantity);)的修改也因爲這是一種自定義方法而被刪除,通常無法使用setter。