20
  • I wonder if there is a special opencart function to add/edit a product to Opencart database programmatically using php/html form on front-end page (such as wordpress "wp_insert_post" function ) or I have to write all my code PHP way (insert into DB_TABLE .....)

  • Also which opencart db tables I have to use to add simple product information with an image, is it only product and product_description tables, of course i need the product to appear also in the admin page after adding it using front-end page

I really need some references to show me the way to do that

(I'm using opencart 1.5.1.3)

usama sulaiman
  • 2,013
  • 4
  • 24
  • 37

2 Answers2

21

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

Community
  • 1
  • 1
Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
  • thank you Jay for fast reply, I'll give it a try and tell you, also thanks for your tutorial in the previous link – usama sulaiman Jun 15 '13 at 13:54
  • 3
    Can you please explain a bit more how to do this. What model? Why we need model? Where i must save this script? Bc i got this when try use ur answer `Fatal error: Class 'Model' not found in /home/wild/plushy.com.ua/test/admin/model/catalog/product.php on line 2` =) – fdrv Mar 06 '14 at 15:51
  • 1
    @Jek-fdrv: What you are asking really has nothing to do with this answer. All details on models, how to call them and why you need them are in the link at the bottom of the post – Jay Gilford Mar 06 '14 at 16:05
  • 1
    I imagine there will be some easy way. create file in this folder (example), add in file this content (example) and then call this file like `http://domain/admin/model/catalog/custom.php?name=123&model=456` etc. How i can do this? – fdrv Mar 06 '14 at 16:09
  • @Jek-fdrv: No this is not possible – Jay Gilford Mar 06 '14 at 16:24
  • Well, then i think it useless help. But any way thank you =) Will try search better detail answer. – fdrv Mar 06 '14 at 17:04
  • @JayGilford Thanks for your answer, where should I put this .php file that has this code to insert products? and which URL should I access that file? I am new to MVC, can you please help me? – Faizan Apr 01 '15 at 09:34
  • @Jek-fdrv Did you find step by step way, where to put this file and how to call it to add products dynamically? Can you answer here? – Faizan Apr 01 '15 at 09:35
1

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

Andrew
  • 127
  • 4