1

I am using date repeat entity module in a content type say Movies to successfully clone the nodes.I also have a commerce product reference field in that content type which is working fine when I am not using repeat date. I want to be able to create a new commerce product content for each node in the series when using repeat date.(while creating the Movies node I can see product fields like SKU,product title etc.)

For example, I am creating a new Movies having start as 1st Jan 2016 and end date as 2nd Jan 2016.I want to repeat this event for every Friday till 31st Jan. Now there will be 5 nodes created automatically. how do i create a new commerce product content for each of these nodes?

Thanks.

6
  • 1
    Are you integrating with any third party system? Is there a specific reason for why you are recreating the nodes?
    – nvahalik
    Commented Feb 10, 2016 at 13:18
  • With regards to the Product entity being reference with the entity reference field, does this need to be cloned too or will you just be referencing the same product for each clone? Commented Feb 10, 2016 at 13:23
  • @nvahalik No,there is no third party system,just these modules.i want to create these nodes automatically( say each Friday for a year or two),each having add to cart available,each node may have different product title ,sku and price etc. Commented Feb 10, 2016 at 13:41
  • @AlexK yes,the product content needs to be different( different title,sku and price) which is referenced to each node. Commented Feb 10, 2016 at 13:43
  • Nodes don't have SKUs or prices—are you talking about creating a whole node/product combination, just the nodes, or just the products?
    – nvahalik
    Commented Feb 10, 2016 at 14:02

1 Answer 1

0

I'm not too familiar with Date repeat entity module, but why don't you try implementing hook_node_insert() in a custom module, to handle creating a new product every time a a new node is created:

/**
 * Implements hook_node_insert().
 */
function my_module_node_insert($node) {

  //check type and that no product set previously
  if($node->type == 'my_type') {

    //create product
    $cp = commerce_product_new('product');
    $cp->is_new = TRUE;
    $cp->revision_id = NULL;
    $cp->uid = 1;
    $cp->status = 1;
    $cp->created = $cp->changed = time();
    $cp->sku = 'My SKU';
    $cp->title = 'My Product';
    $cp->language = LANGUAGE_NONE;
    $cp->commerce_price = array(LANGUAGE_NONE => array( 0 => array(
      'amount' => 1000, //$10.00
      'currency_code' => 'USD',
    )));

    //save new product
    commerce_product_save($cp);

    //set reference field
    $node->field_product['und'][0]['product_id'] = $cp->product_id;

    //update node
    node_save($node);
  }
}

I haven't tested this code, but this would be the general approach I would try.

This would only work if the Date repeat module invoked this hook though.

4
  • This sounds like a good idea, but I am not too familiar with creating or editing a module.Could you provide more detail please?How do u find the place to invoke in the entity module? Thanks for your time. Commented Feb 11, 2016 at 6:00
  • Oh OK, then I would recommend doing some tutorials on how to build your first module. There are plenty of great examples: drupal.org/project/examples curveagency.com/blog/using-hooks Commented Feb 11, 2016 at 7:24
  • 1
    Okay so I followed your steps and went through that example,This code worked perfectly except I didn't use !isset because productid is already assigned everytime.Superb,Thanks a lot :) Commented Feb 17, 2016 at 13:33
  • great! glad I could help :) adjusted my answer code accordingly Commented Feb 18, 2016 at 14:32

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.