Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6304

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T05:18:07+00:00 2024-11-27T05:18:07+00:00

Customizing vendure

  • 60k

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case.

In this article, I'll take a high-level look at some elements we can customize within Vendure.

The cool part is that it mainly works via changing the one vendure-config.ts file.

Customizing models

The first thing one might want to do is add custom fields to any of the existing Entities.
I'm surprised by how easy this process is and how slick everything works.

Open your vendure-config.ts file and add or modify the customFields section.
In my case, I'll add a custom field to the product entity. You can give it any name you'd like.

export const config: VendureConfig = {   customFields: {     Product: [{ name: 'myCustomField', type: 'string' }],   }, }; 
Enter fullscreen mode Exit fullscreen mode

Since this will introduce a database change, we'll need to generate a new migration.
Don't worry. Vendure will take care of all the heavy lifting.

npm run migration:generate customField 
Enter fullscreen mode Exit fullscreen mode

And now, we can run our server, and everything will work.
If we visit a product, we should see the custom field.

Custom field in Vendure

Customizing the order process

Besides customizing the fields, you might want to add additional steps in the order process.

For instance, you might want to validate some aspects of the order or the customer.
This could be customer should be a valid tax-related customer, or an order above a certain weight can't be shipped.

So let's take the first example as the Vendure docs describe this best.

Currently, we would have the following state:

AddingItems > ArrangingPayment 
Enter fullscreen mode Exit fullscreen mode

We want it to be:

AddingItems > ValidateCustomer > ArrangingPayment 
Enter fullscreen mode Exit fullscreen mode

The first thing we need to do is define a new state. This is a physical text and doesn't do anything yet.

Let's create a new file to separate things: validate-customer.ts.

import { CustomOrderProcess } from '@vendure/core';  export const validateCustomer: CustomOrderProcess<'ValidateCustomer'> = {   transitions: {     AddingItems: {       to: ['ValidateCustomer'],       merge strategy: 'replace',     },     ValidateCustomer: {       to: ['ArrangingPayment', 'AddingItems'],     },   }, }; 
Enter fullscreen mode Exit fullscreen mode

As you can see, this tells what transitions are valid to make.
We can then open our vendure-config.ts and load our new process.

export const config: VendureConfig = {   orderOptions: {     process: [validateCustomer],   }, }; 
Enter fullscreen mode Exit fullscreen mode

Now let's make sure this intercepts something. Go back to the validate-customer.ts file and add the following function.

export const validateCustomer: CustomOrderProcess<'ValidateCustomer'> = {   // Transitions go here    // The logic for enforcing our validation goes here   async onTransitionStart(fromState, toState, data) {     if (fromState === 'ValidateCustomer' && toState === 'ArrangingPayment') {       const isValid = await validateTheActualCustomer(data.order.customer);       if (!isValid) {         // Returning a string is interpreted as an error message.         // The state transition will fail.         return `The customer is not valid`;       }     }   }, }; 
Enter fullscreen mode Exit fullscreen mode

This is simply a mockup, and you should create the actual validation function to evaluate the customer on specific fields.
If we return a string, it's handled as an error state, and the transition won't happen.

Generic modifiers

The above two are very common modifiers, but Vendure comes packed with more.

For instance, I needed to modify the stock level output in one of my shops.
By default, it only shows things as “low stock” and never the actual number.
On my webshop, it was needed to show the exact stock at hand.

To achieve that, we can open the vendure-config.ts and create a new function.

export class MyStockDisplayStrategy implements StockDisplayStrategy {   getStockLevel(     ctx: RequestContext,     productVariant: ProductVariant,     saleableStockLevel: number   ): string {     return saleableStockLevel.toString();   } } 
Enter fullscreen mode Exit fullscreen mode

This is a function to extend the default stock display strategy. In my case, I return the physical stock level as a string.
To ensure Vendure uses this value, we can modify the config to load that.

export const config: VendureConfig = {   catalogOptions: {     stockDisplayStrategy: new MyStockDisplayStrategy(),   }, }; 
Enter fullscreen mode Exit fullscreen mode

And now, the stock levels on the site will show the exact amount you have left in stock.

This is just one example, Vendure comes packed with small little hooks that we can use to customize our webshop and system.
I love that they made it easy and hassle-free to adjust the system to your needs.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

beginnersjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.