Created
January 11, 2017 18:49
-
-
Save chadsten/51d87eb7092759264afd29b3f1a35f31 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 | |
// required to hide add to cart | |
function commerce_price_visibility_form_alter(&$form, &$form_state, $form_id) { | |
if (strpos($form_id,"cart_add_to_cart")) { | |
// standard group/user vis check | |
$show_price = determine_visibility(); | |
// used to hide all from anon users until public access is opened | |
global $user; | |
if ($user->uid == 0) { | |
$form['quantity']['#access'] = false; | |
$form['submit']['#access'] = false; | |
} | |
if ($show_price == 0) { | |
// enables wishlist with no pricing/hide pricing enabled | |
//$form['quantity']['#access'] = false; | |
$form['submit']['#access'] = false; | |
} | |
} | |
} | |
// node view alter | |
function commerce_price_visibility_node_view_alter(&$build) { | |
// only run if we're looking at something with a product field | |
if (!empty($build['field_product'])) { | |
// standard group/user vis check | |
$group = get_user_group(); | |
$show_price = determine_visibility(); | |
// if we're still at 1, let's make sure this product has a price | |
if ($show_price == 1) { | |
// check if the price is 0 on the current product | |
if ($build['product:commerce_price']['#object']->commerce_price[LANGUAGE_NONE][0]['amount'] <= 0 AND $group->nid != '2421') { | |
// we don't hide this since it's 0, part of gap pricing | |
//$show_price = 0; | |
//$build['product:commerce_price']['#object']->commerce_price[LANGUAGE_NONE][0]['amount'] = 'Call for pricing'; | |
//dpm($build['product:commerce_price']['#object']->commerce_price[LANGUAGE_NONE][0]['amount']); | |
} | |
} | |
// now let's hide the price no matter how show price got set | |
if ($show_price == 0) { | |
unset($build['product:commerce_price']); | |
unset($build['product:field_comm_unit_of_measurement']); | |
} | |
//dpm($build); | |
return $build; | |
} | |
} | |
// list view alter | |
function commerce_price_visibility_views_post_execute(&$view) { | |
if ($view->name == 'product_list') { | |
$show_price = determine_visibility(); | |
// if it's 0 based on group/user settings, we don't need to price check | |
// each item in the view to determine visibility | |
if ($show_price == 0) { | |
// loop through each result and unset price/uom | |
foreach ($view->result as &$result) { | |
$result->field_field_comm_unit_of_measurement = array(); | |
$result->field_commerce_price = array(); | |
} | |
} else { | |
// let's only hide 0 price items | |
foreach ($view->result as &$result) { | |
// check each item to see if the price is 0 | |
if (isset($result->field_commerce_price[0]['raw']['amount'])) { | |
$result->field_field_comm_unit_of_measurement = array(); | |
$result->field_commerce_price = array(); | |
} | |
} | |
} | |
return $view; | |
} | |
} | |
// function to actually check visibility based on custom criteria | |
function determine_visibility() { | |
// get all of our user related data | |
global $user; | |
$user_fields = user_load($user->uid); | |
// set this to true at first, so we can eval it later if it's never false | |
$show_price = 1; | |
// find out if the user level price hiding is enabled | |
if (isset($user_fields->field_hide_pricing[LANGUAGE_NONE])) { | |
$user_hide = $user_fields->field_hide_pricing[LANGUAGE_NONE][0]['value']; | |
} else { | |
$user_hide = 1; | |
} | |
if ($user_hide == 1) { | |
$show_price = 0; | |
} else { | |
// let's get the node data ready, in here to decrease queries | |
$group = get_user_group(); | |
// we'll see if the group has hidden pricing | |
if ($group->field_hide_pricing[LANGUAGE_NONE][0]['value'] == 1 AND $group->nid != '2421') { | |
$show_price = 0; | |
} | |
} | |
return $show_price; | |
} | |
function get_user_group() { | |
$groups = og_get_groups_by_user(); | |
if (is_array($groups)) { | |
$group = array_values($groups['node']); | |
$group = $group[0]; | |
$group = node_load($group); | |
} else { | |
$group = node_load('2456'); | |
} | |
return $group; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment