Skip to main content
  • Home
  • Womblog
  • Contact
  • Login

By Wombats logo By Wombats

You should see them code...

Bypassing Drupal Commerce customer profile duplication

In:
  • Drupal
  • Drupal Commerce
12Apr2013

In Drupal Commerce, we deal in entities, fields, and field-based relationships between entities (i.e. references). The extent to which we implemented our data model on these systems from the earliest days of Drupal 7 is what grants Drupal Commerce developers a level of flexibility previously unavailable to eCommerce developers in general. Cool, right?

Unfortunately, the Drupal entity trade can be dangerous when dealing with historical data that isn't supposed to change - when a reference should do more than merely "refer" but also express constraints.

Such is the case with customer profile references on orders.

Once customer information (e.g. a billing or shipping address) is entered for an order, we don't want the order to lose that information at a later date. This means we have to prevent not just the deletion of the referenced customer profiles but also changes to them - the latter because our references are to entitiies, not revisions of entities.

Side note: honestly, I'm fine with that - I can't imagine a reasonable UI or update strategy for entity revision references. It's hard enough to keep straight as is.

The most common place users encounter customer profile duplication is when they go to update a customer profile through the UI. If any field values are changed, the Order module will prevent the update if it detects the customer profile is referenced by a non-shopping cart order. It empties the profile IDs in a presave hook, forcing the save to create a new customer profile instead of updating the existing one.

This same process affects not just customer profiles being edited through the UI but also customer profiles being updated through code. However, if you combine the fact that customer profiles have revisions with the absence of the duplication related messages you see when performing the operation in the UI, it's easy to see how this functionality might appear to developers as a case of revisions gone awry.

There are a couple of functions developers can refer to to read comments describing the process and see the implementation itself:

  • commerce_order_commerce_customer_profile_presave()
  • commerce_order_commerce_customer_profile_can_delete()

It's worth noting that we've coded this functionality to be paranoid - if there's a slight chance something substantive may have changed in the field data, we force duplication instead of updating the original. Better to duplicate than to lose vital data.

Still, the keen observer will note that we actually do permit customer profiles to be updated through the UI on one condition. If an administrator edits a customer profile through the edit form of the sole order referencing the profile, we permit the update.

You may have a need in custom code to perform an update to a customer profile field that you know does not affect the historical record in a substantive way. Maybe it's simply a matter of changing some internal field that has no bearing on the fulfillment of orders. In such cases, to bypass customer profile duplication, you can imitate the process the order edit form uses to identify an order as safe to delete.

To do this, add a temporary "entity_context" property to the customer profile object. This is the property the Order module looks for in the presave hook to determine if the customer profile is being edited in the context of its sole referencing order. If you properly identify this order in the entity_context, the Order module will permit the update to occur without duplication:

<?php
$profile
= commerce_customer_profile_load(1);
$wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
$wrapper->field_referral_source = 'MySpace';
$profile->entity_context = array(
 
'entity_type' => 'commerce_order',
 
'entity_id' => 1,
);
commerce_customer_profile_save($profile);
?>

Obviously, this is obtuse.

In Commerce 2.x, we'll do well to improve the developer experience here. It gets even worse if you want to make such an update on a site using Commerce Addressbook where you do want to update a customer profile referenced by multiple orders without enacting duplication. In fact, until I wrote this post, there would have been no way to achieve this short of a miraculous hook_query_alter().

This whole post came out of a quick e-mail exchange with Forest Mars, whom I'm looking forward to meeting at next week's Florida DrupalCamp (you going?). Since I've had a chance to think about the issue in a longer form post, I'm going to go ahead and add a query tag so it's at least possible to target the "can delete" query without committing developer sins.

And that is how e-mail and blogging improve open source projects, my friends!

  • Add new comment

Learn Front End Development at The Iron Yard Academy

In:
  • Drupal
  • Greenville
10Mar2013

A year ago I moved to Greenville, SC to live closer to family. I haven't looked back since, and I've fallen more and more in love with my new hometown every month. I had cursory knowledge of its great sights, food, culture, and climate before we moved, but the big surprise has been the city's vibrant tech community.

A few months after moving, I discovered The Iron Yard, which at the time was running a three month startup accelerator. Peter and the crew were all very welcoming and invited me to hang out with the teams giving and receiving in turn... and of course occasionally talking Drupal. I wrote more about their program and space on my therapeutic moving blog, One Ville to Another.

In recent months, The Iron Yard merged with this awesome co-working space (creatively ) named CoWork. They then scored a 30,000 sq ft office space ("the forge") in the heart of our wonderful downtown to gather not just their programs but also like minded design, development, and creative firms into a single space. They also launched a Coder Dojo to teach kids web development (along with Scratch game programming and Arduino hacking) and are now expanding the school to education for adults, too. Hence the blog post.

Starting next month, The Iron Yard Academy will take a small class of folks through an intense, full-time, three month course in front end development. The two teacher-mentors will be Mason Stewart and Marco Suarez, a killer development / design pair who recently launched the attractive Etsy curation site Haberdash Fox.

Their class will cover the basics (HTML, CSS, JavaScript) and quickly move into pertinent topics for front-end Drupal development, especially with Drupal 8 - Git / the command line, HTML5, jQuery, Backbone.js, responsive design, and more.

In addition to the course, students will get a MacBook Air, lodging in the city (if needed), access to The Iron Yard mentors, and guaranteed job placement (again, if needed) within a year doing front-end development in a startup - hopefully locally. The whole course is only $9,750 and will be repeated a couple more times this year if you can't make it so soon.

The program is definitely open to students who already have experience in web development, even if you're currently employed. If you have an underutilized dev who wants to super charge their front end skills, I'd highly recommend sending them to learn and grow in this great community. I look forward to meeting the new class, and I hope some of the new talent makes its way into the Drupal community.

  • 4 comments

Shinar, Oklahoma

In:
  • Bible
  • Minisagas
3Mar2013

Brick by brick we built a beacon, a Tower on the Great Plains in defense against the vastness. "Leave Manifest Destiny to those unafraid to die! Dwell in security." But those bricks could not save us from that confusing wind. Tongues and spirits broken, we depart to die in obscurity.

This is my second minisaga, written to fulfill in small part a vision I've had for a while of the story of Genesis 11 played out on the Great Plains in the 19th century. The vision isn't clear enough for a full story, but I hope it was clear enough for 50 words.

  • Add new comment

Other recent blog posts:

  • No Greater Love
  • Toward HyperDrupal
  • Commerce Shipping brings a lot to the table
  • Executing arbitrary JavaScript from a Forms API #ajax callback
  • Next Steps for Drupal Commerce
  • On Cats, Bags, and Letting it all out
  • Updating Completed Order Timestamps in Drupal Commerce
more
Entries (RSS)

About Ryan

Ryan Szrama is a Drupal e-commerce developer for Commerce Guys, focusing on Drupal Commerce. Aside from his work, he loves his wife, his daughter, his church, and a good book over a white mocha.

You can find him elsewhere online at:

Commerce Guys logo Druplicon Twitter

Find me at these upcoming events...

I'll be presenting Drupal Commerce at the following events and sponsoring some with Commerce Guys. Be sure to say hi!

I'm speaking at Florida DrupalCamp
Speaking and hacking at
FL DrupalCamp, April 20-21
I'm a Trainer at DrupalCon Portland
Training and speaking at
DrupalCon Portland May 20-24

Some of my other sites...

Find more content from me and my wife at a couple of our other websites:

  • The Definitive Guide to Drupal 7
  • My raw milk cheese e-commerce site
  • My roguelike / game development blog
  • Christina's children's book reviews
Bible Books Christianity Christina Drupal Drupal Commerce Drupalcon drupalconszeged2008 Family Homeownership Marriage Ministry Music Programming Space Ubercamp Ubercart Work
more tags
Creative Commons License © 2009 Ryan Szrama

Drupal port by 3rdWorld : Created by Design Disease