Git Product home page Git Product logo

Comments (6)

dylviz avatar dylviz commented on June 16, 2024 3

I think this could be handled with a strategy-based approach. This would be the most flexible I think. Something like:

export interface ProductVariantPriceUpdateStrategy extends InjectableStrategy {

  /**
   * This would be invoked whenever a price is changed.
   */
  onPriceUpdate(
    ctx: RequestContect, 
    updatedPrice: ProductVariantPrice, 
    allPrices: ProductVariantPrice[]): Promise<ProductVariantPrice[]> {
    
    // For example, let's get all the other prices with the same currencyCode
    const pricesWithSameCurrency = allPrices.filter(p => p.currencyCode === updatedPrice.currencyCode);
   
    // we can now sync the prices
    pricesWithSameCurrency.forEach(p => p.price = updatedPrice.price);
 
    // any returned ProductVariantPrice entities will have the new price
    // persisted to the DB.
    return pricesWithSameCurrency;
  }

}

I quite like this approach because:

* It requires zero changes to the data model

* It can handle different sync directions. E.g. one-way sync only from default channel to child channels, or two-way sync as in this example.

* It can be used for other use cases. For instance, let's say you wanted to have automatic currency conversion. This mechanism could be used to do a lookup on exchange rates and update other currencies accordingly.

Very nice approach! This way will be much better

from vendure.

michaelbromley avatar michaelbromley commented on June 16, 2024 2

I think this could be handled with a strategy-based approach. This would be the most flexible I think. Something like:

export interface ProductVariantPriceUpdateStrategy extends InjectableStrategy {

  /**
   * This would be invoked whenever a price is changed.
   */
  onPriceUpdate(
    ctx: RequestContect, 
    updatedPrice: ProductVariantPrice, 
    allPrices: ProductVariantPrice[]): Promise<ProductVariantPrice[]> {
    
    // For example, let's get all the other prices with the same currencyCode
    const pricesWithSameCurrency = allPrices.filter(p => p.currencyCode === updatedPrice.currencyCode);
   
    // we can now sync the prices
    pricesWithSameCurrency.forEach(p => p.price = updatedPrice.price);
 
    // any returned ProductVariantPrice entities will have the new price
    // persisted to the DB.
    return pricesWithSameCurrency;
  }

}

I quite like this approach because:

  • It requires zero changes to the data model
  • It can handle different sync directions. E.g. one-way sync only from default channel to child channels, or two-way sync as in this example.
  • It can be used for other use cases. For instance, let's say you wanted to have automatic currency conversion. This mechanism could be used to do a lookup on exchange rates and update other currencies accordingly.

from vendure.

michaelbromley avatar michaelbromley commented on June 16, 2024 1

@NoahPerez It's not available yet - it is going to be released in v2.2. You can try it now by installing the pre-release v2.2.0-next.3

from vendure.

michaelbromley avatar michaelbromley commented on June 16, 2024 1

Within the next 2-3 weeks is very likely.

from vendure.

NoahPerez avatar NoahPerez commented on June 16, 2024

@dylviz Where should I add ProductVariantPriceUpdateStrategy strategy-based approach or export it as a plugin?

or should I wait for Vendure 2.2 ?

from vendure.

NoahPerez avatar NoahPerez commented on June 16, 2024

@michaelbromley okay thanks, I will install the pre-release "@vendure/core": "2.2.0"
Do you know when you will release V2.2 ? i saw some nice improvements

from vendure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.