How do I get the correct price in the site's default currency to work like the the add product form does?
This function gives a product with a price of $0.01 I want $1.00.
I am sure this has to do with calculated price?
function generate_products () {
$product_type = 'product';
$values = array (
'price' => 1.00 ,
'currency_code' => commerce_default_currency() ,
);
$extras = array(
'status' => 1,
'uid' => 1,
'sku' => randomName ( 10 ),
'title' => randomName ( 10 ),
);
commerce_installments_create_product ($product_type,$values,$extras);
}
function commerce_installments_create_product ( $product_type , $values , $extras ) {
$form_state = array ();
$form_state[ 'values' ] = $values;
$form = array ();
$form[ '#parents' ] = array ();
// Generate a new product object
$new_product = commerce_product_new ( $product_type );
$new_product->status = $extras[ 'status' ];
$new_product->uid = $extras[ 'uid' ];
$new_product->sku = $extras[ 'sku' ];
$new_product->title = $extras[ 'title' ];
$new_product->created = $new_product->changed = time ();
if ( ! empty( $values[ 'original_order' ] ) ) {
// field_original_order[und][0][target_id]
$order = array ( LANGUAGE_NONE => array ( 0 => array ( 'target_id' => $values[ 'original_order' ] ) ) );
$form_state[ 'values' ][ 'field_original_order' ] = $order;
}
if ( ! empty( $values[ 'original_line_item' ] ) ) {
// field_original_line_item[und][0][target_id]
$line_item = array ( LANGUAGE_NONE => array ( 0 => array ( 'target_id' => $values[ 'original_line_item' ] ) ) );
$form_state[ 'values' ][ 'field_original_line_item' ] = $line_item;
}
if ( ! empty( $values[ 'original_product' ] ) ) {
$product = array ( LANGUAGE_NONE => array ( 0 => array ( 'target_id' => $values[ 'original_product' ] ) ) );
$form_state[ 'values' ][ 'field_original_product' ] = $product;
}
//commerce_price[und][0][amount]
$price = array ( LANGUAGE_NONE => array ( 0 => array (
'amount' => $values[ 'price' ] ,
'currency_code' => $values[ 'currency_code' ],
) ) );
$form_state[ 'values' ][ 'commerce_price' ] = $price;
// Notify field widgets to save their field data
field_attach_submit ( 'commerce_product' , $new_product , $form , $form_state );
commerce_product_save ( $new_product );
return $new_product->product_id;
}
//function based on answer here How do I programmatically create an entity with its fields?