Import script
Hi!
I would like to make a custom import XML script, to load external data from a custom software. This software only send xml data to the webshops.
It is ok, but I would like to store an external ID in commerce (what is searchable).
I tried that:
...
$product = commerce_product_new('product');
$product->product_id = $sid; // $sid is the external ID
...
but it doesn't work. The product_id is an internal ID, it is not modified.
I created custom field in "product types": field_sid
But how can I search in this field? I can search in SKU field with commerce_product_load_by_sku() function.
Gabor
Comments
import script with product image
Hi!
I have a problem in import script. How can I save the product with product image?
I tried it:
...
$product_q = commerce_product_load_by_sku( $xml_product ); // load to modify with image data
$product = new StdClass();
$product = $product_q;
$product->status = '1';
$product->revision_timestamp = time();
$product->created = time();
$product->changed = time();
$product->field_image[LANGUAGE_NONE][0] = array(
'uid' => ...,
'width' => ...,
'height' => ...,
'filename' => ...,
'uri' => 'public://commerce/' . $file_name,
'filemime' => 'image/jpeg',
'filesize' => ...,
'fid' => ...,
'status' => '1',
'timestamp' => time(),
);
commerce_product_save( $product );
...
No error, but I cannot save image data into database.
How can I set the " $product->field_image[LANGUAGE_NONE][0] " in script?
G.
You will have to create the
file_save
I used the file_save().
No error, but not image to product. The entry is ok in "file_managed" table, but in "field_data_field_image" and "field_revision_field_image" tables are no entries (with entity_id and revision_id fields). Why? :(
The code:
...
$imgdata = base64_decode( $xml_webpic );
$img = imagecreatefromstring( $imgdata );
if ( $img !== false && imagejpeg( $img, DRUPAL_ROOT . '/sites/default/files/commerce/' . $xml_product . '_img.jpg' ) ) {
$file = new StdClass();
$file->fid = NULL;
$file->uid = $user->uid;
$file->filename = $xml_product . '_img.jpg';
$file->uri = 'public://commerce/' . $xml_product . '_img.jpg';
$file->filemime = image_type_to_mime_type(IMAGETYPE_JPEG);
$file->filesize = filesize( DRUPAL_ROOT . '/sites/default/files/commerce/' . $xml_product . '_img.jpg' );
$file->status = '1';
$file->timestamp = time();
// file_save() to get $fid
$thefile = file_save( $file );
// get image info from saved image
list($width, $height, $type, $attr) = getimagesize( DRUPAL_ROOT . '/sites/default/files/commerce/' . $xml_product . '_img.jpg' );
// $product_q exists in commerce?
if ( $product_q = commerce_product_load_by_sku( $xml_product ) ) {
$product = new StdClass();
$product = $product_q;
$product->status = '1';
$product->revision_timestamp = time();
$product->type = $product_q->type;
$product->uid = $product_q->uid;
$product->created = time();
$product->changed = time();
$product->field_image[LANGUAGE_NONE][0] = array(
'uid' => $product_q->uid,
'alt' => '',
'revision_id' => $product->revision_id,
'entity_id' => $product->product_id,
'title' => '',
'width' => $width,
'height' => $height,
'filename' => $thefile->filename,
'uri' => $thefile->uri,
'filemime' => $thefile->filemime,
'filesize' => filesize( DRUPAL_ROOT . '/sites/default/files/commerce/' . $xml_product . '_img.jpg' ),
'fid' => $thefile->fid,
'status' => $thefile->status,
'timestamp' => $thefile->timestamp,
'rdf_mapping' => array(),
);
// so I tried it:
// $product->field_image[LANGUAGE_NONE][0] = $thefile;
commerce_product_save( $product );
} // if
} // if
...
SOLVED
I solved it!
The new product is a custom product_type (for exmaple: book).
I need to add the built-in field "Image" (field_image) to this product_type (book).
Then it is working. :)
Intersting: the built-in fields need to add to the product_types, but the custom fields no need add to product_types (these are automatic added fields).
:O