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
@joshmiller83
joshmiller83 / ModulenameAddToCartForm.php
Last active May 31, 2019 15:53
Add to cart modifications for Commerce 2
<?php
// In folder `src/Form/`.
namespace Drupal\modulename\Form;
use Drupal\commerce\Context;
use Drupal\commerce_cart\Form\AddToCartForm;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Core\Form\FormStateInterface;
@joshmiller83
joshmiller83 / composer.json
Created January 31, 2018 01:17 — forked from andyshinn/composer.json
Docker Compose PHP Composer Example
{
"require": {
"mfacenet/hello-world": "v1.*"
}
}
@joshmiller83
joshmiller83 / commerce_2_order.php
Created September 28, 2016 15:12
Create an order programmatically for Drupal Commerce 2.x
<?php
$order_type = "default";
$store_id = 1;
$uid = 0;
$entity_type_manager = \Drupal::service('entity_type.manager');
$order_storage = $entity_type_manager->getStorage('commerce_order');
$new_order = $orderStorage->create([
'type' => $order_type,
'store_id' => $store_id,
<?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'];
}
@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 / 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 / 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 / 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 / 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;