增加和减少篮子中字幕项目的数量不起作用,laravel php

问题描述 投票:0回答:0

我一直在尝试制作一个篮子,你可以在其中添加两种项目,一种是有字幕的,其余的是无字幕的。我可以成功地将它们添加到篮子和从篮子中删除它们,但我只能增加和减少非字幕项目的数量。

来自 BasketBusiness 类的片段:

public function setItem($itemOrig, $item, $count, $add = false)
    {
        $itemProductId = explode('_',  $item['id'])[0];

        if ($count > 0) {
            // add subtitled items to basket
            if ( $item['subtitling'] == 1 ) {
                $basket = $this->getBasket();
                foreach ($basket['items'] as $_item) {
                    $basketProductId = explode('_',  $_item['item']['id'])[0];
                    if ( $itemProductId == $basketProductId ) {
                        $count = $count + $_item['item_sum']['count']-1;
                        if ($count < 1)
                        $count = 1;
                        break;
                    }
                }
            // add non-subtitled items to basket
            } else {
                $basketitem = Basket::factory()->get($item['id']);
                if (!empty($basketitem) && $add) {
                    $count = $count + $basketitem['item_sum']['count'];
                    if ($count < 1)
                        $count = 1;
                }
            }

            if ($add) {
                $color = $item['color'];
                $size  = $item['size'];
            } else {
                $color = $basketitem['item']['color'];
                $size  = $basketitem['item']['size'];
            }

            if ($itemOrig->getEntity()->onStock() == 1) {
                $stock = $itemOrig->getEntity()->colorSizeStock($color, $size);
                if ($count > $stock) {
                    return 12;
                }
            }

            if (!empty($basketitem)) {
                $status_code = Basket::factory()->update($item['id'], $this->prepareBasketItem($basketitem['item'], $count));
                //$status_code = Basket::factory()->update($item['id'], $this->prepareBasketItem($_item['item'], $count));
            } else {
                $status_code = Basket::factory()->add($item['id'], $this->prepareBasketItem($item, $count));
            }
        } else {
            $status_code = Basket::factory()->delete($item['id']);
        }

        if ($status_code == 1) {
            $this->modal = view('Order/cartAdded', [
                'product'               => Products::find(explode('_', $item['id'])[0]),
                'productOrderedSimilar' => (new \d2c\Products\Business\Products())->getOrderedSimilar(explode('_', $item['id'])[0]),
                'basketItem'            => $item,
                'count'                 => $count
            ])->render();
        }

        $this->updateBasketSum();
        return $status_code;
    }

篮类:

<?php

namespace d2c\Basket;

use Illuminate\Support\Facades\Session;

define('BASKET_ADD', 1);
define('BASKET_UPDATE', 2);
define('BASKET_REMOVE', 3);

class Basket
{
    public static function factory()
    {
        $basket = new Basket();
        return $basket;
    }

    public function getBasket()
    {
        if (!Session::exists('basket')) {
            $basket = [
                'items' => [],
                'sum'   => []
            ];
            return $this->saveBasket($basket);
        }
        return Session::get('basket');
    }

    public function get($id)
    {
        $basket = $this->getBasket();
        return isset($basket['items'][ $id ]) ? $basket['items'][ $id ] : null;
    }

    public function add($id, $basketitem)
    {
        $this->addUpdate($id, $basketitem);
        return BASKET_ADD;
    }

    public function update($id, $basketitem)
    {
        $this->addUpdate($id, $basketitem);
        return BASKET_UPDATE;
    }

    public function addUpdate($id, $basketitem)
    {
        $basket                 = $this->getBasket();
        $basket['items'][ $id ] = $basketitem;
        $this->saveBasket($basket);
    }

    public function delete($id)
    {
        $basket = $this->getBasket();
        unset($basket['items'][ $id ]);
        $this->saveBasket($basket);
        return BASKET_REMOVE;
    }

    public function saveBasket($basket)
    {
        Session::put('basket', $basket);
        return $basket;
    }
}
php laravel shopping-cart product-quantity
© www.soinside.com 2019 - 2024. All rights reserved.