Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCart automatically add new products. Products manipulation

I am new to opencart, and I have to write code which will automatically add/delete/edit products on that opencart website, so my question is the following :

How to add new products to the database automatically with php? (Maybe there are some methods in opencart, so I can use them, or I have to write the code from scratch?)

P.S: an example will be great (suppose we have the product information in an array)

P.P.S: not all products will be added automatically.

like image 928
John Avatar asked May 21 '14 20:05

John


3 Answers

If you have them in an array then it is a piece of cake. There is a method in opencart in admin/model/catalog which you can use, addProduct(). Read the indexes of the $data array and try to bring your array in that form. Eg in this method you can see $data['model'], $data['image'], etc. Assuming you have done so you can create a new controller file, say in admin/controller/module/script.php with some code like the following.

<?php
class ControllerCatalogProduct extends Controller {
    public function index() {
        $products = array(...); //your products here. Eg $products[0] has all the data for the first product
        $this->load->model('catalog/product');
        foreach ($products as $data) {
            $this->model_catalog_product->addProduct($data);
        }
    }
}
?>

Simple as that. Then you call this through http://yoursite/admin/index.php?route=module/script

like image 131
Nikolaos Kakouros Avatar answered Oct 13 '22 15:10

Nikolaos Kakouros


You can create an associative array as below and then creating a controller in admin section to handle it . In this array you have to mind that if you have two languages or so installed in dashboard, you have to repeat each contents for other languages as well . I have an example of two language installed on my side at below and you can follow this structure for your array :

Array (
    [product_description] => Array
        (
            [1] => Array
                (
                    [name] => "product name in first language"
                    [meta_description] => "meta description for first" language
                    [meta_keyword] => "meta keywords in first language"
                    [description] => "product description in first" language 
                    [tag] => "product tags in first language"
                )

            [2] => Array
                (
                    [name] => "first product in second language"
                    [meta_description] => "meta description in second language"
                    [meta_keyword] => "meta tags in second language"
                    [description] => "product description in second language"

                    [tag] => "product tags in second language"
                )
            ....
        )

    [model] => 
    [sku] => 
    [upc] => 
    [ean] => 
    [jan] => 
    [isbn] => 
    [mpn] => 
    [location] => 
    [price] => 2300000
    [tax_class_id] => 9
    [quantity] => 1
    [minimum] => 1
    [subtract] => 1
    [stock_status_id] => 5
    [shipping] => 1
    [keyword] => 
    [image] => data/demo/htc_touch_hd_3.jpg
    [date_available] => 1394-2-23
    [length] => 40
    [width] => 30
    [height] => 20
    [length_class_id] => 2
    [weight] => 23000
    [weight_class_id] => 1
    [status] => 1
    [sort_order] => 1
    [manufacturer] => 
    [manufacturer_id] => 0
    [category] => 
    [filter] => 
    [product_store] => Array
        (
            [0] => 0
        )

    [download] => 
    [related] => 
    [product_attribute] => Array
        (
            [0] => Array
                (
                    [name] => attribute 1
                    [attribute_id] => 
                    [product_attribute_description] => Array
                        (
                            [1] => Array
                                (
                                    [text] => "first language content"
                                )

                            [2] => Array
                                (
                                    [text] => "second language content"                                    )

                        )

                )

        )

    [option] => 
    [points] => 
    [product_reward] => Array
        (
            [1] => Array
                (
                    [points] => 
                )

        )

    [product_layout] => Array
        (
            [0] => Array
                (
                    [layout_id] => 
                )

        )

)

To handle controller in admin side , i think simplest way is what Nikolaos said in last answer.

like image 42
OnlyMAJ Avatar answered Oct 13 '22 15:10

OnlyMAJ


you need to manage 5 tables in order to insert the product details.

  1. oqc_product
  2. oc_product_description
  3. oc_product_to_category
  4. oc_product_to_layout
  5. oc_product_to_store

the above said solutions are also fine.

and yes you may write your own PHP script to add products or directly import it from the excel in above tables.

good day, Hiren

like image 1
Sarthak Patel Avatar answered Oct 13 '22 16:10

Sarthak Patel