php Magento 获取按“位置”字段排序的特定类别中的所有产品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14318488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:04:30  来源:igfitidea点击:

Magento get all products in a certain category ordered by the 'Position' field

phpmagentomagento-1.7

提问by Gga

I am simply trying to get all products that belong to a certain category IDin the Positionorder they are set in the back end.

我只是想按照后端设置category IDPosition顺序获取属于某个特定产品的所有产品。

I seemed to have tried every example available to no avail.

我似乎尝试了所有可用的示例都无济于事。

The basic code I'm working with is as follows (external php file that loads magento manually):

我正在使用的基本代码如下(手动加载 magento 的外部 php 文件):

// Load Magento
require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
umask(0);
Mage::app();
// set Store ID
$store_id = Mage::app()->getStore()->getStoreId();
// set Cat ID
$cat_id = 345;


$cat = Mage::getModel('catalog/product')->setId(345);

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addCategoryFilter($cat)
    ->addAttributeToSelect("*")
    ->setOrder('name','asc')
    ->load();

foreach($products as $p) {
    var_dump($p->getName());
}

How might I achieve this?

我怎么能做到这一点?

回答by Gga

This was what finally worked:

这就是最终奏效的:

$cat_id = 345;

$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = $category->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);

foreach ($collection as $product) {
    var_dump( $product->getName() );
}

回答by theycallmepepper

$product_position_array = Mage::getModel('catalog/category')->load($CategoryID)->getProductsPosition();

This should return an array of $product_id = $position.

这应该返回一个$product_id = $position.