Skip to content

Instantly share code, notes, and snippets.

View BBGuy's full-sized avatar

Guy Schneerson BBGuy

View GitHub Profile
<?php
// Get the request object.
$request = \Drupal::request();
// Query - get all parameter.
$q = $request->query->all();
// Check if a query parameter exists.
if ($request->query->has('commerce_product_v_id')) {
// Get the query parameter.
$pvid = $request->query->get('commerce_product_v_id');
@BBGuy
BBGuy / dc_add_default_product.module
Created August 8, 2016 14:40
Create a default producr if one was not set.
/**
* Implements hook_form_alter().
*/
function dwwcommerce_form_alter(&$form, &$form_state, $form_id) {
// The style edit form.
if ($form_id == 'product_style_node_form') {
// Add a custom submit.
$form['actions']['submit']['#submit'][] = 'mymodule_node_prod_display_form_submit';
}
}
<?php
// some examples on
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/product-architecture/code-recipes
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/product-management/code-recipes
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/displaying-products/code-recipes
// Work with orders.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($order_id);
@BBGuy
BBGuy / commerce_tools.php
Last active January 11, 2018 17:33
Drupal 7 commerce utility functionality
<?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();
@BBGuy
BBGuy / gist:592f191e2ab120f75df0
Last active August 29, 2015 14:20
Working with Images in Drupal
// Using the Entity API
$image = $MY_ENITIY->field_image->value();
$variables = array(
'style_name' => 'my_style__img',
'path' => $centre['image']['uri'],
'width' => $centre['image']['width'],
'height' => $centre['image']['height'],
);
$img = theme('image_style', $variables );
@BBGuy
BBGuy / dc_emw.php
Last active November 16, 2022 14:08
Working with Drupal commerce entities using entity_metadata_wrapper. Just a bunch of exampled copied from the web or added by me. will keep adding over time. now more then just commerce entities
<?php
// Basics
// Get a value.
$value = $wrapper->field_x->value();
// If the field is a reference field you will get the field object
// To get the wrapper back use:
$wrapper_b = $wrapper->field_ref;
// To set a value.
@BBGuy
BBGuy / drush
Created August 14, 2014 09:00
Sort out Drupal 7: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions()
drush vset comment_maintain_node_statistics TRUE
@BBGuy
BBGuy / drupal_commerce_order_total_components_1.php
Last active August 29, 2015 14:00
Drupal Commerce Order Total components
<?php
// Get a nicely formatted amount for Ex VAT & Vat order totals.
// Get the order wrapper
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Order total.
$order_total = $order_wrapper->commerce_order_total->value();
// Get ex vat amount.
@BBGuy
BBGuy / gist:7875084
Created December 9, 2013 16:23
Drupal Commerce Localization - translate the currency symbol
<?php
/**
* Implementation of hook_commerce_currency_info_alter().
*/
function MyModule_commerce_currency_info_alter(&$currencies, $langcode) {
// Add a curreny format callback so we can take over formating.
$currencies['RUB']['format_callback'] = 'MyModule_commerce_currency_format';
}
@BBGuy
BBGuy / drupal_commerce_localization.sh
Last active December 19, 2015 11:48
The following is a script to download and install Drupal Commerce localization modules
drush dl entity_translation
drush en entity_translation -y
drush dl title
drush en title -y
drush dl variable
drush en variable -y
drush dl i18n
drush en i18n, i18n_translation, i18n_field, i18n_menu -y