1

I have a node display for more than one product variation. Normally the user has to select one of the variation options by selecting them from the drop-down list which rebuilds the node display via AJAX.
Now say for instance that I want to link one of the product variations from else where. If I simply link www.example.com/node/10 the default product variation will be displayed which might not be the one I would like to present to the user.

So I probably have to send a query parameter in the URL, for example www.example.com/node/10?product_id=15, and trigger the AJAX event programmatically in my custom module.

5 Answers 5

3

Please see Implement unique URLs for particular products on product displays. It is an open feature request for Commerce, and it also links to a working sandbox with the implementation: http://drupal.org/sandbox/maciej.zgadzaj/1845182

2

Sure here you go, you'd have to modify for you own implementation.

/**
 * Implements hook_commerce_product_reference_default_delta_alter().
 */
function location_variant_flag_commerce_product_reference_default_delta_alter(&$delta, $products){
    $lang = LANGUAGE_NONE;

    //load the node
    $node = node_load(arg(1));
    //only execute if the hide location variant field is set to 1
    if(count($node->hide_location_variant) > 0 && $node->hide_location_variant[$lang][0]['value'] == 1){
        //get the default product based on the users location
        $default_product = location_variant_flag_get_default_product_for_user_location($products);

        if($default_product){
            //DC doesn't always return the delta consistently, sometimes it is the product id and
            //sometimes it is the array key so check if array key 0 exists, if so return the array key
            //otherwise return the product ID
            if(isset($products[0])){
                foreach($products as $key=>$product){
                    if($product->product_id == $default_product->product_id){
                        $delta = $key;
                        break;
                    }
                }
            }
            else{
                $delta = $default_product->product_id;
            }
        }
    }
}
0
<?php
/**
 * Implementation of hook_form_alter()
 */
 function MYMODULE_form_alter(&$form, $form_state, $form_id) {
   if (strpos($form_id,'commerce_cart_add_to_cart_form_') !== FALSE) {
    $product_id = $form['product_id']['#default_value'];

    foreach(drupal_get_query_parameters() as $paramenter => $argument) {
      if ($paramenter == 'product_id') {
        $product_id = $argument;
      }
    }

    $form['product_id']['#default_value'] = $product_id;

    if (!isset($form_state['triggering_element'])) {
      $form['product_id']['#ajax']['event'] = 'MYMODULE_commerce_variation_trigger';
      drupal_add_js(drupal_get_path('module', 'MYMODULE') . '/MYMODULE.js',  'file');
    }
  }
}
?>

MYMODULE.js
(function ($) {
  $(document).ready(function() {
    $('#edit-product-id').trigger('MYMODULE_commerce_variation_trigger');
  });
} (jQuery));
0

You can quite easily do this in a custom module using hook_commerce_product_reference_default_delta_alter()

4
  • Here's an example implementation, might help someone.
    – user25794
    Commented Jun 19, 2015 at 1:12
  • Where is the example implementation? Commented Jun 19, 2015 at 1:39
  • I haven't tried your solution but it seems sensible and simple and might works. Commented Jun 19, 2015 at 1:42
  • You can improve this answer by adding a summary of an example implementation. Commented Jun 19, 2015 at 3:22
0

Just because I've come across this twice, if you have a custom module for your custom functionality here is a quick and easy example of sending in a product id or sku as a query parameter using hook_commerce_product_reference_default_delta_alter:

function myModule_commerce_product_reference_default_delta_alter(&$delta, $products){
    if(isset($_GET['pid']) || isset($_GET['sku'])){

        //if a "pid" or "sku" parameter exists loop through the products that are attached to the node
        foreach($products as $key=>$product){

            //if the product id or sku matches the current productm set the delta to the key
            if($product->product_id==$_GET['pid'] || $product->sku==$_GET['sku']) $delta=$key;
        }
    }
}

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.