次のコードを使用してください。
{
$object = new Product();
foreach ($_POST as $key => $value) {
if (array_key_exists($key, $object) && $key != 'id_product') {
$object->{$key} = $value;
}
}
$languages = Language::getLanguages(false);
$class_vars = get_class_vars(get_class($object));
$fields = array();
if (isset($class_vars['definition']['fields'])) {
$fields = $class_vars['definition']['fields'];
}
foreach ($fields as $field => $params) {
if (array_key_exists('lang', $params) && $params['lang']) {
foreach ($languages as $language) {
$value = '';
if (Tools::getIsset($field . '_' . (int)$language['id_lang'])) {
$value = Tools::getValue($field . '_' . (int)$language['id_lang']);
} elseif (isset($object->{$field}[(int)$language['id_lang']])) {
$value = $object->{$field}[(int)$language['id_lang']];
}
foreach ($languages as $lang) {
if (Tools::getIsset($field . '_' . (int)$lang['id_lang']) && Tools::getValue($field . '_' . (int)$lang['id_lang']) != '')
$value = Tools::getValue($field . '_' . (int)$lang['id_lang']);
}
if ($field == 'description_short') {
$short_description_limit = Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT')
? Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT') : 400;
$object->{$field}[(int)$language['id_lang']] = strip_tags(
$this->clipLongText(
$value,
'',
$short_description_limit,
false
)
);
} else {
$object->{$field}[(int)$language['id_lang']] = $value;
}
}
}
}
foreach ($languages as $language) {
$keywords = '';
if (Tools::getIsset('meta_keywords_' . $language['id_lang'])) {
$keywords = Tools::getValue('meta_keywords_' . $language['id_lang']);
} elseif (isset($object->meta_keywords[$language['id_lang']])) {
$keywords = $object->meta_keywords[$language['id_lang']];
}
$keywords = $this->cleanMetaKeywords(
Tools::strtolower($keywords)
);
$object->meta_keywords[$language['id_lang']] = $keywords;
}
$_POST['width'] = (!Tools::getIsset('width')) ? '0' : str_replace(',', '.', Tools::getValue('width'));
$_POST['height'] = (!Tools::getIsset('height')) ? '0' : str_replace(',', '.', Tools::getValue('height'));
$_POST['depth'] = (!Tools::getIsset('depth')) ? '0' : str_replace(',', '.', Tools::getValue('depth'));
$_POST['weight'] = (!Tools::getIsset('weight')) ? '0' : str_replace(',', '.', Tools::getValue('weight'));
if (Tools::getIsset('unit_price') != null) {
$object->unit_price = str_replace(',', '.', Tools::getValue('unit_price'));
}
$object->available_for_order = (int)Tools::getValue('available_for_order');
$object->show_price = $object->available_for_order ? 1 : (int)Tools::getValue('show_price');
$object->on_sale = (int)Tools::getValue('on_sale');
$object->online_only = (int)Tools::getValue('online_only');
$ecotaxTaxRate = Tax::getProductEcotaxRate();
if ($ecotax = Tools::getValue('ecotax')) {
$_POST['ecotax'] = Tools::ps_round($ecotax/(1 + $ecotaxTaxRate/100), 6);
}
if (Tools::getIsset('ecotax') != null) {
$object->ecotax = str_replace(',', '.', Tools::getValue('ecotax'));
}
$object->add();
}