Solve the problem that the Magento batch assign products can only save successfully 1000 pieces when the product is sorted

Source: Internet
Author: User

Note: The issue in this article has been verified in Magento EE 1.14/ce 1.9.

Recently, I found a very strange problem. When assigning classifications for a batch of products (greater than 1000 pieces), only a maximum of 1000 products can be saved successfully, while others are ignored. In order to solve this problem, we need to go deep into the Magento source for analysis and testing.

Here is how Magento assigns the source code of the product Category:

Class Mage_adminhtml_catalog_categorycontroller extends Mage_adminhtml_controller_action{public function saveAction () {... if (isset ($data [' category_products ']) &&! $category->getproductsreadonly ()) {$products = array (); Parse_str ($data [' category_products '], $products); $category->setpostedproducts ($products);} . . .}}

$data [' category_products '] is a product sequence of strings that are generated by parse_str an associative array to save the product in an array. But from this code, do not see what is wrong, and then go to the error log check, and then found the following warning:
warning:parse_str (): Input variables exceeded 1000. To increase the limit change 
max_input_vars in php.ini.

As you can see, this problem is caused by PHP's configuration parameter max_input_vars, because PHP limits the number of input variables that are acceptable. In the description of PHP version 5.3.9, it is largely to avoid hash collisions, PARSE_STR limits the number of parameters. When the length of the result array is greater than or equal to 1000, the default is only 1000, which is why only one thousand product has been saved and the rest is not.

There are 2 ways to solve this problem. One is to increase the value of the Max_input_vars parameter, and the second is to modify the Magento code.

1: Modify the value of the Max_input_vars
Max_input_vars values can be changed in two ways, modifying htaccess or modifying php.ini.
Add the following line to the htaccess in the Magento root directory, and the values can be changed as appropriate:
Php_value Max_input_vars 2000

Locate php.ini, locate Max_input_vars = 1000, uncomment the row if it is commented, and then adjust the value. You may need to restart the server for the changes to take effect.

2: Modify the Magento code

Changing the Max_input_vars value is fast and easy. But if your products and categories are growing fast every day, this repetitive operation can be a bit of a hassle. To avoid this situation, we can try to modify the code as follows:

Require_once ' mage/adminhtml/controllers/catalog/categorycontroller.php '; class Inchoo_smile_adminhtml_catalog_ Categorycontroller extendsmage_adminhtml_catalog_categorycontroller{public function saveaction () {... if (Isset ($ data[' category_products ') &&! $category->getproductsreadonly ()) {$products = array (); $exploded = Explode ( ' & ', $data [' category_products ']); foreach ($exploded as $row) {$temp = array ();p arse_str ($row, $temp), List ($key, $value) = each ($temp), if (!empty ($key)) {$p roducts[$key] = $value;}} $category->setpostedproducts ($products);} ...}}


The above method overrides the class controller, but in fact we can use a more elegant solution. is still in
Mage_adminhtml_catalog_categorycontroller, in the saveaction time, Magento will set out an event called Catalog_category_prepare_save, With this event, the data can be modified with the following code:

Class inchoo_smile_model_observer{//Event:catalog_category_prepare_savepublic function Assigncategoryproducts ($ observer) {$category = $observer->getcategory (); $request = $observer->getrequest (); $products = Array (); $ exploded = Explode (' & ', $request->getparam (' category_products ')); foreach ($exploded as $row) {$temp = array ();p arse_str ($row, $temp), List ($key, $value) = each ($temp), if (!empty ($key)) {$p roducts[$key] = $value;}} $category->setpostedproducts ($products);} }


The above code can be placed in any of your own written module, and then add the event Config, remember not to modify the core code Magento.

From: Jonas's Magento Blog





Solve the problem that the Magento batch assign products can only save successfully 1000 pieces when the product is sorted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.