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/modules/grain.iiko/lib/ |
Upload File : |
<?php namespace Grain\Iiko; use \Bitrix\Main\Localization\Loc; use \Bitrix\Main\Web\Json; use \Bitrix\Main\Loader; use \Grain\Iiko\Restaurants; Loc::loadMessages(__FILE__); class Size { const basketPropCode = 'GRAIN.IIKO.S'; public static function get($element, \Bitrix\Sale\BasketItem $item=null) { $sizes = array(); static $cache = array(); if(!is_array($element) || !is_array($element['PROPERTIES']) || count($element['PROPERTIES'])<=0) { $elementId = is_array($element) && isset($element['ID'])?intval($element['ID']):intval($element); if($elementId<=0 || !Loader::includeModule('iblock')) return $sizes; if(array_key_exists($elementId, $cache)) { $element = $cache[$elementId]; } else { $result = \Bitrix\Iblock\ElementTable::getList(array( 'filter' => array('=ID'=>$elementId), 'limit' => 1, 'select' => array("ID", "IBLOCK_ID"), )); if(!$el=$result->fetch()) return $sizes; $resultElement = \CIBlockElement::GetList(array(), array( "IBLOCK_ID" => $el['IBLOCK_ID'], "=ID" => $el['ID'] ), false, false, array( "ID", "IBLOCK_ID", "PROPERTY_sizes") ); if (!$ob = $resultElement->GetNextElement()) return $sizes; $element = $ob->GetFields(); $element['PROPERTIES'] = array('sizes'=>$ob->GetProperty('sizes')); $cache[$elementId] = $element; } } if(!is_array($element['PROPERTIES']['sizes']) || !is_array($element['PROPERTIES']['sizes']['~VALUE'])) return $sizes; foreach($element['PROPERTIES']['sizes']['~VALUE'] as $sz) { try { $size = Json::decode(trim($sz)); $sizes[] = $size; } catch (\Exception $e) { //$e->getMessage(); } } if($item) { $basketProps = self::getBasketPropertyValues($item); $hasSize = false; foreach($sizes as &$size) { if($basketProps[self::basketPropCode]==$size['sizeId']) { $hasSize = true; $size['selected'] = true; } else { $size['selected'] = false; } } unset($size); if(!$hasSize) { foreach($sizes as &$size) { if($size['s']['isDefault']) { $size['selected'] = true; $hasSize = true; } } unset($size); } if(!$hasSize) { foreach($sizes as &$size) { $size['selected'] = true; $hasSize = true; break; } unset($size); } } return $sizes; } public static function getDefaultSelected($productId) { $defaultSizeId = self::getDefaultId($productId); $sizes = self::get($productId); foreach($sizes as &$size) $size['selected'] = $size['sizeId']==$defaultSizeId; unset($size); return $sizes; } public static function getSelectedSize($element, \Bitrix\Sale\BasketItem $item) { $sizes = self::get($element,$item); foreach($sizes as $size) { if(!$size['selected']) continue; return $size; } return null; } public static function setSize(\Bitrix\Sale\BasketItem $item,$sizeId) { $sizes = self::get($item->getProductId()); $currentSize = false; foreach($sizes as $size) { if($size['sizeId']!=$sizeId) continue; $currentSize = $size; break; } if($currentSize) { $name = $currentSize['sizeId']; foreach($sizes as $size) { if($size['sizeId']!=$currentSize['sizeId']) continue; $name = $size['s']['name']; break; } self::saveBasketProperty($item,self::basketPropCode,$currentSize['sizeId'],$name); } else { self::deleteBasketProperty($item,self::basketPropCode); } return $changeModifiersQuantity; } public static function getBasketPropertyValue(\Bitrix\Sale\BasketItem $item,$code) { foreach ($item->getPropertyCollection() as $property) { if($property->getField('CODE')!=$code) continue; return $property->getField('VALUE'); } return false; } public static function getBasketPropertyValues(\Bitrix\Sale\BasketItem $item) { $values = array(); foreach ($item->getPropertyCollection() as $property) $values[$property->getField('CODE')] = $property->getField('VALUE'); return $values; } public static function saveBasketProperty(\Bitrix\Sale\BasketItem $item,$code,$value=false,$name=false,$sort=false,$xmlId=false) { foreach ($item->getPropertyCollection() as $property) { if($property->getField('CODE')!=$code) continue; if($value!==false) $property->setField('VALUE', $value); if($name!==false) $property->setField('NAME', $name); if($sort!==false) $property->setField('SORT', $sort); if($xmlId!==false) $property->setField('XML_ID', $xmlId); return; } $property = $item->getPropertyCollection()->createItem(); $property->setField('CODE', strval($code)); $property->setField('VALUE', strval($value)); $property->setField('NAME', strval($name)); $property->setField('SORT', $sort!==false?$sort:'100'); if($xmlId!==false) $property->setField('XML_ID', $xmlId); } public static function deleteBasketProperty(\Bitrix\Sale\BasketItem $item,$code) { foreach ($item->getPropertyCollection() as $property) { if($property->getField('CODE')!=$code) continue; $property->delete(); return; } } public static function getDefault($productId) { $sizes = self::get($productId); foreach($sizes as $size) { if(!isset($size['s']['isDefault']) || !$size['s']['isDefault']) continue; return $size; } foreach($sizes as $size) { return $size; } return null; } public static function getDefaultId($productId) { $defaultSize = self::getDefault($productId); if(is_array($defaultSize) && isset($defaultSize['sizeId'])) return $defaultSize['sizeId']; return null; } public static function compact($productSizesFull) { $size = array(); if(!is_array($productSizesFull)) return array(); foreach($productSizesFull as $sizeFull) { if(!isset($sizeFull['price']['currentPrice'])) continue; $size[$sizeFull['sizeId']] = array( 'name' => isset($sizeFull['s']['name'])?$sizeFull['s']['name']:'', 'price' => floatval($sizeFull['price']['currentPrice']), ); } return $size; } }