function commerce_pado_form_commerce_cart_add_to_cart_form_alter in Commerce Product Add-on 7
Implementation of hook_form_FORM_ID_alter().
File
- ./
commerce_pado.module, line 95 - Commerce Product Add On adds an option to entityreference fields that allows selected products to act as "add-ons" for the parent product.
Code
function commerce_pado_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
// If there are no available products currently, do not make any changes to the form.
if (!empty($form_state['default_product'])) {
// Get the correct product. If its the first form load, use the default product.
if (empty($form_state['input']['product_id'])) {
$product = $form_state['default_product'];
}
else {
$product = commerce_product_load($form_state['input']['product_id']);
}
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
// Does the product have an entityreference field with the Add On settings checked?
$instances = field_info_instances('commerce_product', $product->type);
$commerce_pado_settings = variable_get('commerce_pado_settings', array());
$commerce_pado_products = array();
// Loop through all fields on the product, and check the instance ID to see if
// it is an entityreference field checked to be used as a product Add On.
foreach ($instances as $field_name => $field_info) {
if (!empty($commerce_pado_settings[$field_info['id']]) && $commerce_pado_settings[$field_info['id']] == 1 && !empty($product->{$field_name})) {
$all_field_info = field_info_field($field_name);
// If cardinality of the field does not equal one, loop through the values of the field and put the products in the array.
if ($all_field_info['cardinality'] != 1) {
foreach ($product_wrapper->{$field_name} as $field_value_wrapper) {
$product_id = $field_value_wrapper->product_id
->value();
$commerce_pado_products[$product_id] = $field_value_wrapper
->value();
}
}
else {
$product_id = $product_wrapper->{$field_name}->product_id
->value();
$commerce_pado_products[$product_id] = $product_wrapper->{$field_name}
->value();
}
$form_state['commerce_pado_products'] = $commerce_pado_products;
// Add an additional submit handler to execute
if (!in_array('commerce_pado_add_to_cart_submit', $form['#submit'])) {
$form['#submit'][] = 'commerce_pado_add_to_cart_submit';
}
}
}
global $language;
foreach ($commerce_pado_products as $product_add_on) {
// Get the renderable array for the product
if ($product_add_on->status == 1) {
$renderable_product = entity_view('commerce_product', array(
$product_add_on->product_id => $product_add_on,
), 'commerce_pado', $language->language, TRUE);
$form[$product_add_on->sku] = array(
'#type' => 'checkbox',
'#title' => drupal_render($renderable_product),
);
}
}
}
}