📜 ⬆️ ⬇️

import of goods with pictures in Ubercart

Ubercart is an open source software module that fully integrates the online store with CMS Drupal. Ubercart is considered one of the best e-commerce solutions in the world.

As a rule, a lot of questions are caused by the massive import of goods with pictures.
In my case, there was an excel file with a list of products and their attributes (name, description, prices, authors, links to pictures, etc.).
Excel file in its pure form is quite difficult to feed for import, so I saved it as a * .csv file.
I will describe what to do next with him :)

I used the node_import module and wrote my own add-on module.

In the add-on module we add hook hook_node_import_fields () it is responsible for additional fields.
Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  1. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  2. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  3. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  4. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  5. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  6. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  7. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  8. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  9. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  10. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }
  11. Copy Source | Copy HTML function my_module_node_import_fields( $type ) { if (_uc_product_node_import_is_product( $type )) { $fields = array ( 'my_field_0' => 'MY FIELD 0' , 'my_field_1' => 'MY FIELD 1' , ... 'my_field_n' => 'MY FIELD N' , 'my_image_filepath' => 'MY Image filepath' , ); return $fields ; }

my_field_n is the machine name of the additional field.
MY FIELD N is how the field will be displayed to the user upon import.
my_image_filepath - should contain a direct path to the file with the image.
')
then add hook hook_node_import_prepare () . Here you can check the validity of the field and prepare them for import.
Copy Source | Copy HTML
  1. function my_module_node_import_prepare (& $ node , $ preview = FALSE) {
  2. $ errors = array ();
  3. if (! isset ( $ node -> my_field_0) || empty ( $ node -> my_field_0)) {
  4. $ errors [] = t ( 'ERROR! on MY FIELD 0' );
  5. } else $ node -> field_0 = $ node -> my_field_0;
  6. if (! isset ( $ node -> my_field_1) || empty ( $ node -> my_field_1)) {
  7. $ errors [] = t ( 'ERROR! on MY FIELD 1' );
  8. } else $ node -> field_1 = $ node -> my_field_1;
  9. ...
  10. if (! isset ( $ node -> my_field_n) || empty ( $ node -> my_field_n)) {
  11. $ errors [] = t ( 'ERROR! on MY FIELD N' );
  12. } else $ node -> field_n = $ node -> my_field_n;
  13. return $ errors ;
  14. }

here we check our fields ( my_field_n ), if something is wrong, then we give an error, otherwise we write to real fields ( field_n ).
By the way, if the CCK field is a small one, then you need to access it like this $ node-> field_n [0] ['value']

for importing pictures, I wrote a separate function:
Copy Source | Copy HTML
  1. function _my_module_create_image_field ( $ filepath , $ nid ) {
  2. $ file_temp = file_get_contents ( $ filepath );
  3. $ file_temp = file_save_data ( $ file_temp , file_directory_path (). '/ images /' . basename ( $ filename ), FILE_EXISTS_RENAME);
  4. $ image_field [ '0' ] [ 'fid' ] = 'upload' ;
  5. $ image_field [ '0' ] [ 'title' ] = basename ( $ file_temp );
  6. $ image_field [ '0' ] [ 'nid' ] = $ nid ;
  7. $ image_field [ '0' ] [ 'filename' ] = basename ( $ file_temp );
  8. $ image_field [ '0' ] [ 'filepath' ] = $ file_temp ;
  9. $ image_field [ '0' ] [ 'filemime' ] = mime_content_type ( $ file_temp );
  10. $ image_field [ '0' ] [ 'filesize' ] = filesize ( $ file_temp );
  11. return $ image_field ;
  12. }

in hook_node_import_prepare () use this:
Copy Source | Copy HTML
  1. if (! $ preview ) {
  2. if ( isset ( $ node -> my_image_filepath)) {
  3. if (file_exists ( $ node -> naeyc_image_filepath)) {
  4. $ node -> field_image_cache = _my_module_create_image_field ( $ node -> my_image_filepath, $ node -> nid);
  5. } else $ errors [] = $ node -> my_image_filepath. t ( 'file is not exists.' );
  6. }
  7. }

$ preview is responsible for the preview. then this code in the preview will not be executed, but only after the user starts importing.

that's all.
waiting for your criticism :)

Source: https://habr.com/ru/post/56666/


All Articles