Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Data fetched from DB with filter and sort:
- array:6 [
- 0 => array:8 [
- "id" => 60
- "country_id" => 811
- "network" => "1"
- "product_id" => 3
- "group_zone" => null
- "modifier" => "5"
- "created_at" => null
- "updated_at" => null
- ]
- 1 => array:8 [
- "id" => 53
- "country_id" => 811
- "network" => "1"
- "product_id" => 1
- "group_zone" => null
- "modifier" => "3"
- "created_at" => null
- "updated_at" => null
- ]
- 2 => array:8 [
- "id" => 55
- "country_id" => null
- "network" => null
- "product_id" => 3
- "group_zone" => null
- "modifier" => "2"
- "created_at" => null
- "updated_at" => null
- ]
- 3 => array:8 [
- "id" => 56
- "country_id" => null
- "network" => null
- "product_id" => 3
- "group_zone" => null
- "modifier" => "2"
- "created_at" => null
- "updated_at" => null
- ]
- 4 => array:8 [
- "id" => 54
- "country_id" => null
- "network" => null
- "product_id" => 2
- "group_zone" => null
- "modifier" => "2"
- "created_at" => null
- "updated_at" => null
- ]
- 5 => array:8 [
- "id" => 57
- "country_id" => 812
- "network" => null
- "product_id" => 1
- "group_zone" => null
- "modifier" => "2"
- "created_at" => null
- "updated_at" => null
- ]
- ]
- Answer:
- Illuminate\Support\Collection^ {#2351
- #items: array:3 [
- 1 => "3"
- 2 => "2"
- 3 => "5"
- ]
- }
- public function setModifiersForProducts($products, $network = null, Country $country = null)
- {
- $country = $country ?? $this->getCurrentCountry();
- $country_id = $country->id;
- $group_zone = $country->inEEA() ? "EEA" : null;
- $products_count = count($products);
- $result = ProductPriceModifier
- ::where(fn($q) => $q
- ->where("network", $network)
- ->orWhere(fn($q) => $q->whereEmpty("network")))
- ->where(fn($q) => $q
- ->where("country_id", $country_id)
- ->orWhere(fn($q) => $q->whereEmpty("country_id"))
- ->orWhere("group_zone", $group_zone))
- ->where(fn($q) => $q
- ->whereIn("product_id", $products)
- ->orWhere(fn($q) => $q->whereEmpty("product_id")))
- ->orderBy('network', 'desc')
- ->orderBy('product_id', 'desc')
- ->orderBy('country_id', 'desc')
- ->get();
- dump($result->toArray());
- $ans = collect($products)->mapWithKeys(fn($item) => [$item => null]);
- while (true) {
- if ($result->first()->network_id !== null && $result->first()->product_id === null) {
- $ans = $ans->map(fn($item, $key) => $item ?? $result->first()->modifier);
- break;
- }
- $product_id = $result->first()->product_id;
- if ($ans[$product_id] === null) {
- $ans[$product_id] = $result->first()->modifier;
- $products_count--;
- }
- if ($products_count === 0) {
- break;
- }
- while ($result->first() && $result->first()['product_id'] === $product_id) {
- $result->shift();
- }
- }
- dump($ans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement