Important

You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Price Storage

The Pricing bundle enables you to separate price handling from the storage. Out of the box, the bundle is distributed with a Combined Price Lists ORM based storage. Depending on the chosen storage model, you may implement your own logic for storing and fetching pricing. To set up your own price storage, ProductPriceStorageInterface must be implemented.

ProductPriceStorageInterface

ProductPriceStorageInterface consists of 2 methods:

  • getPrices - returns an array of ProductPriceInterface[] by the requested criteria (ProductPriceScopeCriteriaInterface), products, product unit codes, and currencies. Use ProductPriceDTO as the implementation of ProductPriceInterface.

  • getSupportedCurrencies - returns a list of currencies supported by the storage. Currencies should be in ISO 4217 format.

Simple CSV Storage example:

 1 namespace Acme\Bundle\PricingBundle\Storage;
 2
 3 use Oro\Bundle\CurrencyBundle\Entity\Price;
 4 use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
 5 use Oro\Bundle\PricingBundle\Model\DTO\ProductPriceDTO;
 6 use Oro\Bundle\PricingBundle\Model\ProductPriceScopeCriteriaInterface;
 7 use Oro\Bundle\PricingBundle\Storage\ProductPriceStorageInterface;
 8 use Oro\Bundle\ProductBundle\Entity\Product;
 9 use Oro\Bundle\ProductBundle\Entity\ProductUnit;
10
11 /**
12  * CSV based prices storage.
13  */
14 class CSVFilePriceStorage implements ProductPriceStorageInterface
15 {
16     /**
17      * @var DoctrineHelper
18      */
19     private $doctrineHelper;
20
21     /**
22      * @param DoctrineHelper $doctrineHelper
23      */
24     public function __construct(DoctrineHelper $doctrineHelper)
25     {
26         $this->doctrineHelper = $doctrineHelper;
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     public function getPrices(
33         ProductPriceScopeCriteriaInterface $scopeCriteria,
34         array $products,
35         array $productUnitCodes = null,
36         array $currencies = null
37     ) {
38         $productsBySku = [];
39         foreach ($products as $product) {
40             $productsBySku[$product->getSku()] = $product;
41         }
42
43         $pl = fopen(__DIR__ . '/price_lists/prices.csv', 'r');
44         $prices = [];
45         $headers = fgetcsv($pl, 1000, ',');
46
47         while (($data = fgetcsv($pl, 1000, ',')) !== false) {
48             $data = array_combine($headers, array_values($data));
49             if (array_key_exists($data['sku'], $productsBySku)) {
50                 $prices[] = $this->createPriceDTO($data, $productsBySku[$data['sku']]);
51             }
52         }
53
54         return $prices;
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function getSupportedCurrencies(ProductPriceScopeCriteriaInterface $scopeCriteria)
61     {
62         return ['USD'];
63     }
64
65     /**
66      * @param array $data
67      * @param Product $product
68      * @return ProductPriceDTO
69      */
70     private function createPriceDTO(array $data, Product $product): ProductPriceDTO
71     {
72         /** @var ProductUnit $productUnit */
73         $productUnit = $this->doctrineHelper->getEntityReference(ProductUnit::class, $data['unit']);
74
75         return new ProductPriceDTO(
76             $product,
77             Price::create($data['price'], $data['currency']),
78             $data['quantity'],
79             $productUnit
80         );
81     }
82 }

ProductPriceScopeCriteriaInterface

ProductPriceScopeCriteriaInterface contains all information that may be needed for price fetching:

  • Customer

  • Website

  • Context - an entity in which context prices are requested. For example Order, Shopping List, etc.

Replacing Default Storage

To replace default storage implementation decorate oro_pricing.storage.prices service. Read more in the Symfony article on How to decorate services.

Service definition example:

1 acme_pricing.storage.csv_file:
2     class: Acme\Bundle\PricingBundle\Storage\CSVFilePriceStorage
3     public: false
4     decorates: oro_pricing.storage.prices
5     arguments:
6         - '@oro_entity.doctrine_helper'

Disable Oro Pricing

Oro pricing is controlled by the oro_pricing feature. It may be disabled by switching off the appropriate system config option or by voting VoterInterface::FEATURE_DISABLED for the oro_pricing feature.

For more information, see Feature Toggle Bundle.

Voter example:

 1 <?php
 2
 3 namespace Acme\Bundle\PricingBundle\Feature;
 4
 5 use Oro\Bundle\FeatureToggleBundle\Checker\Voter\VoterInterface;
 6
 7 /**
 8  * Disable oro_pricing
 9  */
10 class PricingVoter implements VoterInterface
11 {
12     const PRICING_FEATURE_NAME = 'oro_pricing';
13
14     /**
15      * {@inheritdoc}
16      */
17     public function vote($feature, $scopeIdentifier = null)
18     {
19         if ($feature === self::PRICING_FEATURE_NAME) {
20             return VoterInterface::FEATURE_DISABLED;
21         }
22
23         return VoterInterface::FEATURE_ABSTAIN;
24     }
25 }