1

From using commerce_price_savings_formatter, I've created field field_diskon where it only contains number. field_diskon purpose is for discount rate.

So, I can assign different discount rate for different SKU.

For getting value in field_specific_blabla attached to node, I can use:

// Extract the first field item
$specific_items = field_get_items('node', $node, 'field_specific_blabla');
$specific_item = array_shift($specific_items);

// Get the field value
$itemvalue = $specific_item['value'];

But how to get value field_diskon attached to commerce product field?

I have no idea what entity hold commerce field. I've tried replacing node with keyword related to commerce, but still not works.

2 Answers 2

2

You should be able to use entity_metadata_wrapper quite nicely for this. Note that it is part of the Entity API, but that is a dependency of Drupal Commerce so you'll be fine in this case.

Something like the following code should do it:

$wrapper = entity_metadata_wrapper('commerce_product', YOUR-REQUIRED-PRODUCT-ID);

$discount = $wrapper->field_diskon->value();

You can find out more about how to use entity_metadata_wrapper() here.

0

commerce_price_savings_formatter only handles the display of the price with discount, it doesn't provide a method for creating the discounted price (AFAIK). If you want a field that allows a sale price to be set per-product then you can use Commerce Sale Price. If you want to have a rule that applies to all products check out this page on product pricing rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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