function commerce_webform_get_products_from_webform_submission in Commerce Webform 7.2
Same name and namespace in other branches
- 8 commerce_webform.module \commerce_webform_get_products_from_webform_submission()
- 7 commerce_webform.module \commerce_webform_get_products_from_webform_submission()
Get a list of commerce products from a webform submission.
Parameters
array $submission: A webform submission array.
Return value
array Keyed by product_id. Each value is an object containing product commerce_product The commerce_product object product_id int the product id quantity int the quantity chosen
2 calls to commerce_webform_get_products_from_webform_submission()
- commerce_webform_order_create in ./
commerce_webform.rules.inc - Rules action callback. Adds the product stored in a webform submission to the users cart.
- commerce_webform_order_update in ./
commerce_webform.rules.inc - Rules actions. Update an order from a commerce webform update.
File
- ./
commerce_webform.module, line 278 - Commerce Webform module file
Code
function commerce_webform_get_products_from_webform_submission($submission) {
$product_ids = array();
$products = array();
$data = array();
$components = $submission['components'];
foreach ($components as $component) {
if ($component['component']['type'] == 'productfield') {
foreach ($component['value'] as &$value) {
$value = json_decode($value);
if (!empty($value->product_id) && $value->quantity > 0) {
$product_ids[] = $value->product_id;
if (empty($data[$value->product_id])) {
$data[$value->product_id] = $value;
$data[$value->product_id]->required = $component['component']['required'];
$data[$value->product_id]->choose_quantity = $component['component']['extra']['choose_quantity'];
}
else {
// Now we need to change the quantity.
$data[$value->product_id]->quantity = $data[$value->product_id]->quantity + $value->quantity;
}
}
}
}
}
$products = commerce_product_load_multiple($product_ids);
foreach ($products as $product) {
$data[$product->product_id]->product = $product;
}
return $data;
}