How to programmatically add product to Opencart database How to programmatically add product to Opencart database database database

How to programmatically add product to Opencart database


You simply need to create an associate array of values to be passed to the addProduct() method in /admin/model/catalog/product.php. To load the model in your controller use

// Assoc array of data$productData = array(    'name' => 'Product Name Here',    'model' => 'ABC123',    ...);// Load model into memory if it isn't already$this->load->model('catalog/product');// Attempt to pass the assoc array to the add Product method$this->model_catalog_product->addProduct($productData);

This is exactly what OpenCart does in the admin area, only that it uses the POSTed values from the form to pass as the array

Note that some values such as descriptions, images and so on are arrays within the data array itself and therefore need to be coded as such. If you want to take a look at what the model receives from the product add form, Open the model php file, find the addProduct() method and print_r the $data variable at the start of the method which will give you the full list of array keys, most of which are not required. See the form in the admin for which are. It's pretty easy to work out which field relates to which key in the array

More info on getting started as a developer in OpenCart


Been there.

Most important thing, not just add product data to table PREFIX_product and make link to category in PREFIX_product_to_category.

But also create row in PREFIX_product_description (with same product_id).Without it product shows in admin area, but cant be editable and link will be with empty product_id number like "admin/index.php?route=catalog/product/edit&product_id=".

In my case including and using OC core models or clearing cache was unnecessary. Product added via MySQL queries became editable and fully functional.

OC version 2.2.0.0