3

如何在产品网格中添加自定义 SQL 调用。

这是我到目前为止所拥有的:

$collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id');

$collection->joinField(
            'quantity_in_stock',
            'advancedinventory',
            'quantity_in_stock',
            'product_id=entity_id',
            'advancedinventory.place_id=1',
            'inner'
        );

$this->addColumn('quantity_in_stock',
        array(
            'header'=> Mage::helper('catalog')->__('Custom Column'),
            'width' => '80px',
            'type' => 'number',
            'index' => 'quantity_in_stock'
    ));

但这似乎不起作用,我需要从表advancedinventory中获取product_id该实体的 id 并且place_id始终等于 1 的值。

任何人都可以提供任何帮助吗?

4

3 回答 3

1

解决了,

要修复它,我必须将集合设为:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('attribute_set_id')
            ->addAttributeToSelect('type_id')
            ->joinField('quantity_in_stock', 'mage_advancedinventory', 'quantity_in_stock', 'product_id=entity_id', 'place_id=1', 'left');`

$this->addColumn('quantity_in_stock',
     array(
            'header'=> Mage::helper('catalog')->__('Custom Column'),
            'width' => '80px',
            'type' => 'number',
            'index' => 'quantity_in_stock'
     )
);
于 2014-07-02T16:20:15.610 回答
0

表的名称应该是为 magento getResourceModel 定义的。例如:目录/类别、目录/国家名称...

如果它不起作用,您仍然可以尝试使用 $collection->getSelect()->joinLeft(...) 编辑选择。

于 2014-07-02T14:12:28.553 回答
0

你可以像下面这样使用

这是一个简单的示例,您可以将其应用于您的收藏

$collection = Mage::getResourceModel('catalog/product_collection')

                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('price')
                    ->addAttributeToSelect('status')
                    ->addAttributeToSelect('visibility')
                    ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                    ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                    ->addAttributeToFilter('visibility', array('neq' => 1));


   $collection->getSelect()->join(array('abvinv' => "advancedinventory"), "e.entity_id = abvinv.product_id", array('abvinv.*'))

在这里,我假设表product_id中可用的advancedinventory

希望这肯定对你有用。

于 2014-06-30T10:30:40.467 回答