库存管理电子商务 symfony
我正在 Symfony 中进行电子商务,对于我的购物车,我想管理库存,我的产品表中有库存参数,我希望当库存产品为 0 时,我们无法添加它到购物车,每次我们添加一个产品,它的库存就会减少 1 个,你知道该怎么做吗?
我在 CartService 中的功能:
public function add(int $id)
{
$cart = $this->session->get('cart', []);
if (array_key_exists($id, $cart)) {
$cart[$id]++;
} else {
$cart[$id] = 1;
}
$this->session->set('cart', $cart);
}
我在 CartController 中的功能
public function add($id, Request $request)
{
$product = $this->productRepository->find($id);
if (!$product) {
throw $this->createNotFoundException("le produit $id n'éxiste pas");
}
$this->cartService->add($id);
$this->addFlash('success', "le produit a bien été ajouté au panier");
if ($request->query->get('returnToCart')) {
return $this->redirectToRoute('cart_index');
}
if ($request->query->get('returnToHome')) {
return $this->redirectToRoute('product');
}
return $this->redirectToRoute('cart_index', []);
}
以及我实体产品中的库存
/**
* @ORM\Column(type="integer")
*/
private $stock;
public function getStock(): ?int
{
return $this->stock;
}
public function setStock(int $stock): self
{
$this->stock = $stock;
return $this;
}
感谢您的回复!
I'm doing an e-commerce in Symfony and for my cart, I would like to manage the stocks, I have the stock parameter in my product table and I would like that when there is 0 product in stock, we can't add it to the cart, and each time we add a product, it loses 1 in the stock, do you have an idea of how to do it?
My function in CartService :
public function add(int $id)
{
$cart = $this->session->get('cart', []);
if (array_key_exists($id, $cart)) {
$cart[$id]++;
} else {
$cart[$id] = 1;
}
$this->session->set('cart', $cart);
}
My function in CartController
public function add($id, Request $request)
{
$product = $this->productRepository->find($id);
if (!$product) {
throw $this->createNotFoundException("le produit $id n'éxiste pas");
}
$this->cartService->add($id);
$this->addFlash('success', "le produit a bien été ajouté au panier");
if ($request->query->get('returnToCart')) {
return $this->redirectToRoute('cart_index');
}
if ($request->query->get('returnToHome')) {
return $this->redirectToRoute('product');
}
return $this->redirectToRoute('cart_index', []);
}
And stock in my entity Product
/**
* @ORM\Column(type="integer")
*/
private $stock;
public function getStock(): ?int
{
return $this->stock;
}
public function setStock(int $stock): self
{
$this->stock = $stock;
return $this;
}
Thank you for your response !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从您提供的(不完整)代码示例中假设您使用 Symfony 4.4-ish(或更早版本)。我还假设您使用 Doctrine ORM (请参阅官方 Symfony 文档,了解如何在 Symfony 中使用它项目)
所以我认为您需要更改控制器,以便1-在将任何产品添加到购物车之前检查产品的库存,2-坚持以任何产品的库存更新为基础。类似于以下示例:
I assume from the (uncomplete) code sample you provided that you use a Symfony 4.4-ish (or earlier). I also assume that you use the Doctrine ORM (see official Symfony doc on how to use it in a Symfony project)
So I think you need to change your controller in order 1- to check the product's stock before adding anything to the cart and 2- to persist in base any product's stock update. Something like in the following example: