Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/php_interface/wbs24.lib/lib/Kit/ |
Upload File : |
<?php namespace Wbs24\Lib\Kit; use Bitrix\Main\Loader; class Helper { protected $wrappers; protected $priceId = 1; protected $currency = 'RUB'; public function __construct($objects = []) { if (!Loader::includeModule('catalog')) return false; if (!Loader::includeModule('iblock')) return false; $this->wrappers = new \stdClass(); $this->wrappers->CCatalogProductSet = $objects['CCatalogProductSet'] ?? new \CCatalogProductSet(); $this->wrappers->CIBlockElement = $objects['CIBlockElement'] ?? new \CIBlockElement(); $this->wrappers->Price = $objects['Price'] ?? new \Bitrix\Catalog\Model\Price(); } /** * Если $productId = false, то получить комплекты в которых есть данный товар * Иначе получить все комплекты */ public function getKitOwnerIds($productId = false, $fromProductId = false) { $ownerIds = []; $filter = [ "TYPE" => \CCatalogProductSet::TYPE_SET, ]; if ($productId) $filter["ITEM_ID"] = $productId; if ($fromProductId) $filter[">ITEM_ID"] = $fromProductId; $res = $this->wrappers->CCatalogProductSet->GetList( [], $filter, false, false, [] ); while ($item = $res->Fetch()) { $ownerIds[] = $item["OWNER_ID"]; } return array_unique($ownerIds); } public function getKitProductIdsToQuantity($ownerId) { $productIdsToQuantity = []; $res = $this->wrappers->CCatalogProductSet->GetList( [], [ "TYPE" => 1, "OWNER_ID" => $ownerId, ], false, false, [] ); while ($product = $res->Fetch()) { if ($product["OWNER_ID"] != $product["ITEM_ID"]) { $productIdsToQuantity[$product["ITEM_ID"]] = $product["QUANTITY"]; } } return $productIdsToQuantity; } public function updateKitPrice($ownerId) { $kitPrice = $this->calcKitPrice($ownerId); return $this->savePrice($ownerId, $kitPrice); } public function calcKitPrice($ownerId) { $kitPrice = 0; $productIdsToQuantity = $this->getKitProductIdsToQuantity($ownerId); $productIds = array_keys($productIdsToQuantity); $productIdsToPrices = $this->getProductIdsToPrices($productIds); foreach ($productIdsToQuantity as $productId => $quantity) { $kitPrice += $quantity * $productIdsToPrices[$productId]; } return $kitPrice; } public function getProductIdsToPrices($productIds) { $productIdsToPrices = []; $select = ["ID", "IBLOCK_ID", "SCALED_PRICE_".$this->priceId]; $filter = ["ID" => $productIds]; $result = $this->wrappers->CIBlockElement->GetList(["SORT" => "ASC"], $filter, false, false, $select); while ($fields = $result->Fetch()) { $productIdsToPrices[$fields["ID"]] = $fields["SCALED_PRICE_".$this->priceId]; } return $productIdsToPrices; } public function savePrice($productId, $price = 0) { $catalogGroupId = $this->priceId; $currency = $this->currency; if (!$catalogGroupId || !$productId || !$currency) return false; $priceResult = $this->wrappers->Price->getList([ 'filter' => [ 'CATALOG_GROUP_ID' => $catalogGroupId, 'PRODUCT_ID' => $productId ], ]); if ($priceInfo = $priceResult->Fetch()) { if ($price) { $result = $this->wrappers->Price->update($priceInfo['ID'], [ 'PRICE' => $price, 'PRICE_SCALE' => $price, 'CURRENCY' => $currency, ]); } else { $result = $this->wrappers->Price->delete($priceInfo['ID']); } } else { $result = $this->wrappers->Price->add([ 'CATALOG_GROUP_ID' => $catalogGroupId, 'PRODUCT_ID' => $productId, 'PRICE' => $price, 'PRICE_SCALE' => $price, 'CURRENCY' => $currency, ]); } return $result->isSuccess(); } }