5

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?

4

Prices are stocked as integer in database.

Try replacing 1.00 by 100 in your price array :

$values = array (
        'price' => 100 ,
        'currency_code' => commerce_default_currency() ,
    );

Cf : http://drupal.org/node/1124416

  • Thanks that makes perfect sense for the database, thanks for the link looks like a good read on this decision :) – imp Feb 21 '12 at 9:50
1

The number that's stored in a commerce price field is the price * 100.

So $1.00 is 100, $10 is 1000, $7.50 is 750, etc.

You could do 'amount' => $values[ 'price' ] * 100 , in commerce_installments_create_product and that would correct it.

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