Answer the question
In order to leave comments, you need to log in
How to copy a product SKU into its Product Display field in Drupal Commerce?
There are 4k products, there is a need to copy the content from the product SKU field to a specific field of its Product Display, it doesn’t matter in what way, the main thing is not to manually and quickly))
Answer the question
In order to leave comments, you need to log in
Asked and answered :)
function modulename_get_product_display_by_product_id($product_id, $bundle = 'product', $field_name = 'field_producr') {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->propertyCondition('type', $bundle);
$query->fieldCondition($field_name, 'product_id', $product_id);
$query->range(0, 1);
$result = $query->execute();
if ($result) {
$product_display_nid = key($result['node']);
return node_load($product_display_nid);
}
}
/**
* Implements hook_init().
*/
function modulename_init() {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_product')
->entityCondition('bundle', 'product')
->propertyCondition('status', 1)
->fieldOrderBy('commerce_price', 'amount', 'ASC')
;
$result = $query->execute();
$pids = array();
if (isset($result['commerce_product'])) {
$pids = array_keys($result['commerce_product']);
}
foreach($pids as $pid){
$sku = commerce_product_load($pid)->sku;
$node = modulename_get_product_display_by_product_id($pid);
$node->field_code_display[LANGUAGE_NONE][0]['value'] = $sku;
field_attach_update('node', $node);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question