Как автоматом указать характеристики только те что есть в товарах для категории?

Как в категории указать все характеристики что используются в товарах этой категории? в minishop2

[РЕШЕНО]

Суть в том чтобы в админке появились только те характеристики у товара которые есть, это значит поставить все уникальные характеристики для категории, нужно было именно такое решение, сделал сам. А если не нужно их указывать в админке для редактирования, то да проще будет вывести их список через pdoResources указав класс msCategoryOption

Буду рад кто исправит это код(КОД РАБОЧИЙ), чтобы был чище, так как делался на быструю руку решить задачу, а так же не было знаний для реализации этой задачи.

<?php

function prepareSqlQuery($sqlQuery){
	global $modx;
	return $modx->query($sqlQuery);
}


function extractResultsFromQueryObject($sqlQueryResult, $itemsToRetreive, $multiDimensionRetreive=false){
	global $modx;
	$results = array();

	if (is_object($sqlQueryResult)) {
		while ($row = $sqlQueryResult->fetch(PDO::FETCH_ASSOC)) {
			if($multiDimensionRetreive){
				foreach($itemsToRetreive as $key => $value){
					array_push($results, $row[$key], $row[$value]);
				}	    		
			} else {
				foreach($itemsToRetreive as $itemToRetreive){
					array_push($results, $row[$itemToRetreive]);
				}
			}
		}
	}
	return $results;
}


$prepareSqlQueryOfRetreiveCategories = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msCategory"');
$categoriesIds = extractResultsFromQueryObject($prepareSqlQueryOfRetreiveCategories, array('id'));

if(count($categoriesIds) <= 0){
	return 'Have no categories';
}


foreach($categoriesIds as $categoryId){

	$productsQueryObject = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msProduct" AND modx_site_content.parent = "'.$categoryId.'"');
	$productsIds = extractResultsFromQueryObject($productsQueryObject, array('id'));

	if(count($productsIds) <= 0) continue;

	$productsIds = implode(",", $productsIds);

	$distinctProductOptionsSqlQuery = prepareSqlQuery('SELECT DISTINCT modx_ms2_product_options.key FROM `modx_ms2_product_options` WHERE modx_ms2_product_options.product_id IN ('.$productsIds.")");
	$distinctProductOptions = extractResultsFromQueryObject($distinctProductOptionsSqlQuery, array('key'));

	if(count($distinctProductOptions) <= 0) continue;

	$uniqueOptions = "";
	$uniqueOptionsLength = count($distinctProductOptions);
	foreach ($distinctProductOptions as $idx => $value) {
		if($idx >= $uniqueOptionsLength - 1){
			$uniqueOptions .= "'$value'";
		} else{
			$uniqueOptions .= "'$value',";
		}
	}


	$uniqueOptionsIdsSqlQuery = prepareSqlQuery('SELECT modx_ms2_options.id,modx_ms2_options.key FROM `modx_ms2_options` WHERE modx_ms2_options.key IN ('.$uniqueOptions.')');
	$uniqueOptionsIds = extractResultsFromQueryObject($uniqueOptionsIdsSqlQuery, array('id'));

	if(count($uniqueOptionsIds) <= 0) continue;

	/* #################################### ##################################### */
	foreach($uniqueOptionsIds as $idx => $uniqueOptionId){
		if (!$cop = $modx->getObject('msCategoryOption', array('option_id' => $uniqueOptionId, 'category_id' => $categoryId))) {
			$table = $modx->getTableName('msCategoryOption');
			$sql = "INSERT INTO {$table} (`option_id`,`category_id`,`active`) VALUES ({$uniqueOptionId}, {$categoryId}, 1);";
			$stmt = $modx->prepare($sql);
			$stmt->execute();
		} else {
			$q = $modx->newQuery('msCategoryOption');
			$q->command('UPDATE');
			$q->where(array('option_id' => $uniqueOptionId, 'category_id' => $categoryId));
			$q->set(array('active' => 1));
			$q->prepare();
			$q->stmt->execute();
		}            
		
	}
	/* #################################### ##################################### */

	echo 'Done';
}
Vladimir
06 апреля 2023, 09:35
modx.pro
1
311
0

Комментарии: 2

Артур Шевченко
06 апреля 2023, 19:03
0
Наверное вывести их список через pdoResources указав класс msCategoryOption
    Vladimir
    08 апреля 2023, 13:53
    2
    0
    [РЕШЕНО] Суть в том чтобы в админке появились только те характеристики у товара которые есть, это значит поставить все уникальные характеристики для категории, нужно было именно такое решение, сделал сам. А если не нужно их указывать в админке для редактирования, то да проще будет вывести их список через pdoResources указав класс msCategoryOption

    Буду рад кто исправит это код(КОД РАБОЧИЙ), чтобы был чище, так как делался на быструю руку решить задачу, а так же не было знаний для реализации этой задачи.
    <?php
    
    function prepareSqlQuery($sqlQuery){
    	global $modx;
    	return $modx->query($sqlQuery);
    }
    
    
    function extractResultsFromQueryObject($sqlQueryResult, $itemsToRetreive, $multiDimensionRetreive=false){
    	global $modx;
    	$results = array();
    
    	if (is_object($sqlQueryResult)) {
    		while ($row = $sqlQueryResult->fetch(PDO::FETCH_ASSOC)) {
    			if($multiDimensionRetreive){
    				foreach($itemsToRetreive as $key => $value){
    					array_push($results, $row[$key], $row[$value]);
    				}	    		
    			} else {
    				foreach($itemsToRetreive as $itemToRetreive){
    					array_push($results, $row[$itemToRetreive]);
    				}
    			}
    		}
    	}
    	return $results;
    }
    
    
    $prepareSqlQueryOfRetreiveCategories = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msCategory"');
    $categoriesIds = extractResultsFromQueryObject($prepareSqlQueryOfRetreiveCategories, array('id'));
    
    if(count($categoriesIds) > 0){
    
    	foreach($categoriesIds as $categoryId){
    
    		$productsQueryObject = prepareSqlQuery('SELECT modx_site_content.id FROM `modx_site_content` WHERE modx_site_content.class_key = "msProduct" AND modx_site_content.parent = "'.$categoryId.'"');
    		$productsIds = extractResultsFromQueryObject($productsQueryObject, array('id'));
    
    		if(count($productsIds) > 0){
    			$productsIds = implode(",", $productsIds);
    
    			$distinctProductOptionsSqlQuery = prepareSqlQuery('SELECT DISTINCT modx_ms2_product_options.key FROM `modx_ms2_product_options` WHERE modx_ms2_product_options.product_id IN ('.$productsIds.")");
    			$distinctProductOptions = extractResultsFromQueryObject($distinctProductOptionsSqlQuery, array('key'));
    
    			$uniqueOptions = "";
    			$uniqueOptionsLength = count($distinctProductOptions);
    			foreach ($distinctProductOptions as $idx => $value) {
    				if($idx >= $uniqueOptionsLength - 1){
    					$uniqueOptions .= "'$value'";
    				} else{
    					$uniqueOptions .= "'$value',";
    				}
    			}
    
    
    			$uniqueOptionsIdsSqlQuery = prepareSqlQuery('SELECT modx_ms2_options.id,modx_ms2_options.key FROM `modx_ms2_options` WHERE modx_ms2_options.key IN ('.$uniqueOptions.')');
    			$uniqueOptionsIds = extractResultsFromQueryObject($uniqueOptionsIdsSqlQuery, array('id'));
    
    
    			/* #################################### ##################################### */
    			foreach($uniqueOptionsIds as $idx => $uniqueOptionId){
    				if (!$cop = $modx->getObject('msCategoryOption', array('option_id' => $uniqueOptionId, 'category_id' => $categoryId))) {
    					$table = $modx->getTableName('msCategoryOption');
    					$sql = "INSERT INTO {$table} (`option_id`,`category_id`,`active`) VALUES ({$uniqueOptionId}, {$categoryId}, 1);";
    					$stmt = $modx->prepare($sql);
    					$stmt->execute();
    				} else {
    					$q = $modx->newQuery('msCategoryOption');
    					$q->command('UPDATE');
    					$q->where(array('option_id' => $uniqueOptionId, 'category_id' => $categoryId));
    					$q->set(array('active' => 1));
    					$q->prepare();
    					$q->stmt->execute();
    				}            
    				
    			}
    			/* #################################### ##################################### */
    			echo 'Done';
    		}
    	}
    }
      Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
      2