我们目前正在使用 Sylius 开发基于 ERP 的网上商店。其中一项功能是客户可以选择多种尺寸和数量,并一次性将它们添加到购物车中。
通常 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 时,我们是否遗漏了一些东西以获得正确的总数?
提前致谢。
最佳答案
是的,通过使用此服务,我们能够正确修改数量和单价。谢谢,我们显然忽略了这项服务。我们还删除了 CartItem ($item->setQuantity($quantity);
) 的修改,因为这是一个自定义方法,setter 通常不可用。
关于php - Sylius 通过 CartItemController 添加到购物车总计 0,00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208980/