如何将子查询作为产品集合中的新属性运行?

时间:2013-09-03 16:27:23

标签: php magento

我想在管理员中为产品网格创建一个新列,用于计算管理员产品网格中与可配置产品相​​关的子产品数量。

我想出了子查询来进行计算:

(SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = {{attribute}}) AS children_qtys)

我覆盖了Mage_Adminhtml_Block_Catalog_Product_Grid :: _ prepareCollection(),并使用以下代码插入了我的属性:

      $getChildrenQtySum = '(SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = {{attribute}}) AS children_qtys)';
    $collection->addExpressionAttributeToSelect(
        'custom_qty',
        $getChildrenQtySum,
        'entity_id'
    );

我在Magento核心文件中找到的方法addExpressionAttributeToSelect(),似乎能够运行原始的mysql查询,但是当我加载网格页面时,我得到一个WSOD,并且在日志中我得到的是查询产品。在我看来,我的子查询被包装在“`”中,而不是使用我指定的别名(custom_qty)。

a:5:{i:0;s:747:"SELECT `e`.*, (SELECT SUM(children_qtys.qty) FROM (SELECT cisi.qty AS qty FROM catalog_product_super_link cpsa LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpsa.parent_id AND cisi.stock_id=1 WHERE cpsa.parent_id = e.entity_id) AS `children_qtys)`, `at_status`.`value` AS `status`, `at_visibility`.`value` AS `visibility` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`) AND (`at_status`.`attribute_id` = '96') AND (`at_status`.`store_id` = 0)
 INNER JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND (`at_visibility`.`attribute_id` = '102') AND (`at_visibility`.`store_id` = 0)

我做错了什么,有没有更好的方法去做我想要完成的事情?

1 个答案:

答案 0 :(得分:-1)

这是

的自定义代码

在config.xml中调用observer事件

 <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>                    
                    <NameSpace_Modulename_configurable>
                        <class>NameSpace_Modulename_Model_Observer</class>
                        <method>addConfigureList</method>
                    </NameSpace_Modulename_configurable>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>

这是模型文件夹中的Observer.php文件

<?php
class NameSpace_Modulename_Model_Observer
{       



public function addConfigureList(Varien_Event_Observer $observer)
{    
    $block = $observer->getEvent()->getBlock();
    if( ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid)  ) {
        $block->addColumnAfter('configure_list', array(
                'header'    => Mage::helper('modulename')->__('Associated Products'),
                'index'        => 'configure_list',
                'sortable'    => false,
                'filter' => false,
                'width' => '250px',
                'options'    => Mage::getSingleton('productsearchguide/system_config_source_category')->toOptionArrayForAssociated(),
                'renderer'    => 'productsearchguide/catalog_product_grid_render_product',
                'filter_condition_callback' => array($this, 'customfilterCall'),
        ),'name');
    }
}

public function customfilterCall($collection, $column)
    {
        $value = $column->getFilter()->getValue();
        $_product = Mage::getModel('catalog/product')->load($value);

        return $collection;
    }

}

并按照给定的班级位置添加此块文件

<?php
 class Namespace_Modulename_Block_Catalog_Product_Grid_Render_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $product = Mage::getModel('catalog/product')->load($row->getEntityId());
        if($product->getTypeId() == 'configurable') {
        $childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getUsedProducts(null,$product); 
        $allCats = '';
        foreach($childProducts as $key => $child) {
           $allCats .= $child->getName();
            if($key < count($childProducts)-1)
                $allCats.= ',<br />';
           }
           return $allCats;
        }
        return null;
    }

}

按照给定的班级位置添加模型文件

<?php
class Namespace_Modulename_Model_System_Config_Source_Product
{

    public function toOptionArrayForAssociated($addEmpty = true)
    {
        $options = array();
        foreach ($this->loadConfighurableProduct() as $product) {
            $options[$product['value']] =  $product['label'];
        }

        return $options;
    }   

    public function loadConfighurableProduct()
    {
        $store = Mage::app()->getFrontController()->getRequest()->getParam('store', 0);
        $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('attribute_set_id')
                    ->addAttributeToSelect('type_id'); 
    }
}

享受!!欢呼声........