Opencart module error: Warning: Illegal string offset 'model' in opencart/admin/model/catalog/product.php

Opencart module error: Warning: Illegal string offset 'model' in opencart/admin/model/catalog/product.php

我正在尝试创建一个模块来自动将产品从外部数据库导入到 Opencart 的数据库中。

我按照所有可用的文档来构建模块。创建控制器、视图、语言文件。我没有创建模型文件,因为我使用的是 opencarts 模型。

在我的控制器中opencart/admin/controller/extension/module/auto_product_import.php

我正在开始与我的外部数据库的连接。

然后我执行查询以检索所有产品及其数据(价格、描述等)。

之后,我创建了一个数组以满足 opencart 模型的需要,并用我新检索的数据填充它:

    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' => '' 
                        )
                    )
                );

注意:为了测试目的使用for循环1次迭代

注意:opencart 数组中的某些项目是空的,因为我没有要为它们导入的数据

然后我加载opencart的模型来添加产品:

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

然后我使用以下方法导入产品:

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

当我加载我的模块时,我收到以下消息:

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

我尝试修改 product.php 模型,但没有成功。 :(

它还插入了一行新产品,但所有字段都是空的,它只获取和 id。

为了以防万一,我什至检查了我的新阵列。但是我没有发现任何问题。

有谁知道哪里出了问题或我错过了什么?

addProduct() 似乎是一次添加 1 个产品,而您发送的是整个产品数组。

试试这个:

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

我重构数组后问题解决了。子数组应该有一个子数组,其中 language_id 作为其中的键。

功能数组的结构应如下所示:

$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' 
                    )
                )
            );

注意:此数组适用于 Opencart 3.0.3.8 版本。