Skip to content

Price API

Currencies

To manage currencies, use CurrencyServiceInterface.

To access a currency object by its code, use CurrencyServiceInterface::getCurrencyByCode. To access a whole list of currencies, use CurrencyServiceInterface::findCurrencies.

1
2
3
4
5
6
7
8
        $currency = $this->currencyService->getCurrencyByCode($currencyCode);
        $output->writeln('Currency ID: ' . $currency->getId());

        $currencies = $this->currencyService->findCurrencies();

        foreach ($currencies as $currency) {
            $output->writeln('Currency ' . $currency->getId() . ' with code ' . $currency->getCode());
        }

To create a new currency, use CurrencyServiceInterface::createCurrency() and provide it with a CurrencyCreateStruct with code, number of fractional digits and a flag indicating if the currency is enabled:

1
2
3
        $currencyCreateStruct = new CurrencyCreateStruct($newCurrencyCode, 2, true);

        $this->currencyService->createCurrency($currencyCreateStruct);

Prices

To manage prices, use ProductPriceService.

To retrieve the price of a product in the currency for the current context, use Product::getPrice():

1
2
3
        $productPrice = $product->getPrice();

        $output->writeln("Price for ". $product->getName() . " is " . $productPrice);

To retrieve the price of a product in a specific currency, use ProductPriceService::getPriceByProductAndCurrency:

1
2
3
        $productPrice = $this->productPriceService->getPriceByProductAndCurrency($product, $currency);

        $output->writeln("Price for ". $product->getName() . " in " . $currencyCode . " is " . $productPrice);

To get all prices (in different currencies) for a given product, use ProductPriceService::findPricesByProductCode:

1
2
3
4
        $output->writeln("All prices for " . $product->getName() . ":");
        foreach ($prices as $price) {
            $output->writeln($price);
        }

You can also use ProductPriceService to create or modify existing prices. For example, to create a new price for a given currency, use ProductPriceService::createProductPrice and provide it with a ProductPriceCreateStruct object:

1
2
3
4
5
6
        $money = new Money\Money(50000, new Money\Currency($newCurrencyCode));
        $priceCreateStruct = new ProductPriceCreateStruct($product, $newCurrency, $money, null);

        $this->productPriceService->createProductPrice($priceCreateStruct);

        $output->writeln("Created new price in currency " . $newCurrencyCode);

Note

Prices operate using the Money library. That is why all amounts are provided in the smallest unit. For example, for euro 50000 refers to 50000 cents, equal to 50 euros.