nboulfroy
nboulfroy

Reputation: 221

Add product a product in Prestashop 1.6.10 from an API external?

Currently, I program a module for Prestashop 1.6.10 which is in the administration panel, using an external API and my problem is that I don't understand how to add a product in the database in back-office.

This is the code I have wrote :

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;
}

I use the "product" object but it is not work and I don't why :(

Could someone help me please ?

Upvotes: 1

Views: 970

Answers (3)

nboulfroy
nboulfroy

Reputation: 221

I found a problem in my code : the function Product is not executed by the controller adminController when I click on "Adding a product" from the catalog page.

Moreover, the function works if I force Prestashop to execute the function but Prestashop don't like this because I create a bug and the module is not accessible.

[UPDATE 01-09-2017 at 17:35 GMT]

Currently, the code is working, but I have this problem now ... I think it's about the language parameters when I create the product, but I do not do what I need to do to solve this problem.

enter image description here

Upvotes: 0

Knowband Plugins
Knowband Plugins

Reputation: 1317

Try using the following code.

{
        $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();
    }

Upvotes: 0

PrestaAlba
PrestaAlba

Reputation: 689

You should use Product class instead of ProductCore.

save function is enough to save product in DB. It is not necessary to use add function after that.

If product values are incorrect it will display an error. But you should activate DEBUG MODE first: Activate Prestashop Debug Mode

Good luck.

Upvotes: 1

Related Questions