Jump to content
webddr.net

programmatically adding product

Recommended Posts

Hi, I am new in prestashop, I plan to create special page to insert fix type of product for intern use.

 

Is there a function or way to save the product data/object programmatically?

 

I am imagine it will look like drupal add node programmatically.

 

If this not possible for now, anyone can tell me what tables are affected each time we adding product with combination?

 

Sorry for my bad English

 

Thanks

Share this post


Link to post
Share on other sites

it's a pretty cool part of native prestashop that I hope in the future to use more...

 

enjoy!

Share this post


Link to post
Share on other sites

it is possible to create new product by php code (program)

 

$product = new Product();
$product->ean13 = 9999999999999;
$product->name = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'Test importu');;
$product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'test-importu');
$product->id_category = 2;
$product->id_category_default = 2;
$product->redirect_type = '404';
$product->price = 22;
$product->quantity = 1;
$product->minimal_quantity = 1;
$product->show_price = 1;
$product->on_sale = 0;
$product->online_only = 1;
$product->meta_keywords = 'test';
$product->is_virtual=1;
$product->add();
$product->addToCategories(array(2));

$shops = Shop::getShops(true, null, true);    
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add())
{
    $image->associateTo($shops);
    if (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))
    {
        $image->delete();
    }
}

  • Like 2

Share this post


Link to post
Share on other sites

it is possible to create new product by php code (program)

 

$product = new Product();

$product->ean13 = 9999999999999;

$product->name = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'Test importu');;

$product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') =>  'test-importu');

$product->id_category = 2;

$product->id_category_default = 2;

$product->redirect_type = '404';

$product->price = 22;

$product->quantity = 1;

$product->minimal_quantity = 1;

$product->show_price = 1;

$product->on_sale = 0;

$product->online_only = 1;

$product->meta_keywords = 'test';

$product->is_virtual=1;

$product->add();

$product->addToCategories(array(2));

 

$shops = Shop::getShops(true, null, true);    

$image = new Image();

$image->id_product = $id_product;

$image->position = Image::getHighestPosition($id_product) + 1;

$image->cover = true; // or false;

if (($image->validateFields(false, true)) === true &&

($image->validateFieldsLang(false, true)) === true && $image->add())

{

    $image->associateTo($shops);

    if (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))

    {

        $image->delete();

    }

}

 

Hi and its possible create this way a product with specific product_id?

When i run this script I obtain error : Call to protected method AdminImportControllerCore::copyImg() from context

Edited by mardon (see edit history)

Share this post


Link to post
Share on other sites

We can achieve this by overriding AdminImportController class and make copyImage() method as public.

 

Now you can access copyImage() method from anywhere.

 

Hope it will help.

Share this post


Link to post
Share on other sites

@sami_c I am looking for this as well, If You found it out how to add custom value for feature group then I will be very happy if You will share with it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More