目前,我在管理面板中为Prestashop 1.6.10编写了一个模块,使用外部API,我的问题是我不知道如何在后台的数据库中添加产品。
这是我写的代码:
public function Product() {
if (empty($_GET['product'])) {
return false;
}
switch($_GET['product']) {
case 'add' :
$product = new ProductCore();
$product->id_shop_default = 1;
$product->id_manufacturer = 1;
$product->id_supplier = 1;
$product->reference = "adding_product";
$product->supplier_reference = "";
$product->location = "";
$product->width = "0.00000";
$product->height = "0.00000";
$product->depth = "0.00000";
$product->weight = "0.00000";
$product->quantity_discount = "0";
$product->ean13 = "0";
$product->upc = "";
$product->cache_is_pack = "0";
$product->cache_has_attachments = "0";
$product->is_virtual = "0";
$product->save();
$product->add();
break;
/** Product suppression.
case 'del' :
if (Product::existsInDatabase()) {
}
break;
}
return false;
}
我使用了"product“对象,但它不是work,我不知道为什么:(
有人能帮帮我吗?
发布于 2017-01-09 21:44:35
我在代码中发现了一个问题:当我从目录页面单击“添加产品”时,控制器adminController没有执行函数Product。
此外,如果我强制Prestashop执行函数,但Prestashop不喜欢这样,因为我创建了一个bug,并且模块不可访问,则该函数可以工作。
更新1-09-2017格林尼治标准时间17:35
目前,代码正在运行,但我现在遇到了这个问题……我认为这与我创建产品时的语言参数有关,但我并没有做我需要做的事情来解决这个问题。
发布于 2017-01-06 23:32:03
您应该使用Product
类而不是ProductCore
。
save
函数足以将产品保存在数据库中。之后就不需要再使用add
函数了。
如果产品值不正确,将显示错误。但是你应该首先激活调试模式: Activate Prestashop Debug Mode
祝好运。
发布于 2017-01-09 20:37:11
尝试使用以下代码。
{
$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();
}
https://stackoverflow.com/questions/41502333
复制相似问题