1

I am creating a Drupal Commerce product programmatically.

I have this test custom PHP script in root folder where Drupal is installed. I am trying to create product/ custom product type "sensilab_product" (product type is already defined in Drupal admin ui), but i get errors when i run this script.

I want to know what to include in the file to start Drupal bootstrap? I've got this script from this url: http://polso.info/how-create-drupal-commerce-products-programmatically

It is Drupal 7.41 /Install profile Commerce Kickstart (commerce_kickstart-7.x-2.31)

<?php

    ini_set('display_errors','1');
    //ini_set('memory_limit', '512M');
    define('DRUPAL_ROOT', getcwd());
    include_once 'includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    module_load_include('inc', 'node', 'node.pages');

    // Product
    $product = commerce_product_new('sensilab_product');
    $product->sku = "PRODUCT-SKU111111";
    $product->title = "Product Title 1111111";
    $product->language = LANGUAGE_NONE;
    $product->uid = 1;
    $product->commerce_price[LANGUAGE_NONE][0] = array(
    'amount' => 10 * 100, // $10
    'currency_code' => "EUR",
    );
    commerce_product_save($product);

I get this errors:

PHP Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923

Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923 PHP Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923

Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923 PHP Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923

Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923 PHP Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923

Notice: Undefined index: REMOTE_ADDR in /home/sanazentyal/public_html/drupalCommerce/includes/bootstrap.inc on line 2923 PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'PRODUCT-SKU111111' for key 'sku': INSERT INTO {commerce_product} (revision_id, sku, title, type, language, uid, status, created, changed, uuid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => PRODUCT-SKU111111 [:db_insert_placeholder_2] => Product Title 1111111 [:db_insert_placeholder_3] => sensilab_product [:db_insert_placeholder_4] => und [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1459943194 [:db_insert_placeholder_8] => 1459943194 [:db_insert_placeholder_9] => d6a9bafb-d0fe-44d1-a217-df7ebfa0c865 ) in drupal_write_record() (line 7333 of /home/sanazentyal/public_html/drupalCommerce/includes/common.inc).

  • Hello! And welcome to Drupal Answers! From the error, it seems that the value of the SKU you are trying to enter is already present in the database. Can you try with other SKU value and see if that works? – AjitS Apr 7 '16 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.