I need to migrate data into Drupal Commerce, the weight & the size of the object.
How could I do that ?

I try $this->addFieldMapping('field_prod_weight', 'weight') but it didn't work.
Do I need arguments ?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johnv’s picture

Do you use the Migrate module? Feeds-module integration works fine with this patch: #1372070: Integrate with Feeds (Patch Ready)

Gilles_C’s picture

Yes, I'm working with migrate.

delta’s picture

For the physical_weight field use this class as fieldHandler arguments :


class MigrateCommercePhysicalWeightFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this->registerTypes(array('physical_weight'));
  }

  static function arguments($unit = NULL) {
    $arguments = array();
    if (!is_null($unit)) {
      $arguments['unit'] = $unit;
    }
    return $arguments;
  }

  public function prepare($entity, array $field_info, array $instance, array $values) {
    $migration = Migration::currentMigration();
    $arguments = (isset($values['arguments']))? $values['arguments']: array();
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
    // Setup the standard Field API array for saving.
    $delta = 0;
    foreach ($values as $value) {
      $unit = isset($arguments['unit']) ? $arguments['unit'] : 'kg';

      $return[$language][$delta]['weight'] = $value;
      $return[$language][$delta]['unit'] = $unit;
      $delta++;
    }
    if (!isset($return)) {
      $return = NULL;
    }
    return $return;
  }
}

use with:

    $weight_field_arg = MigrateCommercePhysicalWeightFieldHandler::arguments('kg');
    $this->addFieldMapping('field_weight', 'weight')
    ->arguments($weight_field_arg);
delta’s picture

for the Physical dimensions field use this fieldHandler

class MigrateCommercePhysicalDimensionsFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this->registerTypes(array('physical_dimensions'));
  }

  static function arguments($unit = NULL) {
    $arguments = array();
    if (!is_null($unit)) {
      $arguments['unit'] = $unit;
    }
    return $arguments;
  }

  public function prepare($entity, array $field_info, array $instance, array $values) {
    $migration = Migration::currentMigration();
    $arguments = (isset($values['arguments']))? $values['arguments']: array();
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
    // Setup the standard Field API array for saving.
    $delta = 0;
    foreach ($values as $value) {
      if (is_array($value)) {
        $unit = isset($arguments['unit']) ? $arguments['unit'] : 'cm';

        $return[$language][$delta]['length'] = $value['length'];
        $return[$language][$delta]['height'] = $value['height'];
        $return[$language][$delta]['width'] = $value['width'];
        $return[$language][$delta]['unit'] = $unit;
        $delta++;
      }
    }
    if (!isset($return)) {
      $return = NULL;
    }
    return $return;
  }
}

use with :

$dimensions_field_arg = MigrateCommercePhysicalDimensionsFieldHandler::arguments('cm');
    $this->addFieldMapping('field_dimensions', 'dimensions')
    ->arguments($dimensions_field_arg);

in prepareRow migrate method i prepare the dimensions row value:

    $row->dimensions = array('length' => $row->depth, 'width' => $row->width, 'height' => $row->height);

and don't forget to add 'dimensions' to source_fields argument of MigrateSource

Gilles_C’s picture

Thanks a lot ! I'll try this week.

mikeryan’s picture

See also #1671108: Support for physical_weight field - one of these should be marked a dupe of the other. The other issue has the advantage of having a patch...

cameron prince’s picture

I tried this and weight worked perfectly, but dimensions wasn't working. I updated it based on code from addressfield and it seems to be working now.

fearlsgroove’s picture

Status: Active » Needs review
FileSize
4.46 KB

Here's an updated patch with subfield support. Marking the other as a dupe

dwkitchen’s picture

Status: Needs review » Reviewed & tested by the community

Tested and works

bleedev’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
FileSize
3.33 KB

Updated patch using subfields for dimensions. Length is the dimension key and weight is the weight key.

johnv’s picture

Title: Migrate » Add Migrate support to 'Physical Fields'
Issue summary: View changes

better title for my dashboard.

jcisio’s picture

FileSize
5.52 KB

New patch, based on #10, but:

  • Patch against physical instead of migrate_extras.
  • Field handlers are in includes/migrate sub folder.
  • Add example in the comment for each handler.
  • Use sub fields (arguments are possible, too) for all dimensions.

Tested with all the 3 dimensions and the weight field. But I'm not going to RTBC my patch.

ayalon’s picture

FileSize
6.09 KB

The patch in #12 is not working, if you have a single value field and no values on the entity.

I made a new patch.

DaleTrexel’s picture

I was going to ask a question about the behavior of the patch I was seeing, but in the process of writing my question, I discovered the source of my problem. (Applying the patch to Commerce Kickstart's profile structure did not go as error-free as I'd thought.) I've now got it working, and it appears to be working smoothly for me.

So, instead I'd just like to thank you all for the work you put into this patch! It's quite useful and I hope it gets approved and implemented.

ayalon’s picture

If you want this patch to be commited, set the status to reviewed.

DaleTrexel’s picture

How much authority does it take to set the status to reviewed? I can say that the patch is working for me, but I'm not exactly the right person to tell whether it meets Drupal's coding standards.

mikl’s picture

Status: Needs review » Reviewed & tested by the community

#13 works great, thanks.

KarlShea’s picture

#13 also worked for me.

  • bojanz committed 3281d0e on 7.x-1.x
    Issue #1626578 by ayalon, jcisio, fearlsgroove, cameronbprince, bleedev...
bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.