Skip to content

Instantly share code, notes, and snippets.

View joshmiller83's full-sized avatar
🐢
Taking things one step at a time.

Josh Miller joshmiller83

🐢
Taking things one step at a time.
View GitHub Profile
<?php
global $user;
// Load the order object.
$order = commerce_cart_order_load($user->uid);
if (!empty($order)) {
// Create order entity metadata wrapper.
$wrapper = entity_metadata_wrapper('commerce_order', $order);
// Determine cart order total without taxes
// Maybe rszrama can help?
}
@joshmiller83
joshmiller83 / template.php
Last active October 21, 2015 16:09
A quick example of how to use hook form alter to change the add to cart form.
<?php
/**
* Implements hook_form_alter()
*/
function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id,"cart_add_to_cart")) {
// load the node that is rendering the cart form
$node = node_load(array_pop(explode('_',$form_id)));
// hide the form
$form['#access'] = false;
@joshmiller83
joshmiller83 / template.php
Created October 21, 2015 16:19
A quick example of using theme_field to change the price field right before being rendered.
<?php
/**
* Implements theme_field()
*/
function THEMENAME_field__commerce_price($variables) {
if ($variables['items'][0]['#markup'] == '$0.00') {
return '<div class="commerce-product-field field-commerce-price sr-only">Call for pricing.</div>';
} else {
return;
}
@joshmiller83
joshmiller83 / hideprice.info
Last active October 21, 2015 19:32
Quick example module of how to hide prices for anonymous users. This could be installed as is or could be a good starter to copy/paste into your own custom glue module.
name = View Price Permissions
description = Add a permission to view prices. Allows you to hide the price for anonymous or other roles.
core = 7.x
dependencies[] = commerce_product
@joshmiller83
joshmiller83 / file.php
Last active October 30, 2015 16:35
Change Add to Cart on hook form alter when price is zero
<?php
/**
* Implements hook_form_alter()
*/
function MODULE_form_alter(&$form, &$form_state, $form_id) {
// Avoid bundle cart forms and other forms
if (strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0) {
// Use the price calculation system
$price = commerce_product_calculate_sell_price($form_state['default_product']);
// Some products don't have a price, like bundles. In this case we get "NULL".
@joshmiller83
joshmiller83 / file.php
Created November 16, 2015 23:15
Quickly decouple a user's current cart from Drupal Commerce 7.x
<?php
// Only meant for quick development issues where the current order
// is somehow borked.
global $user;
$order_id = commerce_cart_order_id($user->uid);
db_query("UPDATE commerce_order SET uid = 0 WHERE order_id=".$order_id)->execute();
@joshmiller83
joshmiller83 / clone_drupal_blocks.php
Last active November 19, 2015 17:07
Using this function you can clone drupal blocks from one theme to another for Drupal 7 only.
<?php
/**
* Clones blocks from one theme to another.
*
* @param $original
* The machine name of the original theme.
*
* @param $new
* The machine name of the new theme.
*/
@joshmiller83
joshmiller83 / gist:5033497
Created February 25, 2013 21:29
Tired of looking at the boring CSS for Drupalcon Portland comments? This will spice it up nicely:
.comments header {
width: 30%;
float: left;
margin-left: -210px;
}
.comments header p {
font-size: 12px;
color: #FFF;
margin-bottom: 0;
}
@joshmiller83
joshmiller83 / gist:5483972
Created April 29, 2013 19:14
Random Community Stats
// # of new committers
$commerce_module = "http://drupal.org/node/605898/committers";
echo return_new_committer_ul($commerce_module,'<a href="http://drupal.org/node/605898/committers">Drupal Commerce</a>');
$commerce_module = "http://drupal.org/node/1079066/committers";
echo return_new_committer_ul($commerce_module,'<a href="http://drupal.org/node/1079066/committers">Drupal Commerce Kickstart</a>');
function return_new_committer_ul ($url,$module) {
$doc = new DOMDocument();
<?php
/**
* Implements hook_form_FORM_ID_alter().
*
* This will work on all cart forms without running the global hook_form_alter().
*/
function MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
$product = $form_state['default_product'];
}