【问题标题】:In Magento Admin: How to add "Color" attribute coloumn under "Products in Cart" Report在 Magento Admin 中:如何在“购物车中的产品”报告下添加“颜色”属性列
【发布时间】:2013-04-09 13:09:57
【问题描述】:

在 Magento 管理员中:在报告/购物车/购物车中的产品下。
我想在“购物车中的产品”网格下添加“颜色”属性列。假设网店中的所有产品都是可配置产品。
IE;如果从网上商店 - 客户选择带有颜色“红色”选项的测试产品(可配置产品),则该属性值应显示在报告中。

请提出实现这一目标的最佳方法!

【问题讨论】:

  • 看看@/app/code/core/Mage/Reports/Model/Resource/Quote/Collection.php, /app/code/core/Mage/Adminhtml/Block/Report/Shopcart/产品/Grid.php
  • @R.S:虽然我已经研究过你建议的路径中的代码,但仍然不知道我的集合(数组)将如何返回添加的属性值(“红色”)??跨度>

标签: magento


【解决方案1】:

复制 \app\code\core\Mage\Reports\Model\Resource\Quote\Collection.php 并粘贴到 app\code\local\Mage\Reports\Model\Resource\Quote\Collection.php 中

重写 公共函数 prepareForProductsInCarts() 函数

public function prepareForProductsInCarts()
    {
        $productEntity          = Mage::getResourceSingleton('catalog/product_collection');
        $productAttrName        = $productEntity->getAttribute('name');
        $productAttrNameId      = (int) $productAttrName->getAttributeId();
        $productAttrNameTable   = $productAttrName->getBackend()->getTable();
        $productAttrPrice       = $productEntity->getAttribute('price');
        $productAttrPriceId     = (int) $productAttrPrice->getAttributeId();
        $productAttrPriceTable  = $productAttrPrice->getBackend()->getTable();

        $ordersSubSelect = clone $this->getSelect();
        $ordersSubSelect->reset()
            ->from(
                array('oi' => $this->getTable('sales/order_item')),
                array(
                   'orders' => new Zend_Db_Expr('COUNT(1)'),
                   'product_id'))
            ->group('oi.product_id');

        $this->getSelect()
            ->useStraightJoin(true)
            ->reset(Zend_Db_Select::COLUMNS)
            ->joinInner(
                array('quote_items' => $this->getTable('sales/quote_item')),
                'quote_items.quote_id = main_table.entity_id',
                null)
            ->joinInner(
                array('e' => $this->getTable('catalog/product')),
                'e.entity_id = quote_items.product_id',
                null)
            ->joinInner(
                array('product_name' => $productAttrNameTable),
                "product_name.entity_id = e.entity_id AND product_name.attribute_id = {$productAttrNameId}",
                array('name'=>'product_name.value'))
            ->joinInner(
                array('product_price' => $productAttrPriceTable),
                "product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
                array('price' => new Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')))
            ->joinLeft(
                array('order_items' => new Zend_Db_Expr(sprintf('(%s)', $ordersSubSelect))),
                'order_items.product_id = e.entity_id',
                array()
            )
            ->columns('e.*')
            ->columns(array('carts' => new Zend_Db_Expr('COUNT(quote_items.item_id)')))
            ->columns('order_items.orders')
            ->where('main_table.is_active = ?', 1)
            ->group('quote_items.product_id');

        return $this;
    }

并用下面的函数替换。

public function prepareForProductsInCarts()
    {
        $productEntity          = Mage::getResourceSingleton('catalog/product_collection');
        $productAttrName        = $productEntity->getAttribute('name');
        $productAttrNameId      = (int) $productAttrName->getAttributeId();
        $productAttrNameTable   = $productAttrName->getBackend()->getTable();
        $productAttrPrice       = $productEntity->getAttribute('price');
        $productAttrPriceId     = (int) $productAttrPrice->getAttributeId();
        $productAttrPriceTable  = $productAttrPrice->getBackend()->getTable();

        $ordersSubSelect = clone $this->getSelect();
        $ordersSubSelect->reset()
            ->from(
                array('oi' => $this->getTable('sales/order_item')),
                array(
                   'orders' => new Zend_Db_Expr('COUNT(1)'),
                   'product_id'))
            ->group('oi.product_id');

        $this->getSelect()
            ->useStraightJoin(true)
            ->reset(Zend_Db_Select::COLUMNS)
            ->joinInner(
                array('quote_items' => $this->getTable('sales/quote_item')),
                'quote_items.quote_id = main_table.entity_id',
                null)
            ->joinInner(
                array('e' => $this->getTable('catalog/product')),
                'e.entity_id = quote_items.product_id',
                null)
            ->joinInner(
                array('product_name' => $productAttrNameTable),
                "product_name.entity_id = e.entity_id AND product_name.attribute_id = {$productAttrNameId}",
                array('name'=>'product_name.value'))
            ->joinInner(
                array('product_price' => $productAttrPriceTable),
                "product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
                array('price' => new Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')))

            ### Newly added script ###
            ->joinLeft(
            array('cpei' => 'catalog_product_entity_int'),
            'cpei.entity_id = e.entity_id AND cpei.attribute_id = 92',
            array()
            )
            ->joinLeft(
            array('eaov' => 'eav_attribute_option_value'),
            'eaov.option_id = cpei.value',
            array('color'=>'eaov.value')
           )
           ### End script ###

            ->joinLeft(
                array('order_items' => new Zend_Db_Expr(sprintf('(%s)', $ordersSubSelect))),
                'order_items.product_id = e.entity_id',
                array()
            )
            ->columns('e.*')
            ->columns(array('carts' => new Zend_Db_Expr('COUNT(quote_items.item_id)')))
            ->columns('order_items.orders')
            ->where('main_table.is_active = ?', 1)
            ->group('quote_items.product_id');

        return $this;
    }

这里 cpei.attribute_id = 92 是我的“颜色”属性 id。您可以根据需要进行更改。

也将下面的代码放入 \app\code\core\Mage\Adminhtml\Block\Report\Shopcart\Product\grid.php 文件。

$this->addColumn('color', array(
            'header'    =>Mage::helper('reports')->__('Color'),
            'index'     =>'color',

是的,我们可以将 grid.php 文件从核心复制到本地文件夹。 在 magento 1.7 中工作和测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2011-05-02
    • 2016-12-19
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多