Created
January 11, 2017 18:47
-
-
Save chadsten/df37b56a2c5cef3100e4ad5d95fa65ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function commerce_group_sku_form_alter(&$form, &$form_state, $form_id) { | |
if(strstr($form_id, 'commerce_cart_add_to_cart_form_')) { | |
$group = get_user_groups(); | |
$custom_label = get_custom_label($group); | |
$gpn = get_custom_sku($form_state['default_product']->product_id, $group); | |
if (!empty($gpn)) { | |
$form_state['default_product']->field_gpn_placeholder[LANGUAGE_NONE][0]['value'] = "<div class='field-label'>" . $custom_label . ":</div> <div class='gpn'>" . $gpn . "</div>"; | |
$form_state['default_product']->field_gpn_placeholder[LANGUAGE_NONE][0]['format'] = "full_html"; | |
} | |
} | |
} | |
function get_custom_sku($product_id, $group) { | |
$query = new EntityFieldQuery(); | |
$query->entityCondition('entity_type', 'group_part_number') | |
->entityCondition('bundle', 'group_part_number') | |
->fieldCondition('field_product_gpn_ref', 'product_id', $product_id, '=') | |
->fieldCondition('field_group_gpn_ref', 'target_id', $group, '='); | |
$result = $query->execute(); | |
if (isset($result['group_part_number'])) { | |
$group_part_numbers = array_keys($result['group_part_number']); | |
$gpn = entity_load('group_part_number', $group_part_numbers); | |
} | |
$gpn = reset($gpn); | |
return $gpn->field_group_sku_gpn_ref[LANGUAGE_NONE][0]['value']; | |
} | |
function get_custom_label($group) { | |
$group = node_load($group); | |
if (isset($group->field_custom_pn_label[LANGUAGE_NONE][0]['value'])) { | |
$custom_label = $group->field_custom_pn_label[LANGUAGE_NONE][0]['value']; | |
} else { | |
$custom_label = 'Customer PN'; | |
} | |
return $custom_label; | |
} | |
function get_user_groups() { | |
$groups = og_get_groups_by_user(); | |
if (is_array($groups)) { | |
$group = array_values($groups['node']); | |
$group = $group[0]; | |
return $group; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment