Tags/topics: 
1
Answers
Vote up!
0
Vote down!

programmically change price of product

I want have a product that user can change the price of it ,I want programmically change the price of product when user submit a custom form with entered price.
really I dont know how can I achieve it.
appreciate any urgency help

Asked by: zhilevan
on December 15, 2013

1 Answer

Vote up!
0
Vote down!

To answer your full question, you can do this with line item fields:

But as I originally read your comment, you can change the base price of a product using code that looks similar to this:

<?php
// Need these a couple of times...
$entity_id = 3;
$new_price = 4000; // $40 in minor units

// Load entity
$entity = entity_load('commerce_product',array($entity_id));

// Create Wrapper
$product = entity_metadata_wrapper('commerce_product', $entity[$entity_id]);

// Before change, using the devel module's dpm() function
dpm($product->commerce_price->value());

// Change price
$product->commerce_price->amount = 4000;

// Save this change
$product->save();

// Test saved data
$entity2 = entity_load('commerce_product',array($entity_id));
$product2 = entity_metadata_wrapper('commerce_product', $entity[$entity_id]);
dpm($product2->commerce_price->value());
?>
Josh Miller
Answer by: Josh Miller
Posted: Dec 17, 2013