commerce_amws-8.x-1.x-dev/modules/order/src/Plugin/EntitySync/FieldTransformer/OrderItemUnitPrice.php
modules/order/src/Plugin/EntitySync/FieldTransformer/OrderItemUnitPrice.php
<?php
namespace Drupal\commerce_amws_order\Plugin\EntitySync\FieldTransformer;
use Drupal\commerce_amws\Utilities;
use Drupal\entity_sync\FieldTransformer\PluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Transformer that sets the unit price for order items.
*
* @EntitySyncFieldTransformer(
* id = "commerce_amws_order_item_unit_price"
* )
*/
class OrderItemUnitPrice extends PluginBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
// Using the unit price setter method will trigger recalculating the order
// item's total price as well.
'setter' => 'setUnitPrice',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function transformImportedValue(
$original_value,
\stdClass $remote_entity,
ContentEntityInterface $local_entity,
array $field_info,
array $context
) {
$total_price = Utilities::amwsPriceToDrupalPrice($original_value);
// Subtract any promotion discount as well.
// We might develop a feature of connecting Drupal promotions with Amazon
// MWS promotions, but that needs a bit more thinking. For now, don't create
// an adjustment.
if (!empty($remote_entity->PromotionDiscount)) {
$total_price = $total_price->subtract(
Utilities::amwsPriceToDrupalPrice($remote_entity->PromotionDiscount)
);
}
return $total_price->divide($remote_entity->QuantityOrdered);
}
}
