Discussions

Sell Advert nodes - copying Product Display fields to new entity node

I have a product type called "Ad top". Two custom fields: Duration (taxonomy term: 1,3,6,9,12 months) and Pages (Taxonomy term: Homepage, Other pages).
I created 10 products of the Ad top to cover all variations of Duration and Pages, each product has different price.
Then I created a Product Display: "Top Ad" with reference to all 10 products. When I view it I have two Dropdowns: Duration and Pages. depending on the dropdowns price changes as expected.

Rules:
When order is paid in full I loop through all products in shopping cart.
Using Component - For each product I Create new entity Node of Content Type "Advertisement Top" that has fields Duration(taxonomy term) and Pages (taxonomy term).
What I want is to Set a data value of the Created Entity to values chosen on Product Display. But the values of these dropdowns are not accessible in Rules. Why the product fields are not accessible for Rules?

I tried using line item fields:
When I add custom fields Duration(taxonomy) and Pages(taxonomy) to Line Item /admin/commerce/config/line-items/product/fields
I can access value of these fields set at the Product Display node and I can easily Set a data value to new Entity. However this gives me 2x Duration and 2x Pages dropdowns that are not related to each other.
I know it is not the best solution but I am actually thinking about using product fields (dropdowns generated by Product Display) to get the correct price (pick the correct product) and whenever these dropdowns change I would copy the values to Line Item dropdowns. Then use Line Product fields to get the values and copy them to new Entity I create.
Does anybody have a better solution than this?

Finally I set the author of new node to basket owner and Save entity. This way user who bought the product automatically "creates" new node and is able to edit the content, set publishing date etc.

Posted: May 28, 2014

Comments

Thomas Griffiths on June 25, 2014

This may be the answer: https://www.drupal.org/project/commerce_node_checkout
Failing that, I've had to do pretty much what you describe - copying the contents of a field on the product display node into a field on the line item.
You can do this in a custom module with hook_form_alter:

function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {

  $line_item = $form_state['line_item'];
  $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);

  if ($product->type == 'my_product_type') {

// get the $node from the context of the add to cart form
    $node = node_load($form_state['context']['entity_id']);
// if options have been selected...
    if ($node -> field_my_field) {
        $vals = array();
        foreach ($node -> field_my_field['und'] as $value) {
          $vals[$value['value']] = $value['value'];
        }
// populate the line item field with the options that were selected for this node on its edit form
        $form['line_item_fields']['field_my_field']['und']['#options'] = $vals;
// limit the field to single select on the add to cart form
        $form['line_item_fields']['field_my_field']['und']['#type'] = 'radios';
    }
    else {
// hide the option (and cheat at preventing validation errors) when nothing was selected on the product display node
    $form['line_item_fields']['field_my_field']['#access'] = FALSE;
    }
  }
// uncomment these to find out what's going on in your form (you'll need the devel module installed):
//   dpm($form);
//   dpm($form_state);
}