After upgrading to Magento 1.9.3+ I get a 404 error when creating configurable product
-
04-10-2020 - |
Pregunta
When I create a new configurable product this sometimes results in a 404 error after selecting the attributes to use for configuring the product and clicking Next
. It doesn't always happen but only with certain attributes and / or attribute sets.
Note: Today I ran into this issue with a customer's installation of Magento 1.9.3.1 and after tracking it down I found out it's due to a change in Magento 1.9.3.0. I will answer this question myself to document the fix for future reference for others.
Solución
This is caused by a double URL encoding problem introduced in Magento 1.9.3.0. This update of Magento changed the following function in /js/mage/adminhtml/tools.js
function setLocation(url){
window.location.href = url;
}
to
function setLocation(url){
window.location.href = encodeURI(url);
}
Thus it now URI encodes the entire URL.
However, the /app/design/adminhtml/default/default/template/catalog/product/edit.phtml
template for the product edit page already contains a (poor) implementation to encode the parameters. Because these parameters are base64 encoded it's not unlikely that a /
or =
ends up in the parameters. It seems a bit unnecessary as the values are just integers (ID's) but anyway.
The code is in the setSuperSettings
function:
function setSuperSettings(urlTemplate, attributesClass, validateField) {
var attributesFields = $$('.' + attributesClass);
var attributes = Form.serializeElements(attributesFields, true).attribute;
if(typeof attributes == 'string') {
attributes = [attributes];
}
if(!attributes) {
$(validateField).value = 'no-attributes';
} else {
$(validateField).value = 'has-attributes';
}
if (productForm.validator.validate()) {
var template = new Template(urlTemplate, productTemplateSyntax);
var url = template.evaluate({
attributes: encode_base64(attributes.join(',')).replace(new RegExp('/','g'),'%2F').replace(new RegExp('=','g'),'%3D')
});
setLocation(url);
}
}
Specifically this line:
attributes: encode_base64(attributes.join(',')).replace(new RegExp('/','g'),'%2F').replace(new RegExp('=','g'),'%3D')
The problem can be fixed by removing the last two replace functions. Thus resulting in:
attributes: encode_base64(attributes.join(','))
You should now be able to create configurable products again for all attributes / attribute sets.