
There is one crucial method to implement: resolve().Check out the sidebar for our AMA schedule, or view our past AMA's. Our service class needs to implement \Drupal\commerce_price\Resolver\PriceResolverInterface. The Priority is used to determine which order resolvers get called - the first one to return a price sets the price for that product, and any other resolvers get skipped. The crucial part here is the tag - by registering the service as a "commerce_price.price_resolver", this service will get called any time the price of a product is evaluated. We ended up with: public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) There are a lot of examples of creating custom fields around the web, but very few were clear on what types were available for propertyDefinitions and schema - it took a bit of digging and experimenting to find types that worked. (If we wanted to publish the module on, we might want to create settings for whether the threshold for a price is the bottom value or the top value of threshold, perhaps - but for the time being we're keeping this as simple as possible). It actually creates more than you need - you can strip out several of the methods, because we don't need a field settings form for this custom field type. Drupal Console creates this file for you, you just need to fill in the necessary methods. The field type plugin is the first thing to get in place. New module directory structure, with field plugin skeleton We need a module to put our custom Commerce functionality, so let's create one:

Create a custom Quantity Pricebreak field typeįirst up, the field type.
#Drupal rules hardcode selector code#
Drupal Console can quickly generate modules, plugins, and more, while PHPStorm is an incredibly smart IDE (development environment) that helps you write the code correctly the first time, with helpful type-aheads that show function header parameters, automatically add "use" statements as you go, and more. There's a couple tools that make this process go very quickly: Drupal Console, and PHPStorm.

We could, at worst, follow the same pattern - query a table using the product SKU. The previous solution used a custom SQL lookup table, which they kept up-to-date from their back office Sage accounting system through an hourly CSV import. This past week was time for the implementation, and in addition to creating the new Price Resolver, we had to decide where to store the pricing data. When validating our plan for Drupal Commerce 2, we stumbled upon some examples of a custom price resolver, and knew this would work perfectly for the need. With thousands of products, and different pricing rules for each one, they need the price for each item in the cart adjusted to the appropriate price for the quantity purchased. We're in the midst of a Commerce 2 build-out for a client, and a key requirement was to preserve their quantity pricing rules.
