5
Answers
Vote up!
0
Vote down!

Allow date field as product attribute

Hi All,

I've a requirement of allowing users to select a date before adding the product to cart and that date will be added by site editors while adding products in site.

Suppose there is product content A which has product B, product C and product D. Site editor has selected 3 different dates and set different prices for these products. On product display page user should be able to see all 3 dates in that product content A and select any of the date and then add to cart.

Currently Commerce module provides support for list type fields only.

I've implemented hook_form_alter and added settings ("Enable this field to function as an attribute field on Add to Cart forms.") for date field, that helped the field to be shown on product display page, but when the date field is shown on product display page it shows empty select list. When I've checked the code in commerce_cart.module file, below line returns values in timestamp

$used_options[$field_name][] = $product_wrapper->{$field_name}->raw();

And because of value received from above line the array_intersect_key() returns an empty array

array_intersect_key($data['options'], drupal_map_assoc($used_options[$field_name]))

does any one know how can i provide the values to this field on product display page.

If anyone knows please let me know how can I get this working. I've already spent a day with no output. I'll also keep updating here, if I found something.

Yogesh

Asked by: yogeshchaugule8
on September 10, 2013

5 Answers

Vote up!
1
Vote down!

Hi All,

Got the solution, here is how I've fixed it

I've implemented hook_options_list which completed my half of the work, i.e. add this date field on product display form for users to select before adding product into his cart.

function date_options_list($field, $instance, $entity_type, $entity) {
  return date_allowed_values($field);
}
function date_allowed_values($field) {
  db_select(); // Get all available dates for this field and return, rest everything will be done by Drupal Commerce.
  // One more thing to note down (which I missed earlier) is you've to return the key of array which is correspondent to what key drupal commerce is getting, that is mostly the unix timestamp in case of date.
  return $all_dates;
}

The only thing I've done wrong earlier was, I was returning dates (i.e. human readable date) because of which Drupal commerce was not able to match allowed dates keys in above array. Implementing these 2 functions solved my issue, and got the date field working as an attribute.

Hope this helps for those who might searching for the same.

Thanks to Drupal Commerce :)

Yogesh

Answer by: yogeshchaugule8
Posted: Sep 10, 2013
Vote up!
0
Vote down!

Great work Yogesh, I can now set the date up as an attribute.

However, could you help me with your db_query.... should I be selecting all of the date fields for my content type, or should this be selecting all of the dates that go with my particular product variations, that is, all products to be displayed on the current product display?

An example of your the select query would be great if possible.

Many thanks,

Steven

Answer by: sjs205
Posted: Oct 3, 2013

Comments

Glad that you got it working.

Here is a sample of query I've run to get all dates in product. In this query I'm selecting all dates available in the node that product attached in and returning those in array.

  <?php
   
function date_options_list($field, $instance, $entity_type, $entity) {
      if (!empty(
$entity->product_id)) {
        return
date_allowed_values($field, $entity->product_id);
      }
      return array();
    }
    function
date_allowed_values($field, $product_id = 0) {
     
// Get all available dates for this field and return, rest everything will be done by Drupal Commerce.
      // One more thing to note down (which I missed earlier) is you've to return the key of array which is correspondent to what key drupal commerce is getting, that is mostly the unix timestamp in case of date.
      // First select node id from product id to avoid conflict of same key in array.
     
$product_query = db_select('node');
     
$product_query->join('field_data_field_product', 'fdfp', 'fdfp.entity_id = node.nid'); // field_product is product ref field in my content type.
     
$product_query->condition('fdfp.field_product_product_id', $product_id)
                            ->
fields('node', array('nid'));
     
$nid = $product_query->execute()->fetchField();

      if (empty(
$nid)) {
        return array();
      }

     
$query = db_select('field_data_field_product_date', 'fdfcd'); // field_product_date is my date field in product
     
$query->join('commerce_product', 'cp', 'cp.product_id = fdfcd.entity_id'); // Join with product ID
     
$query->join('field_data_field_product', 'fdfp', 'fdfp.field_product_product_id = cp.product_id');
     
$query->condition('fdfcd.bundle', 'product')
                ->
condition('fdfp.entity_id', $nid) // filter results with content id
               
->fields('fdfcd', 'field_product_date_value'); // Your date field coloumn
     
$all_dates_result = $query->execute();

     
// Loop through all results and get the array of values to return
     
while ($date = $all_dates_result->fetchObject()) {
       
// If date is not in UNIXTIMESTAMP format.
       
if ($field['type'] != 'datestamp') {
         
$date->field_product_date_value = strtotime($date->field_product_date_value);
        }
       
$all_dates[$date->field_product_date_value] = format_date($date->field_product_date_value, 'medium');
      }

      return
$all_dates;
    }
 
?>

Yogesh

- yogeshchaugule8 on November 13, 2013

Hi Yogesh, when I tried to run your code on my test site, an error code says the $product_id is undefined, which located in this line:
$product_query->condition('fdfp.field_product_product_id', $product_id)->fields('node', array('nid'));
can you provide me with your original code? Without this function, I can only see the drop down list, but there's nothing in the list.
Another question is, the Search API within the Commerce Profile provide the Date search by Unix time format, how can I change it to date like 'YYYY-MM-DD'? Thanks

- zliu95 on November 6, 2013

Hi Yogesh, when I tried to run your code on my test site, an error code says the $product_id is undefined, which located in this line:
$product_query->condition('fdfp.field_product_product_id', $product_id)->fields('node', array('nid'));
can you provide me with your original code? Without this function, I can only see the drop down list, but there's nothing in the list.
Another question is, the Search API within the Commerce Profile provide the Date search by Unix time format, how can I change it to date like 'YYYY-MM-DD'? Thanks

- zliu95 on November 6, 2013
Vote up!
0
Vote down!

Hi zliu,

Thanks for pointing out the error, I've missed the second argument in function definition in above comment, I've rectified the error and updated comment.

I can not copy paste the actual code here, that is completely project specific and there are few extra things in that code which are not generic.

Yogesh

Answer by: yogeshchaugule8
Posted: Nov 13, 2013
Vote up!
0
Vote down!

Hi yogeshchaugule8,
First of all, thanks for your work. I am having problems with your module. I think i am doing something wrong, maybe introducing Product Reference Field Name (i try with variation type name and field name). Can you help me?

Thanks

Answer by: Gorka B
Posted: Oct 22, 2014