将购物车产品与可用交易进行比较-Laravel

时间:2020-08-17 19:58:25

标签: laravel if-statement wherehas

我正在为我的自定义电子商务添加交易。这意味着管理员可以创建交易并指定对哪些产品/类别/品牌有效的交易。例如,管理员可以创建2 + 1交易,但仅针对Category_01,或者他可以创建交易-在Category_01中至少购买100美元的产品并免费获得一个。我创建了带有交易的表和用于产品/类别/品牌的数据透视表。到目前为止一切正常。当用户的购物车中有产品时,我需要找到符合购物车内容的交易。我正在尝试这样的事情:

$productIds = array_unique($cartProducts->pluck('product_id')->toArray());
$categoryIds = ShopProductCategoryPivot::whereIn('product_id', $productIds)->pluck('category_id')->toArray();
$brandIds = array_unique($cartProducts->pluck('product.brand_id')->toArray());

$deals = ShopProductDeal::published()
                        ->whereHas('productsPivot', function($q) use ($productIds) {
                            $q->whereIn('product_id', $productIds);
                        })->orWhereHas('categoriesPivot', function($q) use ($categoryIds) {
                            $q->whereIn('category_id', $categoryIds);
                        })->orWhereHas('brandsPivot', function($q) use ($brandIds) {
                            $q->whereIn('brand_id', $brandIds);
                        })->with(['lang', 'products', 'categories', 'brands'])
                        ->get();

此代码有效,但还不够。我需要比较产品数量/总和(价格)是否满足定义的条件。我在考虑使用

whereHas(function(){...}, '=', $required_count) 

但是我不知道$ required_count是多少。你有解决的办法吗?

0 个答案:

没有答案
相关问题