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
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
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
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