Automatically Import products every week

Posts: 10
Joined: 11/19/2008

I've spent hours trying to find out where to start, but all I see is the import module. This does not work for me because the import has to be automated.

In the past I've written PHP scripts to create Drupal nodes to import data automatically using the drupal_execute() function, but I do not know if the same would apply here. Is their a similar API function in Ubercart that is specific to creating a product with an attached image?

Posts: 10
Joined: 11/19/2008

For Example, the following does NOT work.

<?php

$book_node
= array('type' => 'book');
$book_node = (object) $book_node;
$values = array (
 
'title' => 'Test Book 3',
 
'body' => 'Test',
 
'format' => '1',
 
'model' => '2234567',
 
'list_price' => '',
 
'cost' => '',
 
'sell_price' => '20',
 
'shippable' => '1',
 
'weight' => '',
 
'weight_units' => 'lb',
 
'length_units' => 'in',
 
'length' => '',
 
'width' => '',
 
'height' => '',
 
'pkg_qty' => '1',
 
'default_qty' => '1',
 
'ordering' => '0',
// 'field_authors' => array ('nids' => array (0 => '1')),
 
'field_authors' => array ('nids' => '1'),
 
'shipping_type' => '',
 
'first_name' => 'Pacy',
 
'last_name' => 'World',
 
'company' => 'Pacy World',
 
'street1' => '123 Main Street',
 
'street2' => '',
 
'city' => 'Naples',
 
'zone' => '18',
 
'postal_code' => '34105',
 
'country' => '840',
 
'usps' => array ('container' => 'RECTANGULAR'), 'flatrate' => array (1 => ''), 'ups' => array ('pkg_type' => '02'),
 
'weightquote' => '',
 
'comment' => '2',
 
'name' => 'admin',
 
'status' => '1',
);

$result = drupal_execute('book_node_form', $values, $book_node);
?>

Posts: 5617
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

I'm not sure what you'll need for your install specifically, but here's a dump of a product node object from the Livetest:

stdClass Object
(
    [nid] => 1
    [vid] => 1
    [type] => product
    [status] => 1
    [created] => 1224864445
    [changed] => 1226347031
    [comment] => 0
    [promote] => 0
    [sticky] => 0
    [revision_timestamp] => 1226347031
    [title] => Code Transmogrifier
    [body] => Transform your code into something useful! Buy it today and save your hair for tomorrow. You'll never have to pull it out again! Brought to you by the creators of Ubercart borrowing from the creative genius of Calvin.
    [teaser] => Transform your code into something useful! Buy it today and save your hair for tomorrow. You'll never have to pull it out again! Brought to you by the creators of Ubercart borrowing from the creative genius of Calvin.
    [log] =>
    [format] => 1
    [uid] => 2
    [name] => Ryan
    [picture] =>
    [data] => a:3:{s:18:"admin_compact_mode";b:1;s:11:"remove_file";a:0:{}s:8:"add_file";a:0:{}}
    [model] => TRANS-100
    [list_price] => 0.00
    [cost] => 0.00
    [sell_price] => 495.00
    [weight] => 5
    [weight_units] => lb
    [length] => 0
    [width] => 0
    [height] => 0
    [length_units] => in
    [pkg_qty] => 1
    [default_qty] => 1
    [unique_hash] => ac46de101ea986b7d66ac492aaf2b156
    [ordering] => 0
    [shippable] => 1
    [path] => product/code-transmogrifier
    [field_image_cache] => Array
        (
            [0] => Array
                (
                    [fid] => 1
                    [title] =>
                    [alt] =>
                    [nid] => 1
                    [filename] => code_transmogrifier.gif
                    [filepath] => files/code_transmogrifier.gif
                    [filemime] => image/gif
                    [filesize] => 13118
                )

        )

    [last_comment_timestamp] => 1224864510
    [last_comment_name] =>
    [comment_count] => 0
    [taxonomy] => Array
        (
            [1] => stdClass Object
                (
                    [tid] => 1
                    [vid] => 1
                    [name] => Coding Tools
                    [description] => Coding tools offer you some much needed inspiration when the code just isn't coming.  Purchase one of our products today and watch your productivity increase virtually overnight!
                    [weight] => 0
                )

        )

    [files] => Array
        (
        )

    [flatrate] => Array
        (
            [1] => 0.00
            [2] => 0.00
        )

    [shipping_type] => small_package
    [shipping_address] => stdClass Object
        (
            [first_name] =>
            [last_name] =>
            [company] => Ubercart
            [phone] =>
            [street1] => 500 Uber Way
            [street2] =>
            [city] => Louisville
            [zone] => 27
            [postal_code] => 40218
            [country] => 840
        )

    [ups] => Array
        (
            [vid] => 1
            [nid] => 1
            [pkg_type] => 04
        )

)

Posts: 10
Joined: 11/19/2008

Is it possible that the reason I can not insert a new product node is due to the fact that I am missing the:

[unique_hash] => ac46de101ea986b7d66ac492aaf2b156

element?

If so, how do I generate it? By the way, my post above, the code does not work, but it also does not give error messages.
The following is the form variables for my installation to create a book class product node:

<?php
array (
 
'title' => 'Test Book 2',
 
'body' => 'Test',
 
'format' => '1',
 
'model' => '1234567',
 
'list_price' => '',
 
'cost' => '',
 
'sell_price' => '20',
 
'shippable' => '1',
 
'weight' => '',
 
'weight_units' => 'lb',
 
'length_units' => 'in',
 
'length' => '',
 
'width' => '',
 
'height' => '',
 
'pkg_qty' => '1',
 
'default_qty' => '1',
 
'ordering' => '0',
 
'field_authors' =>
  array (
   
'nids' =>
    array (
     
0 => '1',
    ),
  ),
 
'nid' => '',
 
'vid' => '',
 
'type' => 'book',
 
'changed' => '',
 
'form_token' => 'f9ba5d7f421eb5de6d0c00919c13c293',
 
'form_id' => 'book_node_form',
 
'field_image_cache-attach-url' => 'http://dev.pacyworld.com/XXXXXX/cart/?q=imagefield/js',
 
'shipping_type' => '',
 
'first_name' => 'Pacy',
 
'last_name' => 'World',
 
'company' => 'PacyWorld',
 
'street1' => '123 Main Street',
 
'street2' => '',
 
'city' => 'Naples',
 
'zone' => '18',
 
'postal_code' => '34145',
 
'country' => '840',
 
'usps' =>
  array (
   
'container' => 'RECTANGULAR',
  ),
 
'flatrate' =>
  array (
   
1 => '',
  ),
 
'ups' =>
  array (
   
'pkg_type' => '02',
  ),
 
'weightquote' => '',
 
'log' => '',
 
'comment' => '2',
 
'path' => '',
 
'menu' =>
  array (
   
'title' => '',
   
'description' => '',
   
'pid' => '1',
   
'path' => '',
   
'weight' => '0',
   
'mid' => '0',
   
'type' => '86',
  ),
 
'name' => 'admin',
 
'date' => '2008-11-19 17:01:18 -0500',
 
'status' => '1',
 
'op' => 'Submit',
)
?>

That is what I based my original code off of.

Posts: 10
Joined: 11/19/2008

I just tried the following and it did not work.

Image:

<?php
$image
= array('0' => array
        (
                   
'fid' => 1,
                   
'title' => "",
                   
'alt' => "",
                   
'nid' => 1,
                   
'filename' => "code_transmogrifier.gif",
                   
'filepath' => "files/code_transmogrifier.gif",
                   
'filemime' => "image/gif",
                   
'filesize' => 13118
               
)
);
?>

Taxomony:

<?php
$taxomonyObj
= array
                (
                   
'tid' => 1,
                   
'vid' => 1,
                   
'name' => "Coding Tools",
                   
'description' => "Coding tools offer you some much needed inspiration when the code just isn't coming.  Purchase one of our products today and watch your productivity increase virtually overnight!",
                   
'weight' => 0
               
);

$taxomonyObj = (object) $taxomonyObj;
$taxomony = array('1' => $taxomonyObj);
?>

Shipping Address:

<?php
$shippingAddress
= array
        (
           
'first_name' => '',
           
'last_name' => '',
           
'company' => 'Ubercart',
           
'phone' => '',
           
'street1' => '500 Uber Way',
           
'street2' => '',
           
'city' => 'Louisville',
           
'zone' => 27,
           
'postal_code' => 40218,
           
'country' => 840
       
);
$shippingAddress = (object) $shippingAddress;
?>

Book Product Object:

<?php
$book
= array
(
   
'nid' => '',
   
'vid' => '',
   
'type' => 'book',
   
'status' => 1,
   
'created' => '',
   
'changed' => '',
   
'comment' => 0,
   
'promote' => 0,
   
'sticky' => 0,
   
'revision_timestamp' => '',
   
'title' => 'Code Transmogrifier',
   
'body' => "Transform your code into something useful! Buy it today and save your hair for tomorrow. You'll never have to pull it out again! Brought to you by the creators of Ubercart borrowing from the creative genius of Calvin.",
   
'teaser' => "Transform your code into something useful! Buy it today and save your hair for tomorrow. You'll never have to pull it out again! Brought to you by the creators of Ubercart borrowing from the creative genius of Calvin.",
   
'log' => '',
   
'format' => 1,
   
'uid' => 2,
   
'name' => 'admin',
   
'picture' => '',
   
'data' => '',
   
'model' => 'TRANS-100',
   
'list_price' => 0.00,
   
'cost' => 0.00,
   
'sell_price' => 495.00,
   
'weight' => 5,
   
'weight_units' => 'lb',
   
'length' => 0,
   
'width' => 0,
   
'height' => 0,
   
'length_units' => 'in',
   
'pkg_qty' => 1,
   
'default_qty' => 1,
   
'unique_hash' => '',
   
'ordering' => 0,
   
'shippable' => 1,
   
'path' => '',
   
'field_image_cache' => $image,
   
'last_comment_timestamp' => '',
   
'last_comment_name' => '',
   
'comment_count' => 0,
   
'taxonomy' => $taxomony,
   
'files' => array(),
   
'flatrate' => array('1' => 0.00, '2' => 0.00),
   
'shipping_type' => 'small_package',
   
'shipping_address' => $shippingAddress,
   
'ups' => array('vid' => 1, 'nid' => 1, 'pkg_type' => 04)
);
?>

Using the node_save() function and checking for errors:

<?php
node_save
($book);

$errors = form_get_errors();
if (
count($errors)) {
 
print_r($errors);
}
?>

Returns no errors and no new product node was created.

Posts: 5617
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

All I can say is start checking your watchdog or Apache error log for bugs. Smiling

Posts: 116
Joined: 04/23/2008

tuaris - you should also look at how node_import with ubercart patch loads products. See:

http://www.ubercart.org/contrib/5221

You can then use some of the code directly in your program.

I know there was a thread for someone trying to do what you are doing, but it never got completed... See:

http://drupal.org/node/191197

Posts: 10
Joined: 11/19/2008

Thanks, that actually helped a lot.
Now I just have to figure out how to upload images and assign them to the products, but thats another thread.

Posts: 116
Joined: 04/23/2008

No problem. Let me know how you get on with this, as I am interested in it too.

About how many products are you loading weekly?

By the way, here is a separate thread on what I had to do to node_import to improve performance:

http://drupal.org/node/309563

Posts: 116
Joined: 04/23/2008

Hi Tuaris. Were you able to make more progress on this?

Posts: 10
Joined: 11/19/2008

Yeah, I created a fully detailed document on how I did it right here:

http://www.unibia.com/unibianet/node/31

Posts: 116
Joined: 04/23/2008

Ok. Thanks for the link. Out of interest, I'd like to know what kind of speed you were getting with your script e.g. how many products (books) per hour.