Magento – The Order State “complete” must not be set manually.

If you want to update order status & state programmaticaly, you try this

$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE);

And you get this error :

The Order state 'complete' must not be set manually.

To fix it, use this :

$order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE, "Comments");
$order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);

FPDF error: This document (docuement.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI.

If you ever encounter this error “FPDF error: This document (docuement.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI.”, this is a version problem, FPDF library supports only PDF version 1.4 and previous.

So, what can you do ? change PDF version with ghostscript.

Download it here http://www.ghostscript.com/download/gsdnld.html

run to change :

 ./gs-919-linux_x86_64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=file.pdf newfile.pdf 

PHP define constants with arrays

You need to define a constant containing an array but the code below doesn’t work ?

define('FILTER_ECOLE', array('price, categorie, niveau'));

You get an error :

Notice: Use of undefined constant FILTER_ECOLE - assumed 'FILTER_ECOLE'  in /home/www/adin/app/code/local/Adin/Catalog/Block/Nav/Catalog/Layer/View/Sidebar.php on line 50

To define an use a constant containing an array you can simply serialize it :

#define
define('FILTER_ECOLE', serialize(array('price, categorie, niveau')));

#use
$filter = unserialize(FILTER_ECOLE);

Magento – add a new action in grid

Here, how to add new action in grid like ‘Edit’ link at admin grid.
On this example, we will add a “schedule” action in product admin grid.

We will overrite this class /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php.

Declare the rewrite class on the /app/code/local/Adin/Catalog/etc/config.xml
and add

<global>
      <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>Adin_Catalog_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
</global>

Then create your rewritted class /app/code/local/Adin/Catalog/Block/Adminhtml/Catalog/Product/Grid.php.

<?php
class Adin_Catalog_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
}
?>

Now, rewrite the _prepareColumns() function, find the ‘action’ column an add an entry in the actions array

  $this->addColumn('action',
            array(
                'header'    => Mage::helper('catalog')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('catalog')->__('Edit'),
                        'url'     => array(
                            'base'=>'*/*/edit',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),
                    array(
                        'caption' => Mage::helper('catalog')->__('Schedule'),
                        'url'     => array(
                            'base'=>'*/*/schedule',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
            ));

In our example, we just add this part

array(
                        'caption' => Mage::helper('catalog')->__('Schedule'),
                        'url'     => array(
                            'base'=>'*/*/schedule',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    ),

One last thing, at the end of the _prepareColumns function, change

return parent::_prepareColumns();

by

return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();

PHP – Use of undefined constant MCRYPT_BLOWFISH – assumed ‘MCRYPT_BLOWFISH

While your coding session, you encounter this error :

Use of undefined constant MCRYPT_BLOWFISH - assumed 'MCRYPT_BLOWFISH

You think, easy, a module should be missing on my php
So you install it :

sudo apt-get install php5-mcrypt

And restart apache.

But it still doesn’t work. In phpinfo() it seems the module is not installed.

In fact with the new version of PHP, you still need to activate the module

php5enmod -s apache2 mcrypt

The restart apache and the module should work.

Using GnuPG with PHP

GnuPG is a great tool to encrypt texts and files and you can use it with PHP, only if you install it succesfully.

First, you will get this error : Fatal error: Class ‘gnupg’ not found in ….

OK the lib is not installed, do it with pecl

pecl install gnupg

But you will also have this error :

configure: error: Please reinstall the gpgme distribution
ERROR: `/tmp/pear/temp/gnupg/configure' failed

You need to install the libgpgme too !!!

apt-get install libgpgme11-dev

Now retry to install gnupg

pecl install gnupg

It should be OK.

Don’t forget to modify your php.ini file to load the extension

extension=gnupg.so

And of course, restart your apache web server

/etc/init.d/apache2 restart

PHP form limited to 1000 fields

Since PHP 5.3.9 form are limited to 1000 fields by default.
To fix it, you can edit your php.ini file and modify this variable

max_input_vars = 4000

And don’t forget to restart apache.

You can also edit it in your .htaccess file

 php_value max_input_vars 4000 

Change it directly in your php file with this

 ini_set('php_value max_input_vars', 4000); 

won’t work.

But change this varible may not fix your issue.
If you have suhosin installed, you laso have to edit his configuration file (/etc/php5/apache2/conf.d/suhosin.ini)

suhosin.get.max_vars = 4000 
suhosin.post.max_vars = 4000 
suhosin.request.max_vars = 4000 

To check your suhison configuration, use phpinfo() or in your php script

echo ini_get('suhosin.post.max_vars');