Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Magento-AdminHtml产品网格中的自定义列,其中包含自定义输出/逻辑?_Php_Sql_Magento - Fatal编程技术网

Php Magento-AdminHtml产品网格中的自定义列,其中包含自定义输出/逻辑?

Php Magento-AdminHtml产品网格中的自定义列,其中包含自定义输出/逻辑?,php,sql,magento,Php,Sql,Magento,我想在Magento的管理区域的“目录产品”网格中添加一个新列 新列将被称为“当前折扣”?还是特价商品?当产品的价格高于其特价(即该产品正在销售)时,将显示“是” 到目前为止,我已经能够通过下面的代码将特价列添加到网格中: $collection->joinAttribute('special_price','catalog_product/special_price','entity_id',null',left',$store->getId()) 但我真正需要的是另一个专栏,它实际上执行某种

我想在Magento的管理区域的“目录产品”网格中添加一个新列

新列将被称为“当前折扣”?还是特价商品?当产品的
价格
高于其
特价
(即该产品正在销售)时,将显示“是”

到目前为止,我已经能够通过下面的代码将
特价
列添加到网格中:

$collection->joinAttribute('special_price','catalog_product/special_price','entity_id',null',left',$store->getId())

但我真正需要的是另一个专栏,它实际上执行某种逻辑来比较特价,如果打折,则打印“是”,如果不打折,则打印“否”

我已经添加了该列,但现在它显然是空的,因为它的
索引
与任何真实的数据源都不对应:

$this->addColumn('is_sale_item',
    array(
        'header'=> Mage::helper('catalog')->__('Sale Item?'),
        'type'  => 'options',
        'index' => 'is_sale_item', // <--- HOW DO I POPULATE THIS INDEX?
        'options' => array( 0 => 'No', 1 => 'Yes')
));
$this->addColumn('is\u sale\u item',
排列(
'header'=>Mage::helper('catalog')->(Sale Item?'),
'类型'=>'选项',
“索引”=>“是销售项目”,//数组(0=>“否”,1=>“是”)
));

我将如何实现这一点?我将把“is_sale_item”计算的逻辑放在哪里,并使其成为一个“真实”索引?

我设法实现了这一点,但用了一种略显粗糙的方式。如果有人知道更好的方法,请发表评论

1。使用自定义渲染器和过滤器回调将自定义列添加到列列表中

$this->addColumn('is_sale_item',
            array(
                'header'=> Mage::helper('catalog')->__('Sale Item?'),
                'type'  => 'options',
                'index' => 'is_sale_item',
                'options' => array( "no" => 'No', "yes" => 'Yes'),
                'renderer'  => 'Mage_Adminhtml_Catalog_Product_Renderer_Sale',
                'filter_condition_callback' => array($this, '_filterIsSaleItem'),
        ));
2。为新的自定义属性创建自定义渲染器

我使用一个自定义渲染器来执行逻辑,以测试该项是否已减少。同样,这可能不是正确的方法。在带有
Grid.php
的文件夹中,创建
Renderer/Sale.php

<?php
class Mage_Adminhtml_Catalog_Product_Renderer_Sale extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $_price        = $row->getData("price");
        $_specialPrice = $row->getData("special_price");
        if ($_specialPrice && $_price > $_specialPrice) {
            return "Yes";
        } else {
            return "No";
        }
    }
}
?>
protected function _filterIsSaleItem($collection, $column)
    {   
        $value = $column->getFilter()->getValue();

        if (!$value) {
            return $this;
        }

        $store = $this->_getStore();
        $this->getCollection()->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
        $this->getCollection()->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left', $store->getId());

        if ($value == "yes") {
            $this->getCollection()->getSelect()->where("_table_price.value >  _table_special_price.value");
        } else {
            $this->getCollection()->getSelect()->where("_table_price.value <= _table_special_price.value OR _table_special_price.value IS NULL");
        }

        return $this;

    }

我设法实现了这一点,但用了一种稍微粗糙的方式。如果有人知道更好的方法,请发表评论

1。使用自定义渲染器和过滤器回调将自定义列添加到列列表中

$this->addColumn('is_sale_item',
            array(
                'header'=> Mage::helper('catalog')->__('Sale Item?'),
                'type'  => 'options',
                'index' => 'is_sale_item',
                'options' => array( "no" => 'No', "yes" => 'Yes'),
                'renderer'  => 'Mage_Adminhtml_Catalog_Product_Renderer_Sale',
                'filter_condition_callback' => array($this, '_filterIsSaleItem'),
        ));
2。为新的自定义属性创建自定义渲染器

我使用一个自定义渲染器来执行逻辑,以测试该项是否已减少。同样,这可能不是正确的方法。在带有
Grid.php
的文件夹中,创建
Renderer/Sale.php

<?php
class Mage_Adminhtml_Catalog_Product_Renderer_Sale extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $_price        = $row->getData("price");
        $_specialPrice = $row->getData("special_price");
        if ($_specialPrice && $_price > $_specialPrice) {
            return "Yes";
        } else {
            return "No";
        }
    }
}
?>
protected function _filterIsSaleItem($collection, $column)
    {   
        $value = $column->getFilter()->getValue();

        if (!$value) {
            return $this;
        }

        $store = $this->_getStore();
        $this->getCollection()->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
        $this->getCollection()->joinAttribute('special_price', 'catalog_product/special_price', 'entity_id', null, 'left', $store->getId());

        if ($value == "yes") {
            $this->getCollection()->getSelect()->where("_table_price.value >  _table_special_price.value");
        } else {
            $this->getCollection()->getSelect()->where("_table_price.value <= _table_special_price.value OR _table_special_price.value IS NULL");
        }

        return $this;

    }
这将有助于:

$this->addColumn('special_price', array(
    'header'    => Mage::helper('catalog')->__('Special Price'),
    'type'      => 'options',
    'index'     => 'special_price',
    'options' => Mage::getSingleton('eav/entity_attribute_source_boolean')->getOptionArray(),
));
@怪人我希望你觉得这个有用:)。

这应该会有帮助:

$this->addColumn('special_price', array(
    'header'    => Mage::helper('catalog')->__('Special Price'),
    'type'      => 'options',
    'index'     => 'special_price',
    'options' => Mage::getSingleton('eav/entity_attribute_source_boolean')->getOptionArray(),
));
@怪人我希望你觉得这个有用:)