库存管理电子商务 symfony

发布于 2025-01-12 13:37:25 字数 1496 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长安忆 2025-01-19 13:37:25

我从您提供的(不完整)代码示例中假设您使用 Symfony 4.4-ish(或更早版本)。我还假设您使用 Doctrine ORM (请参阅官方 Symfony 文档,了解如何在 Symfony 中使用它项目

所以我认为您需要更改控制器,以便1-在将任何产品添加到购物车之前检查产品的库存,2-坚持以任何产品的库存更新为基础。类似于以下示例:

public function add($id, Request $request)
    {

        $entityManager = $this->getDoctrine()->getManager();
        $product = $this->productRepository->find($id);
        if (!$product) {

            throw $this->createNotFoundException("le produit $id n'éxiste pas");
        }
        
        $stock = $product->getStock();
        if($stock<=0){
            $this->addFlash('notice', "Sorry, no more stock");
        }
        else{
            $product->setStock($stock-1);
            $entityManager->flush();
            $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', []);
    }

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:

public function add($id, Request $request)
    {

        $entityManager = $this->getDoctrine()->getManager();
        $product = $this->productRepository->find($id);
        if (!$product) {

            throw $this->createNotFoundException("le produit $id n'éxiste pas");
        }
        
        $stock = $product->getStock();
        if($stock<=0){
            $this->addFlash('notice', "Sorry, no more stock");
        }
        else{
            $product->setStock($stock-1);
            $entityManager->flush();
            $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', []);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文