(Note: feel free to move to a more appropriate issue queue, wasn't sure whether to put this here or in the IEF queue!)

Some fixes are being worked on in #1559968: Variation with new term added not displayed correctly to fix the auto-generated product variation title to hide empty attributes, rather than showing something like "[Product Name] (, )". However, I already had a site with nearly 2,000 skus that needed some cleaning up, so I created a custom module for my site using hook_commerce_product_presave() with the updated processing, and the Batch API (borrowed heavily from Examples) to process a resave of all the products.

I'm not sure if this was the best way to do it or not, but it worked for me! To trigger the batch processing, I used Devel to execute the PHP rather than building a FormAPI 'go' button for it.

/**
 * Implements hook_commerce_product_presave().
 */
function mymodule_commerce_product_presave($product) {
	
	// enter the machine names of your product option fields.
	$product_fields = array('field_color','field_size');

	// reset base title, getting the first part before the (
	$exploded = explode(' (', $product->title);
	$product->title = $exploded[0];
	
	// set a flag for whether not attributes are added
	$attributes = FALSE;
	
	foreach ($product_fields as $fieldname) {
		foreach ($product->$fieldname as $field) {
			// both my product fields are taxonomy fields, so that's all I care about
			if (is_array($field) && isset($field[0]['tid'])) {
				$term = taxonomy_term_load($field[0]['tid']);
				$attribute_values[] = $term->name;
				$attributes = TRUE;
			}
		}
	}
	if ($attributes) {
	  $product->title .= ' (' . implode(', ', $attribute_values) . ')';
	}
}


/**
 * Manually trigger the updater batch by running this function
 */
function mymodule_batch_trigger() {
  $batch = mymodule_batch_update_products();
	//dsm($batch);
  batch_set($batch);
}

/**
 * Define batch.
 */
function mymodule_batch_update_products() {

  $operations = array();
	$result = db_select('commerce_product','p')
    ->fields('p',array('product_id'))
    ->execute();
  foreach ($result as $row) {
		$operations[] = array('mymodule_resave_products', array($row->product_id));
	}

  $batch = array(
    'operations' => $operations,
    'finished' => 'mymodule_batch_update_finished',
    // We can define custom messages instead of the default ones.
    'title' => t('Updating products'),
    'init_message' => t('Starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Process has encountered an error.'),
  );
  return $batch;
}

/**
 * Batch operation
 */
function mymodule_resave_products($product_id, &$context) {
	$product = _mymodule_resave_product($product_id);

	// Store some result for post-processing in the finished callback.
	$context['results'][] = $product->sku . ' : ' . check_plain($product->title) . ' ' . $operation_details;
	$context['message'] = 'Updating ' . check_plain($product->title);
}

function _mymodule_resave_product($product_id) {
	// Load the current product, then re-save it.
	$products = entity_load('commerce_product', array($product_id), NULL, TRUE);
	foreach ($products as $key => $product) {
		entity_save('commerce_product', $product);
	}
	return $product;
}

/**
 * Batch 'finished' callback.
 */
function mymodule_batch_update_finished($success, $results, $operations) {
  if ($success) {
    // Here we could do something meaningful with the results.
    // We just display the number of nodes we processed...
    drupal_set_message(t('Updates completed successfully.'));
  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
  }
	drupal_goto('devel/php');
}

Comments

akalata’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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