Created
March 10, 2021 15:44
-
-
Save Isa3v/2b4f24239fda262998c98f41f92fbf28 to your computer and use it in GitHub Desktop.
Остатки на складе товаров bitrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
use Bitrix\Main\Loader; | |
Loader::includeModule("iblock"); | |
Loader::includeModule("sale"); | |
function fputcsvNew($csv_arr, $delimiter = ';', $enclosure = '"') { | |
if (!is_array($csv_arr)) { | |
return(false); | |
} | |
for ($i = 0, $n = count($csv_arr); $i < $n; $i ++) { | |
if (!is_numeric($csv_arr[$i])) { | |
$csv_arr[$i] = $enclosure.str_replace($enclosure, $enclosure.$enclosure, $csv_arr[$i]).$enclosure; | |
} | |
if (($delimiter === '.') && (is_numeric($csv_arr[$i]))) { | |
$csv_arr[$i] = $enclosure.$csv_arr[$i].$enclosure; | |
} | |
} | |
$str = implode($delimiter, $csv_arr).PHP_EOL; | |
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/store_item.csv', $str, FILE_APPEND); | |
echo $str; | |
return $str; | |
} | |
$entityPropsSingle = \Bitrix\Main\Entity\Base::compileEntity( | |
'PropertiesClass', | |
[ | |
'IBLOCK_ELEMENT_ID' => ['data_type' => 'integer'], | |
'IBLOCK_PROPERTY_ID' => ['data_type' => 'integer'], | |
'VALUE' => ['data_type' => 'string'], | |
], | |
['table_name' => 'b_iblock_element_property'] | |
); | |
$products = \Bitrix\Catalog\ProductTable::getList(array( | |
'filter' => ['IBLOCK_ELEMENT.IBLOCK_ID' => 64, '!Store.STORE_ID' => false], | |
'select' => ['NAME'=>'IBLOCK_ELEMENT.NAME', 'XML_ID' => 'IBLOCK_ELEMENT.XML_ID', 'ARTICLE' => 'PropValue.VALUE', 'ID', 'Store.STORE.TITLE', 'Store.*'], | |
'runtime' => [ | |
new \Bitrix\Main\Entity\ReferenceField( | |
'PropValue', | |
$entityPropsSingle->getDataClass(), | |
\Bitrix\Main\ORM\Query\Join::on('this.IBLOCK_ELEMENT.ID', 'ref.IBLOCK_ELEMENT_ID') | |
->where("ref.IBLOCK_PROPERTY_ID", "300") | |
), | |
new \Bitrix\Main\Entity\ReferenceField( | |
'Store', | |
\Bitrix\Catalog\StoreProductTable::class, | |
\Bitrix\Main\ORM\Query\Join::on('this.ID', 'ref.PRODUCT_ID') | |
) | |
] | |
)); | |
$arProduct = []; | |
$csvFile = new CCSVData(); | |
$csvFile->SetFieldsType('R'); | |
$csvFile->SetDelimiter(';'); | |
while($product = $products->fetch()) { | |
if(!in_array($product['ID'], array_keys($arProduct))){ | |
$arProduct[$product['ID']] = [ | |
'Название' => $product['NAME'], | |
'Артикул' => $product['ARTICLE'], | |
'xml_id' => $product['XML_ID'], | |
]; | |
} | |
$arProduct[$product['ID']][$product['CATALOG_PRODUCT_Store_STORE_TITLE']] = $product['CATALOG_PRODUCT_Store_AMOUNT']; | |
} | |
#print_r($arProduct); | |
// to CSV | |
fputcsvNew(array_keys(reset($arProduct))); | |
foreach($arProduct as $product) { | |
fputcsvNew($product); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment