function commerce_bpc_create_product in Commerce Bulk Product Creation 7
Same name and namespace in other branches
- 7.2 commerce_bpc.module \commerce_bpc_create_product()
API function to a bulk product based on the given parameters
Parameters
string $product_type: The name of the product type for which products should be created.
array $combination: An corresponding to one combination of values for which a product should be created. This array should have field names as keys and field value-arrays as values. Example for a non-translatable field with one value (for the current "combination"):
'field_list1' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => 'foo'
),
),
)
This is the format returned by field_ui's forms and used by the field api in general.
array $static_values: An array of the same format as the combination-array above, representing the fields that should have identical values across all combinations.
array $extras: An array for the values of 'extra fields' defined for the product type entity, or patterns for these. Recognized keys are:
- sku_pattern
- title_pattern
- status
- uid
Note that the values do NOT come in the form of complex arrays (as they are not translatable, and can only have single values) like the previous two parameters.
Return value
int The ID of the created product.
1 call to commerce_bpc_create_product()
- commerce_bpc_create_bulk_products in ./
commerce_bpc.module - API function to create bulk products based on the given parameters
File
- ./
commerce_bpc.module, line 331
Code
function commerce_bpc_create_product($product_type, $combination, $static_values, $extras) {
$form_state = array();
$form_state['values'] = $static_values;
$form = array();
$form['#parents'] = array();
// Generate a new product object.
$new_product = commerce_product_new($product_type);
$new_product->status = $extras['status'];
$new_product->uid = $extras['uid'];
$new_product->language = LANGUAGE_NONE;
// Replace the tokens for the SKU and Title.
$data = array();
$data['bulk_data'] = array(
'combination' => $combination,
'static_values' => $static_values,
'product_type' => $product_type,
);
$new_product->sku = token_replace($extras['sku_pattern'], $data, array(
'sanitize' => FALSE,
));
$new_product->title = token_replace($extras['title_pattern'], $data, array(
'sanitize' => FALSE,
));
// Set the proper values in the form_state for this product's combination of
// fields.
foreach ($combination as $field => $value) {
$form_state['values'][$field] = $value;
}
// Notify field widgets to save their field data.
field_attach_submit('commerce_product', $new_product, $form, $form_state);
commerce_product_save($new_product);
return $new_product->product_id;
}