I've been successful in mapping/importing products with a File field using (many thanks!) the documentation found at http://www.drupalcommerce.org/comment/reply/467#comment-form, but have not been able to map to a Commerce File field. It does not appear as a target.

Comments

rfay’s picture

Title: Mapping to Commerce File Field - missing target » Add Commerce File field mapping
Category: support » feature

Yes, I'm pretty sure we'll have to add that as a mapping. Thanks for pointing that out. Patches are welcome.

rennevat’s picture

StatusFileSize
new3.32 KB
new485 bytes

First attempt. The new commerce_file.inc for commerce_feeds/mappers comes from http://drupal.org/node/1162602#comment-4581266. The import is uploading the images and zip files but fails on importing the products and returns SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_commerce_file_data' cannot be null

rfay’s picture

Status: Active » Needs review

I assume you're having to make changes in both commerce_file and commerce_feeds? If so, please post to an issue in commerce_file and crossreference it here.

Setting to "Needs review"

pcambra’s picture

I think that both patches attached by rennevat are for commerce_feeds module.

Keeping it as needs review as I'd like to review and test this with more detail.

discipolo’s picture

they are for commerce_feeds. but i think the code at http://drupal.org/node/1162602#comment-4581266 was supposed to import files that are already in the files table by their fid without duplicating them

the patch in #2 uses the files path and creates a fileduplicate

Stolinski’s picture

What's the status of this? Importing a file to a commerce file field is an essential feature.

joelrosen’s picture

Managed to piece together this snippet in my own custom FeedsProcessor by looking through the code from Commerce File. Seems to be creating Commerce File fields properly. Here, $new_product is a Commerce Product with a Commerce File field named "field_image_file" on it, and $filename is the full path to the file I want to sell access to:


                $file           = new stdClass();
                $file->uid      = 0;
                $file->uri      = $filename;
                $file->filemime = 'application/octet-stream';
                $file->filename = basename($file->uri);
                $file = file_save($file);
                
                // Attach default license data:
                $cf_defaults = field_info_instance_settings(COMMERCE_FILE_FIELD_TYPE);
                
                $new_product->field_image_file[LANGUAGE_NONE][0] = array(
                  'fid' => $file->fid,
                  'data' => $cf_defaults['data']
                );

Not sure if I did this properly... but seems to be working fine.

jonathan_hunt’s picture

Version: 7.x-1.1 » 7.x-1.2
Component: Documentation » Code
StatusFileSize
new4.57 KB

The attached patch builds on #2. Key changes are (a) always using private:// for the target file destination (this is what Commerce File defaults to), though it would be better if Commerce File exposed an API so that the scheme can be queried, and (b) attaching license data to the file (this is the "'field_commerce_file_data' cannot be null" error @rennevat ran into. I'm sure the code can be improved further, but it is working for me now.

One caveat is that files to be imported need to be reachable by drupal_http_request - this may not always be the case for file:// for example. My work around at present is to upload files (e.g. using sftp) into a path within public://

recrit’s picture

feeds support has been added natively to Commerce File at #1162602: Feeds support for Commerce File

jonathan_hunt’s picture

Status: Needs review » Closed (won't fix)

That's great news, thanks. Shame that there has been duplicated effort. Setting this to won't fix with respect to Commerce Feeds.