Home > Mobile >  Opencart module error: Warning: Illegal string offset 'model' in opencart/admin/model/cata
Opencart module error: Warning: Illegal string offset 'model' in opencart/admin/model/cata

Time:10-23

Im trying to create a module to automatically import products from an external database into Opencart's database.

I followed all the available documentation to build the module. Created controller,view,language files. I didnt create a model file cause im using the opencarts models.

Within my controller opencart/admin/controller/extension/module/auto_product_import.php

Im starting a connection with my external database. Then i execute the query to retieve all the products and their data (prices,description,etc).

After that icreate an array to suit the needs of opencart models and populate it with my newly retrieved data :

    for ($i=0; $i < 1; $i  ) { 
                $products_data_array[] = array(
                    'product_description' => array(
                        'name' => strval($results['product_CODE'][$i]),
                        'meta_description' => strval($results['DESCR'][$i]),
                        'meta_keyword' => strval($results['product_CODE'][$i]),
                        'description' => strval($results['DESCR'][$i]),
                        'tag' => strval($results['CODCODE'][$i]),
                    ),
                    'custom_id' => $results['custom_ID'][$i],
                    'model' => $results['product_CODE'][$i], 
                    'sku' => '', 
                    'upc' => '', 
                    'ean' => '', 
                    'jan' => '', 
                    'isbn' => '', 
                    'mpn' => '', 
                    'location' => '', 
                    'price' => $results['PRICE'][$i], 
                    'tax_class_id' => '',
                    'quantity' => '',
                    'minimum' => '',
                    'subtract' => '',
                    'stock_status_id' => '5',
                    'shipping' => '',
                    'keyword' => '', 
                    'image' => '',
                    'date_available' => '',
                    'length' => '',
                    'width' => '',
                    'height' => '',
                    'length_class_id' => '',
                    'weight' => strval($results['weight'][$i]),
                    'weight_class_id' => '',
                    'status' => '1',
                    'sort_order' => '',
                    'manufacturer' => $brand, 
                    'manufacturer_id' => '', 
                    'category' => strval($results['category'][$i]), 
                    'filter' => '', 
                    'product_store' => array(
                        '0' => 0
                    ),
                    'download' => '',
                    'related' => '',
                    'product_attribute' => array(
                        '0' => array(
                            'name' => '',
                            'attribute_id' => '', 
                            'product_attribute_description' => array(
                                '1' => array(
                                        'text' => "first language content"
                                )
                            )
                        )
    
                    ),
                    'option' => '',
                    'points' => '',
                    'product_reward' => array(
                        '1' => array(
                            'points' => ''
                        )
                    ),
                    'product_layout' => array(
                        '0' => array(
                                'layout_id' => '' 
                        )
                    )
                );

Note: Use for loop with 1 itteration for testing purpuses Note: Some items of the opencart's array are empty cause i dont have data to import for them

Then i load the opencart's model to add products:

$this->load->model('catalog/product');

An then i import the products using:

$this->model_catalog_product->addProduct($products_data_array);

When i load my module i get the following messages:


Warning: Illegal string offset 'model' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'custom_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'sku' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'upc' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'ean' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'jan' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'isbn' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'mpn' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'location' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'quantity' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'minimum' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'subtract' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'stock_status_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'date_available' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'manufacturer_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'shipping' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'price' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'points' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'weight' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'weight_class_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'length' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'width' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'height' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'length_class_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'status' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'tax_class_id' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'sort_order' in opencart/admin/model/catalog/product.php on line 4
Warning: Illegal string offset 'product_description' in opencart/admin/model/catalog/product.php on line 12

I tried to modify the product.php model but i had no luck. :( Also it inserts a row with the new product but all the fields are empty it only gets and id. I even checked my new array jsut in case. But i found no issues.

Does anyone know what is wrong or what am i missing?

CodePudding user response:

It looks like addProduct() is for adding 1 product at a time and you're sending the entire array of products.

Try this:

foreach ($products_data_array as $product) {
    $this->model_catalog_product->addProduct($product);
}

CodePudding user response:

The issue resolved after i restructured the array. The sub array should have a child array with language_id as key inside of it.

The functional array should be stuctured like this:

$products_data_array[] = array(

                'product_description' => array(
                    '0' => array(
                        'name' => $name,
                        'meta_description' => $meta_description,
                        'meta_title' => $meta_title,
                        'meta_keyword' => $meta_keyword,
                        'description' => $description,
                        'tag' => $tag,
                    )
                ),
                'mciid' => $mciid,
                'model' => $model, 
                'sku' => '', 
                'upc' => '', 
                'ean' => '', 
                'jan' => '', 
                'isbn' => '', 
                'mpn' => '', 
                'location' => '', 
                'price' => $price, 
                'tax_class_id' => '',
                'quantity' => '',
                'minimum' => '',
                'subtract' => '',
                'stock_status_id' => '5',
                'shipping' => '',
                'keyword' => '', 
                'image' => '',
                'date_available' => '',
                'length' => '',
                'width' => '',
                'height' => '',
                'length_class_id' => '',
                'weight' => $weight,
                'weight_class_id' => '',
                'status' => '1',
                'sort_order' => '',
                'manufacturer' => $brand, 
                'manufacturer_id' => '111', 
                'category' => $category, 
                'filter' => '', 
                'product_store' => array(
                    '0' => 0
                ),
                'download' => '',
                'related' => '',
                'product_attribute' => array(
                    '0' => array(
                        'name' => '',
                        'attribute_id' => '', 
                        'product_attribute_description' => array(
                            '1' => array(
                                    'text' => "first language content"
                            )
                        )
                    )

                ),
                'option' => '',
                'points' => '',
                'product_reward' => array(
                    '1' => array(
                        'points' => ''
                    )
                ),
                'product_layout' => array(
                    '0' => array(
                            'layout_id' => '2' 
                    )
                )
            );

Note: This array is functional on Version 3.0.3.8 of Opencart.

  • Related