### ProductVariant Implementation Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/03-product-variant-interface.md Example of a ProductVariant class implementing ProductVariantInterface, utilizing the TierPriceableTrait and initializing it in the constructor. ```php use Brille24\SyliusTierPricePlugin\Entity\ProductVariantInterface; class ProductVariant extends BaseProductVariant implements ProductVariantInterface { use \Brille24\SyliusTierPricePlugin\Traits\TierPriceableTrait; public function __construct() { parent::__construct(); $this->initTierPriceableTrait(); } } ``` -------------------------------- ### Response Example for Listing Tier Prices Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON response for the 'List All Tier Prices' endpoint, showing a list of tier price members. ```json { "hydra:member": [ { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ], "hydra:totalItems": 42, "hydra:view": { "@id": "/api/tierprice?page=1" } } ``` -------------------------------- ### Creating Tier Price Examples with TierPriceExampleFactory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Demonstrates how to create tier price entities using the TierPriceExampleFactory. Examples show creating with default settings and with specific configurations for quantity, price, product variant, and channel. ```php $factory = $container->get(TierPriceExampleFactory::class); // Create with defaults $tierPrice = $factory->create(); // Create with specific options $tierPrice = $factory->create([ 'quantity' => 5, 'price' => 8500, 'product_variant' => 'VARIANT-001', 'channel' => 'US_WEB', ]); ``` -------------------------------- ### Response Example for Creating Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON response upon successful creation of a tier price. ```json { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ``` -------------------------------- ### Example Tier Price Fixture File Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md A comprehensive example of a fixture file defining multiple tier prices with product variant, channel, quantity, and price details. ```yaml sylius_fixtures: suites: test: fixtures: tier_prices: options: custom: # Tier price: 10+ units at $95 in US - product_variant: "20125148-54ca-3f05-875f-5524f95aa85b" channel: US_WEB quantity: 10 price: 9500 # Tier price: 20+ units at $85 in US - product_variant: "20125148-54ca-3f05-875f-5524f95aa85b" channel: US_WEB quantity: 20 price: 8500 # Tier price: 10+ units at €90 in EU - product_variant: "20125148-54ca-3f05-875f-5524f95aa85b" channel: EU_WEB quantity: 10 price: 9000 ``` -------------------------------- ### Response Example for Partial Update (PATCH) Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON response after a partial update, showing the modified price. ```json { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 8200, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ``` -------------------------------- ### Tier Price Fixture Configuration Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Example YAML configuration for the tier_prices fixture, demonstrating how to define multiple tier prices for different channels and product variants. ```yaml sylius_fixtures: suites: my_suite: fixtures: tier_prices: options: custom: - channel: US_WEB product_variant: "VARIANT-001" quantity: 10 price: 9500 - channel: EU_WEB product_variant: "VARIANT-001" quantity: 20 price: 8500 ``` -------------------------------- ### Response Example for Updating Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON response upon successful update of a tier price. ```json { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 20, "price": 8500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ``` -------------------------------- ### Response Example for Single Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON response for retrieving a single tier price, including detailed information about the price, channel, and product variant. ```json { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": { "@id": "/api/channels/US_WEB", "id": 1, "code": "US_WEB", "name": "US Web Store" }, "productVariant": { "@id": "/api/product-variants/variant-001", "id": 1, "code": "variant-001" }, "customerGroup": null } ``` -------------------------------- ### Usage Example for TierPriceableInterface Methods Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/04-tier-priceable-interface.md Demonstrates how to use the various methods of the TierPriceableInterface to retrieve and set tier prices for different scenarios. ```php use Brille24\SyliusTierPricePlugin\Traits\TierPriceableInterface; // Get all tier prices $allPrices = $variant->getTierPrices(); // Get tier prices for a specific channel $channelPrices = $variant->getTierPricesForChannel($channel, $customer); // Get tier prices by channel code $prices = $variant->getTierPricesForChannelCode('US_WEB', $customer); // Set tier prices $variant->setTierPrices($newPriceArray); ``` -------------------------------- ### Example Usage of TierPriceFinderInterface Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/07-tier-price-finder-interface.md Demonstrates how to use the TierPriceFinderInterface to retrieve an applicable tier price and its associated price. ```php $finder = $container->get(TierPriceFinderInterface::class); $tierPrice = $finder->find( $productVariant, $channel, $quantity, $customer ); if ($tierPrice) { $applicablePrice = $tierPrice->getPrice(); } ``` -------------------------------- ### Update Database, Assets, and Translations Source: https://github.com/brille24/syliustierpriceplugin/blob/master/README.md Run these console commands to update your database schema, install assets, and update translations after installation. ```sh bin/console doctrine:schema:update --force bin/console assets:install bin/console translation:update --force ``` -------------------------------- ### TierPriceExampleFactory create Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Creates a tier price example entity. It accepts an array of options to customize quantity, price, product variant, and channel. Defaults are applied if options are not provided. ```php public function create(array $options = []): TierPriceInterface ``` -------------------------------- ### Tier Pricing Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/00-index.md Illustrates how tier pricing applies to a product based on quantity thresholds. This example shows a base price and progressively lower prices for larger order quantities. ```text Product: Widget Base Price: $10.00 Tier Pricing: - 1-9 units: $10.00 (base price) - 10-19 units: $9.50 - 20+ units: $8.50 ``` -------------------------------- ### PHP Example: Creating and Associating a Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/13-tier-price-factory-interface.md Demonstrates how to use the TierPriceFactory to create a new tier price and associate it with a product variant. Ensure the required options like 'quantity', 'price', and 'channel' are provided. ```php $factory = $container->get(TierPriceFactoryInterface::class); $tierPrice = $factory->createAtProductVariant( $variant, [ 'quantity' => 10, 'price' => 9500, 'channel' => $channel, ] ); // The tier price is now added to the variant assert($variant->getTierPrices()[0] === $tierPrice); ``` -------------------------------- ### Example Usage of OrderPricesRecalculator Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/11-order-prices-recalculator.md Demonstrates how to use the process method on an order object. After execution, order items will have their prices updated. ```php $processor->process($order); // After processing, all order items have prices recalculated with tier pricing ``` -------------------------------- ### Example Usage of find Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/06-tier-price-finder.md Demonstrates how to use the find method to retrieve a tier price and access its price, or handle cases where no tier pricing applies. ```php $tierPrice = $tierPriceFinder->find($variant, $channel, 25, $customer); if ($tierPrice) { echo "Price at qty 25: " . $tierPrice->getPrice(); } else { echo "No tier pricing applies"; } ``` -------------------------------- ### getSortedTierPrices Usage Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/09-tier-price-repository-interface.md Demonstrates how to use the getSortedTierPrices method to fetch general or group-specific tier prices. ```php $finder = $container->get('brille24.repository.tierprice'); // Get general tier prices (no group) $prices = $finder->getSortedTierPrices($variant, $channel); // Get prices for a specific customer group $groupPrices = $finder->getSortedTierPrices($variant, $channel, $group); ``` -------------------------------- ### Product Variant Tier Price Usage Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/03-product-variant-interface.md Demonstrates how to use the ProductVariantInterface methods to add, retrieve, and remove tier prices. ```php // Usage $variant = $productVariantRepository->find(1); // Add a tier price $tierPrice = new TierPrice(10, 9500); $tierPrice->setChannel($channel); $variant->addTierPrice($tierPrice); // Get all tier prices for a channel $channelTierPrices = $variant->getTierPricesForChannel($channel); // Get tier prices by channel code $channelTierPrices = $variant->getTierPricesForChannelCode('US_WEB'); // Remove a tier price $variant->removeTierPrice($tierPrice); ``` -------------------------------- ### TierPriceUniqueValidator Validation Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/17-tier-price-validator.md Example of how to apply the TierPriceUniqueConstraint to a TierPrice entity, specifying the fields to check for uniqueness. ```php // In a constraint on TierPrice entity #[Constraint(TierPriceUniqueConstraint::class, ['fields' => ['qty', 'channel', 'customerGroup']])] public function validate($value, Constraint $constraint): void ``` -------------------------------- ### getTierPriceForQuantity Usage Example Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/09-tier-price-repository-interface.md Illustrates how to use getTierPriceForQuantity to find a tier price for a specific quantity, with or without a customer group. ```php // Find tier price for qty=10 with no customer group $tierPrice = $finder->getTierPriceForQuantity( $variant, $channel, null, 10 ); // Find tier price for qty=5 with specific customer group $tierPrice = $finder->getTierPriceForQuantity( $variant, $channel, $customerGroup, 5 ); ``` -------------------------------- ### Create Tier Prices with Fixtures Source: https://github.com/brille24/syliustierpriceplugin/blob/master/README.md Example of how to define tier prices using Sylius fixtures. Ensure products and product variants exist beforehand. ```yaml sylius_fixtures: suites: my_suite: fixtures: tier_prices: options: custom: - product_variant: "20125148-54ca-3f05-875f-5524f95aa85b" channel: US_WEB quantity: 10 price: 5 ``` -------------------------------- ### Paginate Tier Price List Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Demonstrates how to paginate through a list of tier prices using the 'page' and 'limit' query parameters. This example shows how to request the second page with 25 items per page. ```bash GET /api/tierprice?page=2&limit=25 ``` -------------------------------- ### Request Body for Creating Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON body for creating a tier price, specifying quantity, price, channel, and product variant. ```json { "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ``` -------------------------------- ### Get Tier Prices for a Product Variant using cURL Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Retrieve all tier prices associated with a specific product variant using this GET request. Replace 'YOUR_API_TOKEN' and the product variant ID with your actual values. ```bash curl -X GET "https://your-sylius.example.com/api/tierprice?productVariant=/api/product-variants/1" \ -H "Authorization: Bearer YOUR_API_TOKEN" ``` -------------------------------- ### TierPriceExampleFactory Class Declaration Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Defines the TierPriceExampleFactory class, extending Sylius's AbstractExampleFactory. This class is responsible for creating example tier price entities. ```php namespace Brille24\SyliusTierPricePlugin\Factory; use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; class TierPriceExampleFactory extends AbstractExampleFactory ``` -------------------------------- ### Load Fixture Data for Tier Prices Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/00-index.md Provides an example of how to load tier price data using Sylius fixtures. This is helpful for setting up development or testing environments with predefined tier prices. ```yaml sylius_fixtures: suites: default: fixtures: tier_prices: options: custom: - product_variant: "VARIANT-001" channel: US_WEB quantity: 10 price: 9500 ``` -------------------------------- ### Prepare Factory Options with Correct Data Types Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/errors.md Ensure that options passed to the factory have the correct data types. This example shows converting quantity and price to integers and fetching a channel entity before the factory call, preventing 'Expected an integer' errors. ```php $options = [ 'quantity' => (int)$request->get('quantity'), 'price' => (int)$request->get('price'), 'channel' => $channelRepository->find($channelId), ]; ``` -------------------------------- ### PHP Example: Accessing TierPriceFactory via Service Container Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/13-tier-price-factory-interface.md Shows how to retrieve the TierPriceFactoryInterface instance from the Symfony service container. This is the standard way to access the factory in your application. ```php $factory = $container->get(TierPriceFactoryInterface::class); ``` -------------------------------- ### Service Configuration for TierPriceFactory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/12-tier-price-factory.md Configures the TierPriceFactory as a service, decorating the base tier price factory. This setup is typically done in the application's service configuration file. ```php $services->set(TierPriceFactoryInterface::class, TierPriceFactory::class) ->decorate('brille24.factory.tierprice') ->args([service('.inner')]) ; ``` -------------------------------- ### Request Body for Partial Update (PATCH) Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON body for a partial update, modifying only the 'price' field. ```json { "price": 8200 } ``` -------------------------------- ### calculateOriginal Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/10-product-variant-price-calculator.md Delegates to the base calculator to get the original (non-discounted) price for a product variant. ```APIDOC ## calculateOriginal ### Description Delegates to the base calculator to get the original (non-discounted) price for a product variant. ### Method POST ### Endpoint /calculateOriginal ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **productVariant** (ProductVariantInterface) - Required - Variant to price. - **context** (array) - Required - Calculation context. ### Request Example ```json { "productVariant": "", "context": {} } ``` ### Response #### Success Response (200) - **price** (int) - Original unit price in cents. #### Response Example ```json { "price": 1200 } ``` ``` -------------------------------- ### Get All Tier Prices for a Product Variant Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/05-tier-priceable-trait.md Returns all tier prices associated with a product variant. Useful for iterating through all available price tiers. ```php public function getTierPrices(): array ``` ```php $allPrices = $variant->getTierPrices(); foreach ($allPrices as $tierPrice) { echo "Qty: {$tierPrice->getQty()}, Price: {$tierPrice->getPrice()}"; } ``` -------------------------------- ### Fixture Integration for Tier Prices Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Example of how TierPriceExampleFactory is used within Sylius fixtures to load tier prices. This YAML configuration specifies custom tier prices with product variant, channel, quantity, and price details. ```yaml sylius_fixtures: suites: my_suite: fixtures: tier_prices: options: custom: - product_variant: "VARIANT-001" channel: US_WEB quantity: 10 price: 5000 ``` -------------------------------- ### Request Body for Updating Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Example JSON body for updating a tier price, including updated quantity and price. ```json { "qty": 20, "price": 8500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ``` -------------------------------- ### TierPriceFixture Service Configuration Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Service configuration for TierPriceFixture, registering it as a Sylius fixture and injecting dependencies like the entity manager and example factory. ```php $services->set(TierPriceFixture::class) ->args([ service('doctrine.orm.default_entity_manager'), service(TierPriceExampleFactory::class), ]) ->tag('sylius_fixtures.fixture') ; ``` -------------------------------- ### getPrice Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/02-tierprice-interface.md Retrieves the unit price in cents for this pricing tier. Use this method to get the price value. ```php public function getPrice(): int ``` -------------------------------- ### Accessing TierPriceRepository via Container or EntityManager Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/08-tier-price-repository.md Demonstrates how to retrieve the TierPriceRepository instance from the service container or using the EntityManager. This is the standard way to get repository instances in Sylius applications. ```php $repository = $container->get('brille24.repository.tierprice'); // or $repository = $entityManager->getRepository(TierPrice::class); ``` -------------------------------- ### TierPriceExampleFactory Constructor Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Initializes the TierPriceExampleFactory with necessary repositories and factories. It requires ProductVariantRepositoryInterface, ChannelRepositoryInterface, and TierPriceFactoryInterface. ```php public function __construct( private ProductVariantRepositoryInterface $productVariantRepository, private ChannelRepositoryInterface $channelRepository, private TierPriceFactoryInterface $tierPriceFactory, ) ``` -------------------------------- ### Recommended Fixture Order Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Illustrates the recommended order for loading fixtures, ensuring that products and channels are created before tier prices. ```yaml suites: test: fixtures: products: {} channels: {} tier_prices: {} ``` -------------------------------- ### Example SQLSTATE Error for Unique Constraint Violation Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/errors.md This is an example of the SQLSTATE error message encountered when a unique constraint violation occurs. It is typically caught by form validation before reaching the database. ```text SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ... ``` -------------------------------- ### TierPriceInterface Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/types.md Defines the methods for getting and setting properties of a TierPrice entity. ```APIDOC ## TierPriceInterface ### Description Defines the methods for getting and setting properties of a TierPrice entity. ### Methods - `getPrice(): int` - `setPrice(int $price): void` - `getQty(): int` - `setQty(int $qty): void` - `getProductVariant(): ProductVariantInterface` - `setProductVariant(ProductVariantInterface $productVariant): void` - `getChannel(): ?ChannelInterface` - `setChannel(?ChannelInterface $channel): void` - `getCustomerGroup(): ?CustomerGroupInterface` - `setCustomerGroup(?CustomerGroupInterface $customerGroup): void` ### Extends `Sylius\Component\Resource\Model\ResourceInterface` ``` -------------------------------- ### Get Single Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Retrieves a specific tier price by its ID. ```APIDOC ## GET /api/tierprice/{id} ### Description Retrieves a specific tier price by its ID. ### Method GET ### Endpoint /api/tierprice/{id} ### Parameters #### Path Parameters - **id** (int) - Required - Tier price ID ### Response #### Success Response (200 OK) - **@id** (string) - Resource identifier - **@type** (string) - Resource type - **id** (int) - Tier price ID - **qty** (int) - Minimum quantity - **price** (int) - Unit price in cents - **channel** (object) - Channel details - **@id** (string) - Resource identifier - **id** (int) - Channel ID - **code** (string) - Channel code - **name** (string) - Channel name - **productVariant** (object) - Product variant details - **@id** (string) - Resource identifier - **id** (int) - Product variant ID - **code** (string) - Product variant code - **customerGroup** (string|null) - Customer group reference or null ### Response Example { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": { "@id": "/api/channels/US_WEB", "id": 1, "code": "US_WEB", "name": "US Web Store" }, "productVariant": { "@id": "/api/product-variants/variant-001", "id": 1, "code": "variant-001" }, "customerGroup": null } ### Errors - **404 Not Found** — Tier price does not exist ``` -------------------------------- ### Load All Fixtures Command Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Command to load all fixtures for a specific suite, e.g., 'test'. ```bash bin/console sylius:fixtures:load test ``` -------------------------------- ### Get Single Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Retrieves a specific tier price by its ID. ```http GET /api/tierprice/{id} ``` -------------------------------- ### Service Configuration for TierPriceExampleFactory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/14-tier-price-example-factory.md Shows the service configuration for registering TierPriceExampleFactory in the dependency injection container. This makes it available for use in other parts of the application. ```php $services->set(TierPriceExampleFactory::class); ``` -------------------------------- ### GET /api/tierprice Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Retrieves tier prices, supporting filtering by product variant and sorting by quantity. ```APIDOC ## GET /api/tierprice ### Description Retrieves a list of tier prices. This endpoint supports filtering by a specific product variant and allows for sorting the results based on the quantity. ### Method GET ### Endpoint /api/tierprice ### Parameters #### Query Parameters - **productVariant** (string) - Required - The product variant to filter tier prices by. Expected format is a URL like `/api/product-variants/{id}`. - **sorting[qty]** (string) - Optional - Specifies the sorting order for the quantity. Use `asc` for ascending or `desc` for descending. ### Request Example ```json { "example": "GET /api/tierprice?productVariant=/api/product-variants/1&sorting[qty]=asc" } ``` ### Response #### Success Response (200) - **tierPrices** (array) - A list of tier price objects. The exact structure of each object depends on Sylius configuration and resource definition. #### Response Example ```json { "example": "[ ... tier price objects ... ]" } ``` ``` -------------------------------- ### Add Tier Price Programmatically Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/00-index.md Demonstrates how to programmatically create and associate a new tier price with a product variant. Ensure to flush the entity manager to persist the changes. ```php $tierPrice = new TierPrice(qty: 10, price: 9500); $tierPrice->setChannel($channel); $tierPrice->setProductVariant($variant); $variant->addTierPrice($tierPrice); $entityManager->flush(); ``` -------------------------------- ### ProductVariantPriceCalculator Constructor Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/10-product-variant-price-calculator.md Initializes the ProductVariantPriceCalculator with its dependencies: base price calculator, tier price finder, and customer context. ```php public function __construct( private ProductVariantPricesCalculatorInterface $basePriceCalculator, private TierPriceFinderInterface $tierPriceFinder, private CustomerContextInterface $customerContext, ) ``` -------------------------------- ### Get Tier Prices Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Retrieves a list of tier prices, optionally filtered by product variant. Supports Hydra pagination. ```APIDOC ## GET /api/tierprice ### Description Retrieves a list of tier prices, optionally filtered by product variant. Supports Hydra pagination. ### Method GET ### Endpoint /api/tierprice ### Parameters #### Query Parameters - **productVariant** (string) - Optional - Filter by product variant API URI. - **page** (integer) - Optional - The page number for pagination. - **limit** (integer) - Optional - The number of items per page. ### Response #### Success Response (200 OK) - **hydra:view** (object) - Pagination information. - **hydra:totalItems** (integer) - The total number of tier prices available. - **[Array of Tier Price Objects]** - Each object contains tier price details. #### Response Example (with pagination) ```json { "@context": "@vocab", "@id": "/api/tierprice?page=2&limit=25", "@type": "hydra:Collection", "hydra:member": [ { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ], "hydra:totalItems": 100, "hydra:view": { "@id": "/api/tierprice?page=2&limit=25", "@type": "hydra:PartialCollectionView", "hydra:first": "/api/tierprice?page=1&limit=25", "hydra:last": "/api/tierprice?page=4&limit=25", "hydra:next": "/api/tierprice?page=3&limit=25", "hydra:previous": "/api/tierprice?page=1&limit=25" } } ``` ``` -------------------------------- ### Get Tier Price ID Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/01-tierprice-entity.md Retrieves the primary key of the tier price record. Returns null if the record has not been persisted. ```php public function getId(): ?int ``` -------------------------------- ### Create and Persist a TierPrice Entity Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/01-tierprice-entity.md Demonstrates how to instantiate the TierPrice entity, set its associated channel and product variant, and persist it to the database using the EntityManager. This is useful for programmatically adding new tier pricing rules. ```php use Brille24\SyliusTierPricePlugin\Entity\TierPrice; $tierPrice = new TierPrice(qty: 10, price: 9500); $tierPrice->setChannel($channel); $tierPrice->setProductVariant($productVariant); $tierPrice->setCustomerGroup(null); // Available to all customers // Persist using EntityManager $entityManager->persist($tierPrice); $entityManager->flush(); ``` -------------------------------- ### Configure TierPriceType Form Options Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/configuration.md Example of how to add the 'tierPrices' field using LiveCollectionType in a Symfony form. The 'entry_options' require a 'currency' to be specified. ```php $form->add('tierPrices', LiveCollectionType::class, [ 'entry_type' => TierPriceType::class, 'entry_options' => [ 'currency' => 'USD', // Required option ], 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'block_name' => 'entry', ]); ``` -------------------------------- ### Error Handling in Services using Try-Catch Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/errors.md Demonstrates how to use try-catch blocks in services to handle specific exceptions like InvalidArgumentException and EntityNotFoundException during object creation. Errors are logged for debugging. ```php use Symfony\Component\Validator\Exception\ConstraintDefinitionException; try { $tierPrice = $factory->createAtProductVariant($variant, $options); } catch (InvalidArgumentException $e) { // Invalid option type logger->error("Invalid factory options: " . $e->getMessage()); } catch (EntityNotFoundException $e) { // Related entity not found logger->error("Entity not found: " . $e->getMessage()); } ``` -------------------------------- ### Import Plugin Configuration Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/configuration.md Import the plugin's configuration file into your main application configuration. ```yaml imports: - { resource: '@Brille24SyliusTierPricePlugin/config/config.yaml' } ``` -------------------------------- ### Create Tier Price via Factory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/00-index.md Illustrates using a factory to create a tier price associated with a product variant. This method simplifies the creation process by accepting an array of attributes. ```php $tierPrice = $tierPriceFactory->createAtProductVariant( $variant, ['quantity' => 10, 'price' => 9500, 'channel' => $channel] ); ``` -------------------------------- ### Constructor for TierPriceFactory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/12-tier-price-factory.md Initializes the TierPriceFactory with a base factory for creating tier price entities. ```php public function __construct( private FactoryInterface $factory ) ``` -------------------------------- ### setQty Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/02-tierprice-interface.md Sets the minimum quantity threshold for this pricing tier. Negative values are normalized to zero. ```php public function setQty(int $qty): void ``` -------------------------------- ### Get Tier Prices Filtered by Channel and Customer Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/05-tier-priceable-trait.md Retrieves tier prices filtered by a specific channel and optionally by customer group. It prioritizes group-specific prices if available. ```php public function getTierPricesForChannel(ChannelInterface $channel, ?CustomerInterface $customer = null): array ``` ```php $channelPrices = $variant->getTierPricesForChannel($channel, $customer); ``` -------------------------------- ### Load Specific Fixture Command Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Command to load only the 'tier_prices' fixture for a specific suite, e.g., 'test'. ```bash bin/console sylius:fixtures:load test --fixtures tier_prices ``` -------------------------------- ### TierPriceFinder Constructor Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/06-tier-price-finder.md Initializes the TierPriceFinder with its required dependency, the TierPriceRepositoryInterface. ```php public function __construct( private TierPriceRepositoryInterface $tierPriceRepository ) ``` -------------------------------- ### Usage Pattern in ProductVariantPriceCalculator Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/06-tier-price-finder.md Illustrates how the TierPriceFinder service is integrated into the ProductVariantPriceCalculator to determine applicable tier prices. ```php $tierPrice = $this->tierPriceFinder->find( $productVariant, $context['channel'], $context['quantity'], $customer ); if ($tierPrice !== null) { return $tierPrice->getPrice(); } else { return $this->basePriceCalculator->calculate($productVariant, $context); } ``` -------------------------------- ### Handling InvalidArgumentException in TierPriceFactory Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/errors.md This example demonstrates how to handle InvalidArgumentException when creating tier prices with incorrect data types for quantity, channel, or price. Ensure all parameters match the expected types. ```php // Invalid quantity type $factory->createAtProductVariant($variant, [ 'quantity' => 'ten', // String instead of int 'price' => 9500, 'channel' => $channel, ]); // Throws InvalidArgumentException // Invalid price type $factory->createAtProductVariant($variant, [ 'quantity' => 10, 'price' => '95.00', // String instead of int 'channel' => $channel, ]); // Throws InvalidArgumentException ``` -------------------------------- ### Template Rendering of Tier Prices Collection Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/16-product-variant-type-extension.md Example of how to render the dynamically added tier prices collection in a Twig template. The LiveCollectionType handles the rendering of the collection's elements and buttons. ```twig
{{ form_widget(form.tierPrices) }}
``` -------------------------------- ### Service Configuration for TierPriceFinder Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/06-tier-price-finder.md Shows the service configuration in config/services.php, registering the TierPriceFinder::class under the TierPriceFinderInterface::class. ```php $services->set(TierPriceFinderInterface::class, TierPriceFinder::class); ``` -------------------------------- ### Create Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Creates a new tier price entry. ```APIDOC ## POST /api/tierprice ### Description Creates a new tier price entry. ### Method POST ### Endpoint /api/tierprice ### Parameters #### Request Body - **qty** (int) - Required - Minimum quantity (0 or positive) - **price** (int) - Required - Unit price in cents - **channel** (string (IRI)) - Required - Channel reference (e.g., `/api/channels/1`) - **productVariant** (string (IRI)) - Required - Product variant reference (e.g., `/api/product-variants/1`) - **customerGroup** (string (IRI)|null) - Optional - Customer group reference or null ### Request Example { "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ### Response #### Success Response (201 Created) - **@id** (string) - Resource identifier - **@type** (string) - Resource type - **id** (int) - Tier price ID - **qty** (int) - Minimum quantity - **price** (int) - Unit price in cents - **channel** (string) - Channel reference (IRI) - **productVariant** (string) - Product variant reference (IRI) - **customerGroup** (string|null) - Customer group reference or null ### Response Example { "@id": "/api/tierprice/1", "@type": "TierPrice", "id": 1, "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null } ### Errors - **400 Bad Request** — Invalid data - **422 Unprocessable Entity** — Validation failed (e.g., duplicate combination) ``` -------------------------------- ### Get Tier Prices Filtered by Channel Code Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/05-tier-priceable-trait.md Retrieves tier prices filtered by a channel's code, providing an alternative to using a ChannelInterface object. Also supports customer group filtering. ```php public function getTierPricesForChannelCode(string $code, ?CustomerInterface $customer = null): array ``` ```php $prices = $variant->getTierPricesForChannelCode('US_WEB', $customer); ``` -------------------------------- ### getQty Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/02-tierprice-interface.md Retrieves the minimum quantity threshold for this pricing tier. This indicates the minimum number of items required to qualify for this tier's price. ```php public function getQty(): int ``` -------------------------------- ### OrderPricesRecalculator Constructor Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/11-order-prices-recalculator.md Initializes the OrderPricesRecalculator with its dependencies: a tier-price-aware product variant calculator and the original Sylius order processor. ```php public function __construct( private ProductVariantPricesCalculatorInterface $productVariantPriceCalculator, private OrderProcessorInterface $orderProcessor, ) ``` -------------------------------- ### Configuring Plugin Routes Source: https://github.com/brille24/syliustierpriceplugin/blob/master/UPGRADE-2.md Adjust your routing configuration to reference the plugin's routes from the new directory structure. ```yaml # config/routing.yaml brille24_tierprice_bundle: resource: '@Brille24SyliusTierPricePlugin/config/routing.yml' ``` -------------------------------- ### Get Specific Tier Price for Quantity Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/08-tier-price-repository.md Retrieves a single tier price that exactly matches the product variant, channel, customer group (or null), and quantity. Use this when you need to find a precise tier price for a specific order quantity. ```php public function getTierPriceForQuantity( TierPriceableInterface $productVariant, ChannelInterface $channel, ?CustomerGroupInterface $customerGroup, int $quantity, ): ?TierPriceInterface ``` ```php // Get the tier price for exactly 10 units $tierPrice = $repository->getTierPriceForQuantity( $variant, $channel, null, 10 ); ``` -------------------------------- ### OrderPricesRecalculator Process Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/11-order-prices-recalculator.md Recalculates unit prices for all order items, applying tier pricing based on quantity. Skips immutable items. ```php public function process(BaseOrderInterface $order): void ``` -------------------------------- ### Create a Tier Price using cURL Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Use this cURL command to create a new tier price. Ensure you replace placeholders with your actual API token, channel, and product variant IDs. The 'customerGroup' can be set to null if not applicable. ```bash curl -X POST https://your-sylius.example.com/api/tierprice \ -H "Content-Type: application/ld+json" \ -H "Authorization: Bearer YOUR_API_TOKEN" \ -d '{ "qty": 10, "price": 9500, "channel": "/api/channels/1", "productVariant": "/api/product-variants/1", "customerGroup": null }' ``` -------------------------------- ### Configuring TierPriceType with Currency Option Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/15-tier-price-form-type.md Demonstrates how to set the 'currency' option when adding TierPriceType to a form builder or using the form factory. ```php $builder->add('tierPrice', TierPriceType::class, [ 'currency' => 'EUR', ]); ``` ```php $form = $formFactory->create(TierPriceType::class, $tierPrice, [ 'currency' => 'USD', ]); ``` -------------------------------- ### Get Sorted Tier Prices for Product Variant Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/08-tier-price-repository.md Retrieves all tier prices for a given product variant and channel, with optional filtering by customer group. Results are sorted by quantity in ascending order. Use this to fetch all applicable pricing tiers for a product. ```php public function getSortedTierPrices( TierPriceableInterface $productVariant, ChannelInterface $channel, ?CustomerGroupInterface $customerGroup = null, ): array ``` ```php // Get tier prices for all customers (no group restriction) $prices = $repository->getSortedTierPrices($variant, $channel); // Get tier prices for a specific customer group $prices = $repository->getSortedTierPrices($variant, $channel, $customerGroup); ``` -------------------------------- ### Implement Tier Priceable Functionality Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/types.md This trait provides methods for managing and retrieving tier prices associated with a product variant. It includes functions to initialize, get all, retrieve specific tier prices for a channel and customer, remove, add, and set tier prices. Use this trait in classes that need to manage tier pricing, such as ProductVariant. ```php trait TierPriceableTrait { protected $tierPrices; public function initTierPriceableTrait(): void; public function getTierPrices(): array; public function getTierPricesForChannel(ChannelInterface $channel, ?CustomerInterface $customer = null): array; public function getTierPricesForChannelCode(string $code, ?CustomerInterface $customer = null): array; public function removeTierPrice(TierPriceInterface $tierPrice): void; public function addTierPrice(TierPriceInterface $tierPrice): void; public function setTierPrices(array $tierPrices): void; } ``` -------------------------------- ### Constructor for TierPrice Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/01-tierprice-entity.md Initializes a new TierPrice entity with optional quantity and price. The quantity is normalized to a non-negative value. ```php public function __construct( int $qty = 0, int $price = 0, ) ``` -------------------------------- ### Configure Sylius Tier Price Plugin Source: https://github.com/brille24/syliustierpriceplugin/blob/master/README.md Import the plugin's configuration into your local config/config.yaml file. ```yaml imports: ... - { resource: '@Brille24SyliusTierPricePlugin/config/config.yaml'} ``` -------------------------------- ### createNew Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/13-tier-price-factory-interface.md Creates a new, empty TierPrice entity. ```APIDOC ## createNew ### Description Creates a new, empty `TierPrice` entity. ### Method ```php public function createNew(): object ``` ### Returns `TierPriceInterface` — New entity ``` -------------------------------- ### Create TierPrice at Product Variant Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/12-tier-price-factory.md Creates and configures a new tier price attached to a specific product variant. It requires quantity, price, and optionally a channel, performing validation on these options. ```php $tierPrice = $factory->createAtProductVariant( $variant, [ 'quantity' => 10, 'price' => 9500, 'channel' => $channel, ] ); ``` -------------------------------- ### TierPrice Constructor Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/01-tierprice-entity.md Initializes a new TierPrice entity with a specified quantity and price. Default values are zero for both. ```APIDOC ## Constructor ### Description Initializes a new TierPrice entity. ### Parameters #### Path Parameters - **qty** (int) - Optional - Minimum quantity for this tier (will be normalized to non-negative value). Defaults to 0. - **price** (int) - Optional - Unit price in cents. Defaults to 0. ``` -------------------------------- ### TierPriceFactoryInterface Create Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/types.md Defines the interface for creating TierPriceInterface objects, specifically a method to create a tier price associated with a product variant. ```php interface TierPriceFactoryInterface extends FactoryInterface { public function createAtProductVariant( ProductVariantInterface $productVariant, array $options = [], ): TierPriceInterface; } ``` -------------------------------- ### TierPriceInterface Methods Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/02-tierprice-interface.md This section details the methods available on the TierPriceInterface for interacting with tier pricing entities. ```APIDOC ## TierPriceInterface ### Description Defines the contract for tier price entities, extending Sylius's ResourceInterface. ### Methods #### getPrice Retrieves the unit price in cents for this tier. - **Returns:** `int` — Price in cents #### setPrice Sets the unit price in cents. - **Parameters** - `price` (int) - Required - Price in cents #### getQty Retrieves the minimum quantity threshold for this pricing tier. - **Returns:** `int` — Quantity threshold #### setQty Sets the minimum quantity threshold. This method does not allow negative values; they are normalized to zero. - **Parameters** - `qty` (int) - Required - Quantity threshold (negative values normalized to 0) #### getProductVariant Returns the product variant this tier price is associated with. - **Returns:** `ProductVariantInterface` — The owning product variant #### setProductVariant Associates this tier price with a product variant. - **Parameters** - `productVariant` (ProductVariantInterface) - Required - The product variant #### getChannel Returns the channel scope for this tier price. - **Returns:** `ChannelInterface|null` — Channel or null #### setChannel Sets the channel scope. - **Parameters** - `channel` (ChannelInterface|null) - Required - Channel or null #### getCustomerGroup Returns the customer group restriction for this tier price. - **Returns:** `CustomerGroupInterface|null` — Customer group or null #### setCustomerGroup Sets the customer group restriction. - **Parameters** - `customerGroup` (CustomerGroupInterface|null) - Required - Customer group or null ``` -------------------------------- ### Create Tier Price Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/endpoints.md Creates a new tier price. Requires 'application/ld+json' or 'application/json' content type. ```http POST /api/tierprice ``` -------------------------------- ### Service Registration Alias Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/09-tier-price-repository-interface.md Shows how the TierPriceRepositoryInterface is aliased to its concrete implementation in the service container. ```php $services->alias(TierPriceRepositoryInterface::class, 'brille24.repository.tierprice'); ``` -------------------------------- ### Add Tier Price Routes Source: https://github.com/brille24/syliustierpriceplugin/blob/master/README.md Add the bundle's routes.yml to your local app/config/routes.yml for API functionality. ```yaml ... brille24_tierprice_bundle: resource: '@Brille24SyliusTierPricePlugin/config/routes.yml' ``` -------------------------------- ### TierPriceFixture configureResourceNode Method Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/18-tier-price-fixture.md Configures the schema for tier price fixture data, specifying required fields like channel, product_variant, quantity, and price. ```php protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void ``` -------------------------------- ### Calculate Lowest Price Before Discount Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/10-product-variant-price-calculator.md Calculates the lowest unit price available for a product variant before any discounts are applied. It delegates to the base calculator if it supports this method. ```php public function calculateLowestPriceBeforeDiscount( ProductVariantInterface $productVariant, array $context, ): ?int ``` -------------------------------- ### Service Configuration for OrderPricesRecalculator Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/11-order-prices-recalculator.md Shows how to register OrderPricesRecalculator as a decorator for Sylius's default order prices recalculator in the service container. ```php $services->set(OrderPricesRecalculator::class) ->decorate('sylius.order_processing.order_prices_recalculator') ->arg('$orderProcessor', service('.inner')) ; ``` -------------------------------- ### TierPriceRepositoryInterface Methods Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/types.md Defines methods for retrieving tier prices from a repository, including fetching sorted tier prices and a specific tier price for a given quantity and customer group. ```php interface TierPriceRepositoryInterface extends RepositoryInterface { public function getSortedTierPrices( TierPriceableInterface $productVariant, ChannelInterface $channel, ?CustomerGroupInterface $customerGroup = null, ): array; public function getTierPriceForQuantity( TierPriceableInterface $productVariant, ChannelInterface $channel, ?CustomerGroupInterface $customerGroup, int $quantity, ): ?TierPriceInterface; } ``` -------------------------------- ### Calculation Context for Price Calculator Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/11-order-prices-recalculator.md Illustrates the context object passed to the price calculator, including channel, quantity, and customer, to determine the correct tier price. ```php $context = [ 'channel' => $order->getChannel(), 'quantity' => $item->getQuantity(), 'customer' => $order->getCustomer(), ]; ``` -------------------------------- ### Class Declaration for TierPriceRepository Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/08-tier-price-repository.md Defines the TierPriceRepository class, extending Sylius's EntityRepository and implementing the TierPriceRepositoryInterface. This sets up the repository for database operations related to tier prices. ```php namespace Brille24\SyliusTierPricePlugin\Repository; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; class TierPriceRepository extends EntityRepository implements TierPriceRepositoryInterface ``` -------------------------------- ### Register Product Variant Price Calculator Service Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/10-product-variant-price-calculator.md Register the ProductVariantPriceCalculator as a decorator for the default Sylius product variant price calculator in `config/services.php`. The `.inner` reference injects the original calculator. ```php $services->set(ProductVariantPricesCalculatorInterface::class, ProductVariantPriceCalculator::class) ->decorate('sylius.calculator.product_variant_price') ->args([ service('.inner'), ]) ; ``` -------------------------------- ### setTierPrices Method Signature Source: https://github.com/brille24/syliustierpriceplugin/blob/master/_autodocs/03-product-variant-interface.md Signature for the method that sets all tier prices for a product variant, replacing any existing ones. ```php public function setTierPrices(array $tierPrices): void ```