0

I can get from collection "sales/order" customer ids, but when customer is unregistered, value is "null". What can I do to show in grid which customer is registered, with filter "true" or "false". My code:

    protected function _prepareCollection()
    {    
        $collection = Mage::getModel('sales/order')->getCollection()
                        ->addAttributeToSelect('customer_id');
                foreach($collection as $coll)
                {
                    if ($coll['customer_id'] != '')
                    {
                        $coll['customer_id'] = 'yes';
                    }else{
                        $coll['customer_id'] = 'no';
                    }
                }
    ...

    protected function _prepareColumns()
    {
        $this->addColumn("customer_id", array(
                "header" => Mage::helper("mymodule")->__("Registered"),
                'type' => 'options',
                'options' => array(
                        'yes' => 'True',
                        'no' => 'False'
                    ),
                "index" => "customer_id",
            ));
     ...

This option was destroy grid filter. Is any better solution?

3 Answers 3

1

Instead of trying to force a display value based on customer id (which is pre-maturley loading the collection, having the result that your filters are not applied) you could instead add a new row to the grid based on customer_is_guest.

Something like this maybe.

protected function _prepareCollection()
{    
    $collection = Mage::getModel('sales/order')->getCollection()
                    ->addAttributeToSelect('customer_is_guest');
...

protected function _prepareColumns()
{
    $this->addColumn("customer_is_guest", array(
            "header" => Mage::helper("mymodule")->__("Is Guest"),
            'type' => 'options',
            'options' => array(
                    'yes' => 'True',
                    'no' => 'False'
                ),
            "index" => "customer_is_guest",
        ));
 ...
0

You should use grid renderer function. See this link and this link.

So basically you are calling a .php file for rendering and setting condition to show desired info in there.

Hope this helps.

0

Step by step: 1) create a file in helper: options.php

<?php

namespace MagentoOdoo\CustomPro\Block\Adminhtml\Product\Helper\Form;

use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;

class Options extends AbstractRenderer
{
    private $_storeManager;
    /**
     * @param \Magento\Backend\Block\Context $context
     * @param array $data
     */
    public function __construct(\Magento\Backend\Block\Context $context, StoreManagerInterface $storemanager, array $data = [])
    {
        $this->_storeManager = $storemanager;
        parent::__construct($context, $data);
        $this->_authorization = $context->getAuthorization();
    }
    /**
     * Renders grid column
     *
     * @param Object $row
     * @return  string
     */
    public function render(DataObject $row)
    {
        if ($this->_getValue($row) == 1) {
            return 'Valid';
        } elseif ($this->_getValue($row) == 2) {
            return 'Invalid';
        } else {
            return 'Undefine';
        }
    }
}

2) assign one property for your column:

    /**
     * @return Extended
     */
    protected function _prepareColumns()
    {
     ...
     $this->addColumn('status',
                 [
                        'header' => __('Status'),
                        'renderer' => '\MagentoOdoo\CustomPro\Block\Adminhtml\Product\Helper\Form\Options', 
                        'index' => 'status'
                 ]
                );
     ...
    return $this;
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.