用if语句增加laravel推车的重量

时间:2018-02-10 05:45:33

标签: php laravel cart

我尝试将产品重量放到我的购物车上,我可以得到它,但有两个问题:

  1. 仅当用户添加某些属性时,重量才会添加到我的购物车 产品。 It has to be add no matter user select any attribute or not.
  2. 当必须添加时,Wight会添加到我的每个属性中 分别为third attribute
  3. 这是用户选择其他属性

    的时候

    screen1

    这是我的控制器:

    //finding product
          $product = Product::findOrFail($id);
          $weight = $product->weight;  //get wight of product
    
          // get product options in cart
          $customAttributes = [];
          if(!empty($request->attr)){
              foreach($request->attr as $sub) {
    
                  // find the suboption
                  $sub = Suboption::find($sub);
                  if (!empty($sub->id)) {
                      array_push($customAttributes, [
                         'attr' => [
                            'name' => $sub->title,
                            'value' => $sub->price,
                            'weight' => $weight, // add weight to attributes
                          ]
                      ]);
                  }
              }
          }
    
          //adding product and options to cart
          Cart::add(array(
            'id' => $product->id,
            'name' => $product->title,
            'price' => $product->price,
            'quantity' => $request->input('quantity'),
            'attributes' => $customAttributes, // attributes will add to cart in array
          ));
    

    更新

    在更改bipin patel答案的数据库后,我发现了一些变化,但我发现了另外一个问题。

    这就是重量会出现在产品属性列表中,我无法将其because I only need weight for shipment calculation分开,所以我决定将我的产品attributes发送到conditions并将我的体重作为我的attribute

    在这种情况下,有两个好处:

    1. 我的产品属性价格会自动添加到购物车。 (作为条件)

    2. 我可以在没有过度循环的情况下在任何需要的地方获得体重。 (作为属性)

    3. So here is my code now

      //finding product
            $product = Product::findOrFail($id);
            $weight = $product->weight;
      
            // get product options in cart
            $weightArray = [
              'attr' => [
                    'name' => 'weight',
                    'value' => $weight,
              ]
            ];
            $customAttributes = [];
            // array_push($customAttributes,$weightArray);
            if(!empty($request->attr)){
                foreach($request->attr as $sub) {
                    // find the suboption
                    $sub = Suboption::find($sub);
                    if (!empty($sub->id)) {
                        array_push($customAttributes, [
                              'name' => $sub->title,
                              'value' => $sub->price,
                              'type' => 'additional',
                              'target' => 'item',
                        ]);
                    }
                }
            }
            // dd($customAttributes);
      
            //adding product and options to cart
            Cart::add(array(
              'id' => $product->id,
              'name' => $product->title,
              'price' => $product->price,
              'quantity' => $request->input('quantity'),
              'attributes' => $weightArray,
              'conditions' => $customAttributes,
            ));
      

      截图

      without choosing attributes

      screen2

      When I choose attributes

      screen3

      Here is dd of error above:

      screen4

1 个答案:

答案 0 :(得分:1)

您可以在开始if(!empty($request->attr))

之前创建权重数组并推送customAttributes

喜欢:

$weightArray = ['weight'=>$weight];
$customAttributes = [];
array_push($customAttributes,$weightArray); <--- push here
if(!empty($request->attr)){
...... rest of code

添加条件更新

if(!empty($request->attr)){
    foreach($request->attr as $sub) {
    // find the suboption
        $sub = Suboption::find($sub);
        if (!empty($sub->id)) {
            $itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
                'name' => $sub->title,
                'type' => 'additional',
                'target' => 'item',
                'value' => $sub->price,
            ));
            array_push($customAttributes, $itemCondition1);
        }
    }
}

你做这样的事情来添加条件