0
我想使用url http://mydomain.test/categoru-name.html?cat=9,10,40。我需要使用類別ID(9,10,40)從類別類別名稱和子類別中獲取所有產品。Magento - 按類別獲取產品ID
我該怎麼做?也許有這樣的延伸。
我想使用url http://mydomain.test/categoru-name.html?cat=9,10,40。我需要使用類別ID(9,10,40)從類別類別名稱和子類別中獲取所有產品。Magento - 按類別獲取產品ID
我該怎麼做?也許有這樣的延伸。
使用以下代碼向Magento根添加一個php頁面(categoru-name.php)。你需要做一些修改才能使其工作。
/** START Include Magento **/
define('MAGENTO_ROOT', getcwd());
$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
include $compilerConfig;
}else{exit;}
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
if (file_exists($mageFilename)) {
require_once $mageFilename;
}else{exit;}
Mage::app();
/** END Include Magento **/
// This is not a complete code. You need to fill in.
// Add from query paramter
$categoryIds = array("Insert IDs from URL");
$categoryIdList = array();
array_push($categoryIdList,$categoryIds); //Collect child category IDs
foreach($categoryIds as $Catid){
$cat = Mage::getModel('catalog/category')->load($Catid);
$subCats = $cat->getChildren();
$arrSubCats = explode(',',$subCats);
array_push($categoryIdList,$arrSubCats); //Collect child category IDs
// Go another level down
foreach($arrSubCats as $Catid){
$cat = Mage::getModel('catalog/category')->load($Catid);
$subSubCats = $cat->getChildren();
$arrSubSubCats = explode(',',$subSubCats);
array_push($categoryIdList,$arrSubSubCats); //Collect child category IDs
// Go another level down
// Another foreach loop for $arrSubSubCats
}
}
// Now we have all categoryIds in the tree.
// Get all products.
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIdList))