Last active
January 11, 2018 17:33
-
-
Save BBGuy/de6d09dc12bd7a968b44 to your computer and use it in GitHub Desktop.
Drupal 7 commerce utility functionality
This file contains 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 | |
/** | |
* Get the node ID for a commerce product using it's product ID. | |
* | |
* Asumes only one. If not will return the first node referencing the product. | |
* If the commerce product has no product display will return FALSE. | |
*/ | |
function _get_product_referencing_nid($product_id) { | |
$query = new EntityFieldQuery(); | |
$query->fieldCondition('field_product', 'product_id', $product_id, '='); | |
$result = $query->execute(); | |
if (!empty($result)) { | |
//return the first result | |
//first product style | |
$node = $result['node']; | |
// first array index is the node id | |
$nids = array_keys($node); | |
if (!empty($nids)) { | |
$nid = $nids[0]; | |
} | |
} | |
if (isset ($nid)) { | |
return $nid; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
/** | |
* Add a product to cart using it's ID. | |
* | |
* Based on the Drupal commerce_cart_product_add_by_id() function. | |
* This version supports setting of the data element. sets the context with | |
* the url in the display_path and the product_ids array. | |
*/ | |
function product_add_by_id($product_id, $quantity = 1, $combine = TRUE, $uid = NULL) { | |
global $user; | |
// If the specified product exists... | |
if ($product = commerce_product_load($product_id)) { | |
// Create a new product line item for it. | |
$line_item = commerce_product_line_item_new($product, $quantity); | |
// Set the url. | |
$url = 'node/' . _get_product_referencing_nid($product_id); | |
$line_item->data['context']['display_path'] = $url; | |
// Set the product_id | |
$line_item->data['context']['product_ids'][] = $product_id; | |
// Default to the current user if a uid was not passed in. | |
if ($uid === NULL) { | |
$uid = $user->uid; | |
} | |
return commerce_cart_product_add($uid, $line_item, $combine); | |
} | |
return FALSE; | |
} | |
/** | |
* Get the unit price without Tax/VAT for $line_item_wrapper | |
* | |
* calls | |
* $ex_vat = hswalsh_get_ex_vat_price($line_item_wrapper); | |
*/ | |
function commerce_tools_get_ex_vat_price_li($line_item_wrapper) { | |
$component_types = commerce_tax_commerce_price_component_type_info(); | |
// Loop over the price components in the line item's unit price. | |
$price = $line_item_wrapper->commerce_unit_price->value(); | |
$new_components = array(); | |
foreach ($price['data']['components'] as $key => $component) { | |
// Look for components that match one of the defined tax price components. | |
if (in_array($component['name'], array_keys($component_types))) { | |
// Remove it from the components array. | |
unset($price['data']['components'][$key]); | |
// If the component was marked as "included" in the price amount, update | |
// the price amount to reflect the difference. | |
if (!empty($component['included'])) { | |
$price['amount'] -= $component['price']['amount']; | |
} | |
} | |
} | |
return $price; | |
} | |
/** | |
* Get the price without Tax/VAT for $price | |
* | |
* calls | |
* $ex_vat_unit_price = hswalsh_get_ex_vat_price($line_item_wrapper->commerce_unit_price->value()) | |
* $ex_vat_line_total = hswalsh_get_ex_vat_price($line_item_wrapper->commerce_total->value()) | |
*/ | |
function commerce_tools_get_ex_vat_price($price) { | |
$component_types = commerce_tax_commerce_price_component_type_info(); | |
// Loop over the price components in the line item's unit price. | |
$new_components = array(); | |
foreach ($price['data']['components'] as $key => $component) { | |
// Look for components that match one of the defined tax price components. | |
if (in_array($component['name'], array_keys($component_types))) { | |
// Remove it from the components array. | |
unset($price['data']['components'][$key]); | |
// If the component was marked as "included" in the price amount, update | |
// the price amount to reflect the difference. | |
if (!empty($component['included'])) { | |
$price['amount'] -= $component['price']['amount']; | |
} | |
} | |
} | |
return $price; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment