首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从外部API添加产品在Prestashop 1.6.10中的产品?

从外部API添加产品在Prestashop 1.6.10中的产品?
EN

Stack Overflow用户
提问于 2017-01-06 17:11:37
回答 3查看 799关注 0票数 1

目前,我在管理面板中为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,我不知道为什么:(

有人能帮帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-09 21:44:35

我在代码中发现了一个问题:当我从目录页面单击“添加产品”时,控制器adminController没有执行函数Product。

此外,如果我强制Prestashop执行函数,但Prestashop不喜欢这样,因为我创建了一个bug,并且模块不可访问,则该函数可以工作。

更新1-09-2017格林尼治标准时间17:35

目前,代码正在运行,但我现在遇到了这个问题……我认为这与我创建产品时的语言参数有关,但我并没有做我需要做的事情来解决这个问题。

enter image description here

票数 0
EN

Stack Overflow用户

发布于 2017-01-06 23:32:03

您应该使用Product类而不是ProductCore

save函数足以将产品保存在数据库中。之后就不需要再使用add函数了。

如果产品值不正确,将显示错误。但是你应该首先激活调试模式: Activate Prestashop Debug Mode

祝好运。

票数 1
EN

Stack Overflow用户

发布于 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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41502333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档