You are here

drupalplanet

Creating an action to update the prices of your commerce products

paulbooker's picture
Submitted by paulbooker on Fri, 05/22/2015 - 14:07
/**
 * Implements hook_action_info().
 */

function mymodule_action_info() {
  return array(
    'mymodule_update_products' => array(
      'type' => 'entity',
      'label' => t('Update products by 2%'),
      'configurable' => FALSE, 
      'triggers' => array('any'),
      'pass rows' => TRUE,
    ),
  );
} 
 
function mymodule_update_products(&$entity, $context) {	
  $product_id = $entity->product_id; 
  //dsm($product_id);	
  $price = $entity->commerce_price[LANGUAGE_NONE][0]['amount'];
  //dsm($price);
	
  $updated_price = 1.02 * $price;
  	
  $affected_rows = db_update('field_data_commerce_price')
    ->fields(array('commerce_price_amount' => $updated_price))
    ->condition('entity_id', $product_id)
    ->execute();
          
  //dsm($affected_rows);    
   
}
  
function mymodule_round_up_line_item_price($line_item_id) {
  $line_item = commerce_line_item_load($line_item_id);
  return round($line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'],-2);
}

Pages

Subscribe to RSS - drupalplanet