使用Laravel下订单后减少数据库中的产品数量 [英] Decrease product quantity in database after order is placed with Laravel

查看:64
本文介绍了使用Laravel下订单后减少数据库中的产品数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有购物车,到目前为止,一切正常.现在,我正在尝试为管理员可以在后端(已经完成)中添加的每种产品以及客户订购产品以减少数据库中的数量时增加数量.

I have cart on the site and so far everything work perfectly. Now I'm trying to make quantity for each product which admin can add in backend ( already done ) and when customer order product(s) to decrease quantity in database.

据我所言,管理面板已准备就绪,可以向存储在数据库中的产品增加数量.这是我的购物车提交控制器

So far as I said admin panel is ready and can add quantity to product which is saved in database. Here is my cart submit controller

public function orderSubmit() {
    $cart = Session::get(self::CART_SESSION_KEY, array());
    if (count($cart) < 1) {
        return Redirect::to('/');
    }

    $validatorRules = array(
        'captcha' => 'required|captcha'      
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $order = new Order();
    $order->user_id = self::$user->user_id;
    $order->data = json_encode($cart, true);
    $order->info = Input::get('additional_info');

    $order->save();
    Session::put(self::CART_SESSION_KEY, array());

    return Redirect::to('/)->with('message_success', 'Order created! We will contact you shortly to confirm your order details.');
}

购物车保存在$order->data = json_encode($cart, true);中,并且保存在产品的数据库信息中

The cart is saved in $order->data = json_encode($cart, true); and in database information for product(s) etc look

{"title":"Test Product","description":"You save 25%","quantity":1,"price":135}

如果按顺序有多个产品,则阵列中也将有它们.

If there are multiple products in order above array will have also them.

我无法理解的是如何提取订购的产品以及从中订购多少件产品,以便以后可以更新每种产品的数据库数量字段?

What I can't understand is how to extract which product is ordered and how many pieces from it so I can later update database quantity field for each product?

我什至不知道从哪里开始.

I don't even know from where to start.

推荐答案

假设$cart是一个数组,并且基于您提供的json,您需要:

Assuming $cart is an array and based on the json you provided, you'll need to:

  1. 将产品ID添加到$cart,否则几乎不可能知道该订单的产品
  2. 有了$cart上每个产品的产品ID,您现在可以循环购物车产品并进行更新.类似于以下代码.

  1. Add the product id to the $cart otherwise it will be pretty much impossible to know what are the products for that order
  2. With the product id for each product on the $cart, you can now loop the cart products and update them. Something like the following code.

public function orderSubmit() {
    $cart = Session::get(self::CART_SESSION_KEY, array());
    if (count($cart) < 1) {
        return Redirect::to('/');
    }

    $validatorRules = array(
        'captcha' => 'required|captcha'      
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }
    \DB::beginTransaction();

    try {
        $order = new Order();
        $order->user_id = self::$user->user_id;
        $order->data = json_encode($cart, true);
        $order->info = Input::get('additional_info');

        $order->save();

        foreach ($cart as $item) {
            $product = Product::find($item->id);
            $product->decrement('votes', $item->quantity);
        }

        Session::put(self::CART_SESSION_KEY, array());

        \DB::commit();

        return Redirect::to('/')->with('message_success', 'Order created! We will contact you shortly to confirm your order details.');
    } catch (\Exception $ex) {
        \DB::rollback();

        return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($ex->getMessage())->withInput(Input::except(['captcha']));
    }
}

我添加了交易代码,以确保在正确存储订单后减少数量.

I added the code for the transaction to assure the quantity is decreased when the order is stored correctly.

这篇关于使用Laravel下订单后减少数据库中的产品数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆