A collection of utilities methods for Vtex stores
npm install vtex-utils


A collection of utilities methods for Vtex stores.
- Main
- Getting started
- UtilifyJS
- Vtex Methods
- Rivets Utilify
- Tests
- License
- Dependencies
``text`
dist/
├── vtex-utils.js (UMD)
├── vtex-utils.min.js (UMD, compressed)
├── vtex-utils.common.js (CommonJS, default)
└── vtex-utils.esm.js (ES Module)
Download the script here and include it.
`html`
VtexUtils.js supports npm under the name vtex-utils.
`shell`
npm install vtex-utils --save
VtexUtils.js can also be loaded as an CommonJS or ES6 module (recomended).
`js
// CommomJS
var VtexUtils = require('vtex-utils');
// ES6 module
import VtexUtils from 'vtex-utils';
`
With UMD (Universal Module Definition), the package is available on VTEX namespace.
`js
// Initialize constructor
var vtexUtils = new VTEX.VtexUtils();
// VtexHelpers
var vtexHelpers = vtexUtils.vtexHelpers;
// GlobalHelpers (from UtilifyJS)
var globalHelpers = vtexUtils.globalHelpers;
// LocationHelpers (from UtilifyJS)
var locationHelpers = vtexUtils.locationHelpers;
// Storage (from Store2 | https://github.com/nbubna/store)
var storage = vtexUtils.storage
`
Docs for vtexUtils.globalHelpers and vtexUtils.locationHelpers methods can be found on UtilifyJS
Formats Vtex price
- number:
- Type: Integer
- The number to format
- thousands (optional):
- Type: String'.'
- Default:
- The thousands delimiter
- decimals (optional):
- Type: String','
- Default:
- The decimal delimiter
- length (optional):
- Type: Integer2
- Default:
- The length of decimal
- currency (optional):
- Type: String'R$ '
- Default:
- Set currency
#### Example
`js`
vtexHelpers.formatPrice(1234); // R$ 12,34
vtexHelpers.formatPrice(123456); // R$ 1.234,56
vtexHelpers.formatPrice(123456, null, ',', 3); // R$ 1234,560
vtexHelpers.formatPrice(123456, ',', '.', 2, '$ '); // $ 1,234.56
Unformat Vtex price. Value can be a single string or an array of values
- value:
- Type: String|Array
- Price formatted
- decimal (optional):
- Type: String','
- Default:
- The decimal delimiter
- formatNumber (optional):
- Type: Booleanfalse
- Default: '.'
- Thousands separator (pt-BR default: )
#### Example
`js
var priceFormatted = 'R$ 1.234,56';
vtexHelpers.unformatPrice(priceFormatted); // {unformatted: 123456, real: '1234', cents: '56'}
vtexHelpers.unformatPrice(priceFormatted, null, true); // {unformatted: 123456, real: '1.234', cents: '56'}
vtexHelpers.unformatPrice(['R$ 12,34', 'R$ 23,45', 'R$ 34,56']);
/*
[
{unformatted: 1234, real: '12', cents: '34'}
{unformatted: 2345, real: '23', cents: '45'}
{unformatted: 3456, real: '34', cents: '56'}
]
*/
`
Take the value of the installment with min price and max installments given
- price:
- Type: String|Number
- Price to get installments. Can be formatted price or a integer value.
- minPrice:
- Type: String|Number
- Min price for each installment. Can be formatted price or a integer value.
- maxInstallments:
- Type: Number
- Max installments
- interest (optional):
- Type: Number0
- Default:
- Interest rate
#### Example
`js`
vtexHelpers.setInstallment('R$ 3.499,00', 'R$ 430,00', 10) // {installments: 8, installmentValue: 43737, interest: 0}
vtexHelpers.setInstallment(349900, 43000, 10) // {installments: 8, installmentValue: 43737, interest: 0}
Get the percentage of a discount
- oldPrice:
- Type: String|Number
- Original price. Can be formatted price or a integer value.
- newPrice:
- Type: String|Number
- Price with discount. Can be formatted price or a integer value.
- length (optional):
- Type: Number0
- Default:
- Number of decimals
#### Example
`js`
vtexHelpers.getPercentage('R$ 179,90', 'R$ 149,50'); // 17 (17% OFF)
vtexHelpers.getPercentage(17990, 14900, 2); // 17.18 (17.18% OFF)
Returns a discount amount or adding a set value.
- price:
- Type: String|Number
- Price to apply discount. Can be formatted price or a integer value.
- percent:
- Type: String|Number
- Percentage to apply. Can be formatted price or a integer value.
- formatted (optional):
- Type: Booleanfalse
- Default:
- Format result
#### Example
`js`
vtexHelpers.applyDiscountPercent('R$ 9,55', 37.27); // {discountPrice: 355, priceWithDiscount: 599, priceWithIncrease: 1310}
vtexHelpers.applyDiscountPercent('R$ 9,55', '37.27%'); // {discountPrice: 355, priceWithDiscount: 599, priceWithIncrease: 1310}
vtexHelpers.applyDiscountPercent('R$ 9,55', '37,27%'); // {discountPrice: 355, priceWithDiscount: 599, priceWithIncrease: 1310}
vtexHelpers.applyDiscountPercent(955, 37.27, true); // {discountPrice: 'R$ 3,55', priceWithDiscount: 'R$ 5,99', priceWithIncrease: 'R$ 13,10'}
Formats price from Vtex API /api/catalog_system/pub/products/search/ to use in formatPrice method
- val:
- Type: Number
- Value to convert
#### Example
`js`
vtexHelpers.fixProductSearchPrice(250.25); // 25025
Get first available SKU from Vtex API /api/catalog_system/ end point
Returns an object with first available SKU or false if all are unvailable
- product:
- Type: Object
- Product full data
#### Example
`js`
vtexjs.catalog.getProductWithVariations(1234)
.then(function(res) {
var availableSku = vtexHelpers.getFirstAvailableSku(res);
window.console.log(availableSku);
});
Get the original VTEX image source from a thumb
- src:
- Type: String
- The source of the thumb
#### Example
`js`
vtexHelpers.getOriginalImage('http://domain.vteximg.com.br/arquivos/ids/155242-292-292/image.png');
// http://domain.vteximg.com.br/arquivos/ids/155242/image.png
Change the width & height from a given VTEX image source
- src:
- Type: String
- The source of the image
- width:
- Type: String | Integer
- The new image with
- height:
- Type: String | Integer
- The new image height
#### Example
`js
vtexHelpers.getResizedImage('http://domain.vteximg.com.br/arquivos/ids/155242-292-292/image.png', 500, 600);
// http://domain.vteximg.com.br/arquivos/ids/155242-500-600/image.png
vtexHelpers.getResizedImage('http://domain.vteximg.com.br/arquivos/ids/155242/image.png', 100, 100);
// http://domain.vteximg.com.br/arquivos/ids/155242-100-100/image.png
`
Resize proportionally an VTEX image by aspect ratio
- src:
- Type: String
- The source of the image
- type:
- Type: String
- Type to resize (width or height)
- newSize:
- Type: Number
- New size to redimensioning
- aspectRatio:
- Type: Number
- Image aspect ratio (calculate by (width / height))
#### Example
`js
var imgSrc = 'http://domain.vteximg.com.br/arquivos/ids/155242-292-292/image.png';
var newSize = 250;
var aspectRatio = (10/15);
vtexHelpers.getResizeImageByRatio(imgSrc, 'width', newSize, aspectRatio);
// http://domain.vteximg.com.br/arquivos/ids/155242-250-375/image.png
vtexHelpers.getResizeImageByRatio(imgSrc, 'height', newSize, aspectRatio);
// http://domain.vteximg.com.br/arquivos/ids/155242-167-250/image.png
`
Get the Vtex server time
- callback:
- Type: Function
- The callback to call when the request finishes. The callback will a javascript Date object.
#### Example
`js`
vtexHelpers.getServerTime(function(date) {
window.console.log(date.getFullYear());
});
Get category tree
- categoryId (optional):
- Type: Integerundefined
- Default:
- Return the specific Category
- depth (optional):
- Type: Integer50
- Default:
- The tree depth
#### Example
`js
vtexHelpers.getCategories().then(function(res) {
window.console.log(res)
}); // Return all categories
vtexHelpers.getCategories(1000001, 1).then(function(res) {
window.console.log(res));
}); // Return first level from category id
`
Get product specification
- data:
- Type: Object/api/catalog_system/pub/products/search/
- Vtex API data from endpoint
- specName:
- Type: String
- Specification name
- defaultVal (optional):
- Type: Boolean|Stringfalse
- Default:
- Value to return if spec doesn't exists
#### Example
`js
// Data from API '/api/catalog_system/pub/products/search/'
var response = {
Características: ["Cor real", "Ocasião", "Composição", "Cor"],
Composição: ["Liga Mista, Pedra Natural Pirita"],
Cor: ["Dourado"],
Cor real: ["Ouro"],
Ocasião: ["Weekend"],
allSpecifications: ["Cor real", "Ocasião", "Composição", "Cor"],
// More data...
};
vtexHelpers.getProductSpec(response, 'Cor'); // 'Dourado'
vtexHelpers.getProductSpec(response, 'Cores'); // false
vtexHelpers.getProductSpec(response, 'Cores', 'Branco'); // 'Branco'
`
Get product shipping value.
- postalCode:
- Type: Stringxxxxx-xxx
- Postal code to calculate. Can be or xxxxxxxx format
- skuId (optional):
- Type: IntegerskuId
- Default: skuJson
- Sku ID to calculate. If not passed, get first sku available on product page from global variable. If this method used on other pages, skuId is required
- quantity (optional):
- Type: Integer1
- Default:
- Product quantity to calculate
#### Example
`js
// On product page with a valid postal code
vtexHelpers.getShipping('01010-010')
.then(function(res) {
if ( !res.error ) {
console.log('SUCCESS', res);
}
if ( res.error ) {
console.log('INVALID TYPE', res);
}
});
/*
Case success, will return an object with properties:
{
error: false,
formattedResponse: {
shippingValue: 'R$ 10,00',
shippingText: 'Entrega em 4 dias úteis para o CEP 01010-010',
shippingType: 'Frete Transportadora'
}
fullResponse: '