Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active November 16, 2022 14:08

Revisions

  1. BBGuy revised this gist Jun 24, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,9 @@
    // order currency code & amount from wrapper
    $currency_code = $order_wrapper->commerce_order_total->currency_code->value();
    $order_total = $order_wrapper->commerce_order_total->amount->value();
    // Get as a decimal value
    $price_decimal = commerce_currency_amount_to_decimal($order_total, $currency_code);



    // subtotal without vat
  2. BBGuy revised this gist Jun 24, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -169,6 +169,8 @@

    // shipping method display title
    $line_item_title = $shipping_data['shipping_service']['title'];
    // total price
    $line_item_price = $line_item_wrapper->commerce_total->amount->value();

    }

  3. BBGuy revised this gist Nov 27, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -43,8 +43,9 @@
    // Order total (cents).
    $order_total = $order_total_obj['amount'];

    // order currency code
    // order currency code & amount from wrapper
    $currency_code = $order_wrapper->commerce_order_total->currency_code->value();
    $order_total = $order_wrapper->commerce_order_total->amount->value();


    // subtotal without vat
  4. BBGuy revised this gist Nov 26, 2019. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -78,9 +78,22 @@
    if (isset($address['organisation_name'])) {
    $organisation_name = $address['organisation_name'];
    }


    // Format the address.
    $handlers = array('address' => 'address');
    $context = array('mode' => 'render');
    $address_render_array = addressfield_generate($address, $handlers, $context);
    $output = drupal_render($address_render_array);

    // Format the address with full name (more compact code) note: $context defaults to render.
    $address_name_array = addressfield_generate($address, array('address' => 'name-oneline'));
    $address_array = addressfield_generate($address, array('address' => 'address'));
    $output = drupal_render($address_name_array);
    $output .= drupal_render($address_array);

    }


    // Line item


  5. BBGuy revised this gist Jan 24, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -93,6 +93,9 @@
    // line item type using the bundle.
    $line_item_type = $line_item_wrapper->getBundle();

    // Line Item ID
    $line_id = $line_item_wrapper->getIdentifier();

    // check if a product.
    if (in_array($line_item_type, commerce_product_line_item_types())) {

  6. BBGuy revised this gist Jan 8, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -169,6 +169,11 @@
    }
    }

    // Create a line item / Add product to cart programmatically
    commerce_cart_product_add_by_id($product_id, $quantity);



    // Product.
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

  7. BBGuy revised this gist Jan 8, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -116,8 +116,11 @@

    // Product

    // Product ID.
    // Product ID - You can get the product id from its property.
    $product_id = $line_item_wrapper->commerce_product->product_id->value();
    // But its better prectice to use getIdentifier()
    $product_id = $line_item_wrapper->commerce_product->getIdentifier();


    // Product SKU.
    $sku = $line_item_wrapper->commerce_product->sku->value();
  8. BBGuy revised this gist Jul 13, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -188,6 +188,11 @@
    // Set the new stock level.
    $wrapper->commerce_stock->set($new_stock);

    // Vat & tax
    $p = 1234 // 12.34
    $p = $p * (1 + VAT_RATE); // Add VAT. VAT_RATE a predifined value: define('VAT_RATE', 0.2);
    $p = commerce_round(COMMERCE_ROUND_HALF_DOWN, $p); // Use commerce round function


    // Taxonomy – term referenced
    // Get the term object.
  9. BBGuy revised this gist Apr 27, 2017. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,15 @@
    $uid = $order->uid;
    $email = $order->mail;


    $user = user_load_by_mail($email);
    if ($user) {
    $u_wrapper = entity_metadata_wrapper('user', $user);
    $u_wrapper->field_xxx = $x;
    $u_wrapper->save();
    }


    // order total object
    $order_total_obj = $order_wrapper->commerce_order_total->value();
    // Order total (cents).
    @@ -230,6 +239,7 @@ function custmodule_commerce_order_presave($order) {
    }
    }

    // Other (not D commerce)

    // Create a field collection item and set its fields using the entity_metadata_wrapper
    // Working on $node field name: field_my_field.
    @@ -246,4 +256,9 @@ function custmodule_commerce_order_presave($order) {
    $fc_wrapper->save();



    // entity reference (not using emw) both the below are the same.
    $entity['field_xx'][LANGUAGE_NONE][0]['target_id'] = $x;
    $entity['field_xx'][LANGUAGE_NONE][0] = array('target_id' => $x, 'target_type' => 'node');

    // Using emw
    $e_wrapper->field_user_ldl_account->set($account_id);
  10. BBGuy revised this gist Mar 29, 2017. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -229,4 +229,21 @@ function custmodule_commerce_order_presave($order) {
    }
    }
    }


    // Create a field collection item and set its fields using the entity_metadata_wrapper
    // Working on $node field name: field_my_field.
    // Create the field_collection item entity.
    $fc_entity = entity_create('field_collection_item', array('field_name' => 'field_my_field'));
    // Attach the field_collection item entity to the node.
    $fc_entity->setHostEntity('node', $node);
    // Use an entity_metadata_wrapper.
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_entity);
    // Set the fields.
    $fc_wrapper->field1 = $field1;
    $fc_wrapper->field2 = $field2;
    // Save.
    $fc_wrapper->save();



  11. BBGuy revised this gist Mar 10, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,9 @@
    // Order id.
    $order_id = order_wrapper->order_id->value();

    // Order Number.
    $order_id = order_wrapper->order_number->value();

    // User detils.
    $uid = $order->uid;
    $email = $order->mail;
    @@ -214,4 +217,16 @@
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $uid = $order_wrapper->uid->value();

    // Change the order number.
    function custmodule_commerce_order_presave($order) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    if (isset($order->order_id)) {
    if ($x) {
    $order->order_number = sprintf('%d_x', $order->order_id);
    }
    else {
    $order->order_number = $order->order_id);
    }
    }
    }

  12. BBGuy revised this gist Feb 21, 2017. 1 changed file with 30 additions and 1 deletion.
    31 changes: 30 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,20 @@
    <?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.
    $wrapper->field_x = $value;
    // Or
    $wrapper->field_x->set($value);

    // To save the entity
    $wrapper->save();

    // order.

    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    @@ -174,11 +189,25 @@
    $term_wrapper = $product_w->field_availability;
    $term_name = $term_wrapper->name->value());

    // Multi value term reference fields.
    // Read multi value term reference fields.
    $terms_wrapper = $product_w->field_availability;
    foreach ($term_wrapper as $term) {
    $ex_list[$term->tid] = $term->name;
    }
    // Write (add) a single multi value field
    $x_wrapper->x_field[] = $new_value;
    // Write (update second value) a single multi value field
    $x_wrapper->x_field[1] = $new_value;
    // Write (overwrite all values) a single multi value field
    $new_list = array(
    0 => $value1,
    1 => $value2,
    );
    $x_wrapper->x_field = $new_list;
    // Clear multi value field
    $x_wrapper->x_field->set();
    // or:
    $x_wrapper->x_field = array();

    // Get the user from a line item
    $order = commerce_order_load($line_item->order_id);
  13. BBGuy revised this gist Jan 6, 2017. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -150,7 +150,18 @@
    $product_wrapper->commerce_stock_override->value()
    $product_wrapper->commerce_stock->value()


    // Create a new product
    $wrapper = entity_metadata_wrapper('commerce_product', commerce_product_new('product'));
    $wrapper->title = $title;
    $wrapper->sku = $sku;
    // Set the price.
    $wrapper->commerce_price->amount = $price;
    $wrapper->commerce_price->currency_code = 'GBP';
    // or $wrapper->commerce_price->amount->set($price);
    // Set the new stock level.
    $wrapper->commerce_stock->set($new_stock);


    // Taxonomy – term referenced
    // Get the term object.
    $term = $product_wrapper->field_availability->value();
  14. BBGuy revised this gist May 17, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -163,6 +163,11 @@
    $term_wrapper = $product_w->field_availability;
    $term_name = $term_wrapper->name->value());

    // Multi value term reference fields.
    $terms_wrapper = $product_w->field_availability;
    foreach ($term_wrapper as $term) {
    $ex_list[$term->tid] = $term->name;
    }

    // Get the user from a line item
    $order = commerce_order_load($line_item->order_id);
  15. BBGuy revised this gist Sep 17, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,12 @@
    $customer_billing_wrapper = $order_wrapper->commerce_customer_billing;
    // Get the address
    $address = $customer_billing_wrapper->commerce_customer_address->value();
    }
    // Get organisation name if available.
    if (isset($address['organisation_name'])) {
    $organisation_name = $address['organisation_name'];
    }

    }

    // Line item

  16. BBGuy revised this gist Aug 24, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,10 @@
    // Order id.
    $order_id = order_wrapper->order_id->value();

    // User detils.
    $uid = $order->uid;
    $email = $order->mail;

    // order total object
    $order_total_obj = $order_wrapper->commerce_order_total->value();
    // Order total (cents).
  17. BBGuy revised this gist Apr 15, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -142,8 +142,22 @@
    $product_wrapper->commerce_stock->value()


    // Taxonomy – term referenced
    // Get the term object.
    $term = $product_wrapper->field_availability->value();
    // Get the taxonomy term name
    $term_name = term->name;
    // One liner of the above.
    $term_name = $product_wrapper->field_availability->value()->name;

    // Taxonomy – term referenced using a term wrapper
    $term_wrapper = $product_w->field_availability;
    $term_name = $term_wrapper->name->value());


    // Get the user from a line item
    $order = commerce_order_load($line_item->order_id);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $uid = $order_wrapper->uid->value();


  18. BBGuy revised this gist Feb 4, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -140,5 +140,10 @@
    // Stock
    $product_wrapper->commerce_stock_override->value()
    $product_wrapper->commerce_stock->value()



    // Get the user from a line item
    $order = commerce_order_load($line_item->order_id);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $uid = $order_wrapper->uid->value();

  19. BBGuy revised this gist Jan 8, 2015. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -29,12 +29,21 @@
    $sub_total = commerce_currency_format($sub_total, $currency_code);

    // Order state
    $order_status = commerce_order_status_load($order->status);
    $order_state = $order_status['state'];
    if ($order_state != 'cart') {
    $order_status = commerce_order_status_load($order->status);
    $order_state = $order_status['state'];
    if ($order_state != 'cart') {
    ....
    }
    }

    // Get the customer billing profile (this is not the wrapper object for the wrapper object don't use the ->value())
    $customer_billing = $order_wrapper->commerce_customer_billing->value();
    // Make sure it exists
    if (isset($customer_billing)) {
    // Get the customer billing profile wrapper
    $customer_billing_wrapper = $order_wrapper->commerce_customer_billing;
    // Get the address
    $address = $customer_billing_wrapper->commerce_customer_address->value();
    }

    // Line item

  20. BBGuy revised this gist Jan 8, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,12 @@
    // formatted price string with currency sign
    $sub_total = commerce_currency_format($sub_total, $currency_code);

    // Order state
    $order_status = commerce_order_status_load($order->status);
    $order_state = $order_status['state'];
    if ($order_state != 'cart') {
    ....
    }


    // Line item
  21. BBGuy revised this gist Nov 18, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,9 @@

    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

    // Order id.
    $order_id = order_wrapper->order_id->value();

    // order total object
    $order_total_obj = $order_wrapper->commerce_order_total->value();
    // Order total (cents).
  22. BBGuy revised this gist Nov 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@
    $line_item_type = $line_item_wrapper->getBundle();

    // check if a product.
    if (in_array(line_item_type, commerce_product_line_item_types())) {
    if (in_array($line_item_type, commerce_product_line_item_types())) {

    // line item title
    $line_item_title = $line_item_wrapper->commerce_product->title->value();
  23. BBGuy created this gist Nov 18, 2014.
    126 changes: 126 additions & 0 deletions dc_emw.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    <?php

    // order.

    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

    // order total object
    $order_total_obj = $order_wrapper->commerce_order_total->value();
    // Order total (cents).
    $order_total = $order_total_obj['amount'];

    // order currency code
    $currency_code = $order_wrapper->commerce_order_total->currency_code->value();


    // subtotal without vat
    $total_ex_vat = commerce_price_component_total($order_total , 'base_price');

    // total tax (in this case vat 20%)
    $total_vat = commerce_price_component_total($order_total , 'tax|vat_20');

    // subtotal with vat
    $sub_total = $total_ex_vat['amount'] + $total_vat['amount'];

    // formatted price string with currency sign
    $sub_total = commerce_currency_format($sub_total, $currency_code);



    // Line item


    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

    // get all line item wrappers from order wrapper
    foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
    // line item type
    $line_item_type = $line_item_wrapper->type->value();
    // line item type using the bundle.
    $line_item_type = $line_item_wrapper->getBundle();

    // check if a product.
    if (in_array(line_item_type, commerce_product_line_item_types())) {

    // line item title
    $line_item_title = $line_item_wrapper->commerce_product->title->value();

    // line item unit price
    $line_item_unit_price = $line_item_wrapper->commerce_unit_price->amount->value();

    // line item formatted unit price
    $line_item_unit_price = commerce_currency_format($line_item_unit_price, $currency_code);

    // line item quantity
    $line_item_quantity = (float)$line_item_wrapper->quantity->value();

    // line item total price
    $line_item_total_price = $line_item_wrapper->commerce_total->amount->value();

    // line item formatted total price
    $line_iten_total_price = commerce_currency_format($line_item_total_price, $currency_code);

    // Product

    // Product ID.
    $product_id = $line_item_wrapper->commerce_product->product_id->value();

    // Product SKU.
    $sku = $line_item_wrapper->commerce_product->sku->value();

    // Product image
    $line_item_wrapper->commerce_product->field_product_image->value();

    // Load the product.
    $product = commerce_product_load($product_id);


    } else {
    // for shipping & discount line items

    // shipping/discount price
    $line_item_price = $line_item_wrapper->commerce_unit_price->amount->value();

    // shipping/discount formatted price
    $line_item_price = commerce_currency_format($line_item_price, $currency_code);

    // line item TYPE display title
    $line_ite_type_title = commerce_line_item_type_get_name($line_item_wrapper->type->value());

    // for shipping line items
    if ($line_item_wrapper->type->value() == "shipping") {

    // shipping data
    $shipping_data = $line_item_wrapper->value()->data;

    // shipping method display title
    $line_item_title = $shipping_data['shipping_service']['title'];

    }

    if ($line_item_wrapper->type->value() == 'commerce_discount') {
    // for discount line items

    // get discount display title (I suspect there might be a better way than this)
    $discount_data = $line_item_wrapper->commerce_unit_price->data->value();
    foreach ($discount_data['components'] as $key => $component) {
    if (!empty($component['price']['data']['discount_component_title'])) {
    $line_item_title = $component['price']['data']['discount_component_title'];
    }
    }
    }
    }
    }

    // Product.
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

    // Product Price.
    $price_amount = $product_wrapper>commerce_price->amount->value();
    $product_wrapper>commerce_price->amount->set($new_price);

    // Stock
    $product_wrapper->commerce_stock_override->value()
    $product_wrapper->commerce_stock->value()