$userID = Client::getClientConnected()->id;
$items = self::all()->where('user_id', $userID)->toArray();
$allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items);
+ $idsProducts = array_column(call_user_func_array('array_merge', $allProducts),'id');
- $categories = Product::getCategoriesByProductsId($allProducts);
- $options = Product::getOptionsByProductsId($allProducts);
$categoryClient = Client::getClientConnected()->discount ?? 'A';
+ $productsInfosBase = Product::getProductsByIds($idsProducts);
+
foreach ($allProducts as $key => $products){
$data['products'] = $products;
$currentCartRef = Panier::getRefs($data['products']);
- $data = self::test($currentCartRef,$options,$categories,$categoryClient,$data);
- dd($data);
+ $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
$items[$key]['products'] = json_encode($data['products']);
+ $items[$key]['total'] = $data['total'];
}
return $items;
}
- protected static function test($currentCartRef,$options,$categories,$categoryClient,$data = []){
+ protected static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
$opt = [];
$total = [];
+
foreach ($currentCartRef as $keyRefs => $refs) {
- foreach (explode("/", $refs) as $key => $ref) {
- $key -= 1;
- if ($key > -1) {
- $optByProductId = array_column($options, $data['products'][$keyRefs]['id'])[0][0];
- $optByProductId = json_decode($optByProductId, true);
- $opt_ = array_filter($optByProductId[$keyRefs]["options"], function ($n) use ($ref) {
- return $n["ref"] === $ref;
- });
- $opt_ = array_values($opt_);
- $opt[$refs][] = $opt_[0]["sale_price"] ?? 0;
+ if($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json']) {
+ $options = json_decode($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json'], true);
+ foreach (explode("/", $refs) as $key => $ref) {
+ $key -= 1;
+ if ($key > -1) {
+ $options_ = $options[$key]['options'];
+ foreach ($options_ as $opt_) {
+ if ($opt_["ref"] === $ref) {
+ $opt[$refs][] = $opt_["sale_price"] ?? 0;
+ }
+ }
+ }
}
}
- $cat = array_column($categories, $data['products'][$keyRefs]['id'])['category'] ?? 'A';
+ $cat = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['category'];
$discount = Client::discountArray($cat,$categoryClient);
$data['products'][$keyRefs]['discount'] = $discount;
return $carry + $item;
}) : 0;
- $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"];
+ $basicSellingPrice = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['basic_selling_price'] ?? 0;
+ $data['products'][$keyRefs]["basic_selling_price"] = $basicSellingPrice;
$discount = floatval($data['products'][$keyRefs]["discount"]) ?? 0;
- $price = floatval($basicPriceByProductId + $totalOptionsPrice);
+ $price = floatval($basicSellingPrice + $totalOptionsPrice);
$price = ($price - (($price * $discount) / 100));
$total[] = str_replace(',','.',$price * $quantity);
$data['products'][$keyRefs]["price"] = $price;
/**
*
*/
- $categories = Product::getCategoriesByProductsId($data['products']);
- $options = Product::getOptionsByProductsId($data['products']);
$categoryClient = Client::getClientConnected()->discount ?? 'A';
- $data = self::test($currentCartRef, $options,$categories,$categoryClient,$data);
+ $data = self::test($currentCartRef,$categoryClient,$data);
//
if(isset($data['addresses']))
* Fetch selected product data for use in cart Vue component
* @return array
*/
- public static function getCartData()
+ public static function getCartData($cart_items = [])
{
- if (!self::$_cart_data) {
+ if (!self::$_cart_data && isset($cart_items)) {
- $cart_items = session('cart_items', []);
- self::$_cart_data = [];
+ if(sizeof($cart_items) < 1) {
+ $cart_items = session('cart_items', []);
+ self::$_cart_data = [];
+ }
$cart_items_id = array_map(function($n){ return $n['id']; }, $cart_items);
return self::$_cart_data;
}
- public static function getOptionsByProductsId($products) {
- $options = collect($products)->map(function ($product) {
- return collect($product)->map(function ($pro) {
- return Product::where('id', $pro['id'])
- ->select('id', 'json')
- ->get()
- ->groupBy('id')
- ->map(function ($group) {
- return $group->pluck('json')->toArray();
- })
- ->toArray();
- });
- })->toArray();
-
- $_arr = call_user_func_array('array_merge',$options);
-
- return $_arr;
-
- /*return array_map(function($n) {
- $key = array_keys($n)[0];
- return json_decode($n[$key][0], true);
- }, $_arr
- );*/
-
- /*return array_map(function($n) use($options) {
- return json_decode($options[$n['id']][0],true);
- }, $products
- );*/
- }
-
- public static function getCategoriesByProductsId($products) {
- $cat = collect($products)->map(function ($product) {
- return collect($product)->map(function ($pro) {
- return Product::where('id', $pro['id'])
- ->select('id', 'category')
- ->get()
- ->groupBy('id')
- ->toArray();
- });
- })->toArray();
-
- $_arr = call_user_func_array('array_merge',$cat);
-
- return array_map(function($n) {
- $key = array_keys($n)[0];
- return $n[$key][0];
- }, $_arr
- );
+ public static function getProductsByIds($ids) {
+ return self::whereIn('id', $ids)
+ ->select('id','basic_selling_price','category','json')
+ ->get()
+ ->groupBy('id')
+ ->toArray();
}
public static function getFilteredProducts($product_type, $filter_values = [])