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'];
}
}
}
commerce_product
, or a product display node? If it's a commerce product you're checking the right var in the firstif
, but to make sure you're acting on the right entity type that should beif ($type == 'commerce_product' && $entity->type == 'package') {
. That won't (or shouldn't) make a difference to your code working or not thoughkrumo
orprint_r