What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hi! Quick question: I want to calculate a discount dynamically according to the products that are in

UQ958DA4U
UQ958DA4U Posts: 232 🧑🏻‍🚀 - Cadet

Hi!
Quick question: I want to calculate a discount dynamically according to the products that are in the cart. The rule system is on an external API.
I've currently extended the Discount Module to use the API to fetch for the discount rate. However, now I don't know how to read the Cart from the provided DiscountTransfer or DiscountableItem[] .

What would be the most "Sprykery way" of achieving this? I'm currently thinking about adding the cartId (the literal string) and cartHash (also the string you'd get in the If-Matches header) to the DiscountTransfer in order to receive the cart from withing my calculation method. Is this a good approach?

Comments

  • Andriy Netseplyayev
    Andriy Netseplyayev Domain Lead Solution Architecture Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    Hi Thomas, for that case, The “most spryker way” would be to ignore the standard Spryker discounts and introduce your Calculator (that will fetch the discounts from external system and apply the discount) to the Calculation Stack

  • Andriy Netseplyayev
    Andriy Netseplyayev Domain Lead Solution Architecture Sprykee Posts: 519 🧑🏻‍🚀 - Cadet
  • Andriy Netseplyayev
    Andriy Netseplyayev Domain Lead Solution Architecture Sprykee Posts: 519 🧑🏻‍🚀 - Cadet
    edited November 2019

    the calculation stack is defined in Zed, CalculationDependencyProvider::getQuoteCalculatorPluginStack()

  • UQ958DA4U
    UQ958DA4U Posts: 232 🧑🏻‍🚀 - Cadet

    Hmm. That would mean though that I would skip the "normal" Discount calculation and interface. By adding my own QuoteCalculatorPlugin, I would effectively hook into calculating the end price of the cart, right?

    I don't think this is really what I want, I might have left out a crucial bit: We want to be able to add voucher codes from the web interface that are valid for certain products. Then we want to fetch the real discount rate by calling another API that does the calculation, according to other items in the cart (which might give extra discount).

  • Andriy Netseplyayev
    Andriy Netseplyayev Domain Lead Solution Architecture Sprykee Posts: 519 🧑🏻‍🚀 - Cadet
    edited November 2019

    Extending the calculation stack - would be the way I would implement that. Both discounts - spryker and your’s will stay separate, you can put your’s further in calculation stack, to be able to “overwrite” the default one. But you can of course try to find another way, by extending discounts, maybe some one else had such experience already and could share with you

  • UQ958DA4U
    UQ958DA4U Posts: 232 🧑🏻‍🚀 - Cadet

    Alright 🙂 Thank you very much!

  • UPWG9AYH2
    UPWG9AYH2 Posts: 509 🧑🏻‍🚀 - Cadet

    Hi @UKJSE6T47, thanks for your response.
    The calculation stack has already a DiscountCalculationPlugin right now which get’s in fact the QuoteTransfer. The calculation plugins we need at the moment are sub-parts of the DiscountCalculationPlugin ( like FixedPlugin, PerecentagePlugin etc.). But they just get DiscountableItems and Discount as input. When we would write a complete own calculator-plugin (e.g. “CustomDiscountCalculatorPlugin”) like you recommended, we can’t reuse the whole logic that is already in spryker (like the backend form, filter queries etc.).
    I don’t see a way to reach our goals by implementing another calculator plugin without discarding the existing spryker stuff, which we want to use in every case …
    So what would be your idea with “overwriting” the default one?

  • Andriy Netseplyayev
    Andriy Netseplyayev Domain Lead Solution Architecture Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    @UPWG9AYH2 okay, I thought the only thing you need is to send a cart items to some external service and get back f.e. “18%” and apply it to the cart. If you need to completely re-use Zed UI and combine it some how with your external system - that might be totally different requirement.

  • UPWG9AYH2
    UPWG9AYH2 Posts: 509 🧑🏻‍🚀 - Cadet

    Yeah, we added another plugin to the DiscountCalculator internal calculator stack. So, next to existing FixedPlugin and PerecentagePlugin there is now a “ExternalApiPlugin” which works out of the box especially for the backend form … the problem is - as already said - getting parameters into that plugin that are required for the external api call … like attributes from the cart etc. …