如何按库存数量获取产品?


10

我有以下代码:

umask(0);
Mage::app();
$category =new Mage_Catalog_Model_Category();
$category->load($cid);

if ($status == "2") {
    $products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*') 
    ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
    $products->load();
}

if ($status == "1") {
    $products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*');
    $products->load();  
}

我想添加一个库存数量过滤器来过滤产品,我尝试过:

    $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*');
    **->addAttributeToFilter('qty', array("gt" => 0));**

但是不成功,有什么想法吗?

Answers:


30

您需要使用cataloginventory / stock_item表创建联接以获取产品的库存数量,您可以使用:

$collection = Mage::getModel('catalog/product')
     ->getCollection()
     ->addAttributeToSelect('*')
     ->joinField('qty',
         'cataloginventory/stock_item',
         'qty',
         'product_id=entity_id',
         '{{table}}.stock_id=1',
         'left'
     )->addAttributeToFilter('qty', array('gt' => 0));

'{{table}}。stock_id = 1'这一行的含义是什么?
hkguile

1
简而言之,这意味着您正在从默认库存项目装载数量。{{table}}在此情况下转换为cataloginventory_stock_item表,stock_id = 1表示这是默认库存项目。
Danny Dev Nz 2014年

1
我正在$coll = Mage::getResourceModel('catalog/product_collection')使用您的加入字段和addAttributeToFilter。该集合正在加载就好。但是过滤器不起作用:->addAttributeToFilter('qty', array("gt" => 0))。看$coll->getSelect()qtyWHERE子句中没有引用。知道为什么吗?
Buttle Butkus 2015年

嗨@vladimir如何在soap api中传递以上代码
shivashankar m

6

单程:

$stockIds = Mage::getModel('cataloginventory/stock_item')
    ->getCollection()
    ->addQtyFilter('=', 30) //can be ->addQtyFilter('>=', 30), depending on requirement
    ->getAllIds();

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addIdFilter($stockIds)
    ->setPageSize(10);

第二种方式:

$oCollection = Mage::getModel('catalog/product')
->getCollection()
->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
)
->addAttributeToFilter('qty', array('eq' => 30));

3

@TBI Infotech的答案中的“ FIRST WAY”无效,因为该->getAllIds()方法返回的是库存ID,而不是产品ID。相反,您需要添加它;

$stockIds = Mage::getModel('cataloginventory/stock_item')
->getCollection()
->addQtyFilter('>=', 30); 
//->getAllIds();

foreach($stockIds as $stock) {
   $idarray[] = $stock->getProductId();
}

$products = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($idarray)
->setPageSize(10);

这种逻辑非常清晰,简单,没有魔术。我想知道效率/速度/速度如何$stock->getProductId()
ahnbizcad

2

最优雅的方式:

$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*');
Mage::getSingleton('cataloginventory/stock')
    ->addInStockFilterToCollection($products);

1

迟到的答案,尽管需要在此代码上做一些工作,所以就到这里了。它只需要几个连接,即可用于可配置和简单的产品,还没有针对捆绑软件进行过测试。

$product_collection->getSelect()->joinLeft(
        array(
            'css1' => $products->getTable('cataloginventory/stock_status')
        ),
        'e.entity_id = css1.product_id and css1.stock_id =1 AND css1.website_id='.Mage::app()->getWebsite()->getId(),
        ''
    )->joinLeft(
        array(
            'cpsl' => $products->getTable('catalog/product_super_link')
        ),
        'cpsl.parent_id = e.entity_id',
        ''
    )->joinLeft(
        array(
            'css2' => $products->getTable('cataloginventory/stock_status')
        ),
        'css2.product_id = cpsl.product_id and css1.stock_id =1 AND css1.website_id='.Mage::app()->getWebsite()->getId(),
        ''
    )->joinLeft(
        array(
            'cpei' => 'catalog_product_entity_int'
        ),
        "cpei.entity_id = cpsl.product_id AND cpei.entity_type_id = $entity_type_id and cpei.attribute_id = $attribute_id AND (cpei.value IS NULL OR cpei.value = " . Mage_Catalog_Model_Product_Status::STATUS_ENABLED . ")",
        ''
    )->group(
        'e.entity_id'
    )->columns(
        array(
            'qty' => new Zend_Db_Expr ("IF(`e`.`type_id` = 'simple', `css1`.qty, sum(css2.qty))")
        )
    )->having(
        'qty > 0'
);

我今天过得很慢...是$ products = Mage :: getModel('core / resource'); 和$ product_collection = Mage :: getModel('catalog / product')-> getCollection();
艾夫林
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.