2

When creating a product node for Ubercart, how do I set the product image?

In the code below you see the creation of a Ubercart product node:

$node = new stdClass();

$node->name = "Title";
$node->title = $node->name;
$node->body = "Lorem Ipsum"; 
$node->type = "configurator_product"; //my own product class
$node->created = time();
$node-> changed = $node->created;
$node->promote = 0;
$node->sticky = 0;
$node->format = 2;
$node->status = 1;
$node->language = "en";
$node->default_qty = 1;
$node->sell_price = 55.00;
$node->shippable = 1;

The product node has a field called field_image_cache which expects a file (extensions: png, gif, jpg, jpeg). I tried to set this field:

$node->field_image_cache = "Path/to/module/Images/image1.jpeg";
// Results in Fatal error: Cannot unset string offsets in 
// path/to/modules/cck/content.module on line 1248

$node->field_image_cache[0]['value'] = "Path/to/module/Images/image1.jpeg";
// No errors, but when viewing the product in store administration there is no image

What is the correct / best way to set an image to a ubercart product node?

0
  $img_file = file_get_contents("Path/to/module/Images/image1.jpeg");
  $img_file = file_save_data($img_file, file_directory_path() .'/path2imagefield_folder/image1.jpeg', FILE_EXISTS_RENAME);
  $node->field_image_cache = array(
    array(
      'fid' => 'upload',
      'title' => basename($img_file),
      'filename' => basename($img_file),
      'filepath' => $img_file,
      'filesize' => filesize($img_file),
    ),
  );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.