1

I have a product type called package. It is a package of another product type which is called applications; when a user wants to create or update a package , I want to calculate the price of the package by adding the price of all applications in that package . I want to use hook_entity_presave(). but it doesn't work. I have put my function here.

My question: what should I check exactly against the $entity->type? My node type? Or my product type? It doesn't work either way.

function itunesdev_entity_presave(&$entity, $type) {
  if($entity->type == 'package') {
    $product_ids = array();   
    foreach($entity->field_products[LANGUAGE_NONE] as $key => $pid)
      $product_ids[] = $pid['target_id'];   
    $products = entity_load('commerce_product', $product_ids);   
    // reset default price
    $entity->commerce_price[LANGUAGE_NONE][0]['amount'] = 0;
    foreach($products as $pid => $product) {
      $entity->commerce_price[LANGUAGE_NONE][0]['amount'] += $product->commerce_price[LANGUAGE_NONE][0]['amount'];
    }
  }
}
6
  • are you getting any errors?
    – Aboodred1
    Jun 14, 2013 at 16:44
  • 1
    Could you elaborate on "doesn't work" a bit? Is the price of the package product being set to zero, or is not being altered at all? When you say you have a product of type "package", is that a commerce_product, or a product display node? If it's a commerce product you're checking the right var in the first if, but to make sure you're acting on the right entity type that should be if ($type == 'commerce_product' && $entity->type == 'package') {. That won't (or shouldn't) make a difference to your code working or not though
    – Clive
    Jun 14, 2013 at 17:08
  • I have a product type which called 'package' and a product display node which called 'package_display'. the worst part is that no kind of die or var_dump helps me to get the result and find out what's wrong? should I test my code by adding a new 'package_display' node or new 'package' product?
    – nooshinha
    Jun 14, 2013 at 17:14
  • @Aboodred1 I can't get any kind of result, even 'krumo' or die doesn't show up on save or update.
    – nooshinha
    Jun 14, 2013 at 17:16
  • 1
    @weber85 you need to remove & from &$entity argument then you'll be able test it using krumo or print_r
    – Aboodred1
    Jun 14, 2013 at 17:29

1 Answer 1

-3

Function hook_entity_presave acts on an entity before it is about to be created or updated

Code:

  function hook_entity_presave($entity, $type) {
    $entity->changed = REQUEST_TIME;
  }

source : Drupal API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.