1

Is there a way to change the default variation (e.g. size, color) for thousands of Drupal Commerce products on a site? I found the Commerce Default Product Variant module that does it on a per-product basis, but I need to do it globally.

I am open to doing it directly in MySQL if it won't break anything, but I am not sure how to do so at the moment.

1 Answer 1

1

Typically speaking, the default product variation is the first value in the reference field. You can see this in action on a vanilla install by creating a product display that references multiple products and switching the order of the SKUs in the product reference field (easiest if you're using the autocomplete widget). I doubt there's a way to change the order of your field values en masse, and I'm not sure there's a safe SQL query we can pass on to you.

However, if you're similarly open to writing custom module code to solve this, you can do so using hook_commerce_product_reference_default_delta_alter(&$delta, $products). The products variable will be an array of the fully loaded products on the display and the delta value will be initialized to the first one and available for alteration. You can inspect the field values of the products in the array to find the one whose attributes match what you want the default variation to be and update the delta accordingly.

(If you just wanted to write a script to actually update the storage instead of doing this on run-time, you'd want to process your product displays one at a time and do a similar product load / field value inspection to move the items around in the reference field's values so delta 0 was your desired default variation.)

4
  • Thanks! I am testing with the hook_commerce_product_reference_default_delta_alter and having partial success. The price changes, but the product variation select dropdowns do not. Obviously something wrong in what I'm doing. Will post back if I solve it.
    – Laryn
    Nov 2, 2017 at 16:45
  • 1
    Found a way to make this work and edited sample code into your answer. Thanks @ryan-szrama!
    – Laryn
    Nov 2, 2017 at 17:16
  • Awesome, glad it worked for you! It appears other moderators didn't want to include the code sample, but you can re-add it as a comment to this answer if you'd like to preserve it as an example for folks in the future. Nov 3, 2017 at 1:03
  • 1
    Here's the code that worked for me: gist.github.com/laryn/7a652afd5227f226ea174d8e8c5b8f08
    – Laryn
    Nov 3, 2017 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.