GTM - Creating a list of products in GTM - Wordpress - javascript

Ahoy,
i am using built in gtm plugin on wordpress/woocomerce and it gets the following content:
cartContent: {
totals: {
applied_coupons: [],
discount_total: 0,
subtotal: "2844.715447",
total: "2844.715447"
},
items: [
{
id: "K-GG-V4-S",
name: "Grill Gazowy Koler Relish v4 5 palników 17,2kW",
sku: "K-GG-V4-S",
category: "Grille Gazowe",
price: 3499,
stocklevel: 41,
quantity: 1
},
{
id: "5900000002453",
name: "Fartuch Koler",
sku: "5900000002453",
category: "Wszystkie produkty",
price: 0,
stocklevel: null,
quantity: 1
},
{
id: "6600006666664",
name: "Pokrowiec + wąż i reduktor",
sku: "6600006666664",
category: "Wszystkie produkty",
price: 0,
stocklevel: null,
quantity: 1
}
]
},
but I have to implement the entire content of the basket into something that is to change dynamically depending on the number of products:
<script>
wph('track', 'AddToCart', {
content_type: 'category',
contents: [{
id: 'PRODUKT_ID1',
name: 'NAZWA_PRODUKTU1',
category: 'KATEGORIA_PRODUKTU1',
ean: 'PRODUKT_EAN_ID1',
price: 20.15,
in_stock: true
quantity: 1
weight : 'WAGA PRODUKTU'
}, {
id: 'PRODUKT_ID2',
name: 'NAZWA_PRODUKTU2',
category: 'KATEGORIA_PRODUKTU2',
ean: 'PRODUKT_EAN_ID2',
price: 20.15,
in_stock: true
quantity: 1
weight : 'WAGA PRODUKTU'
}]
})
</script>
How can I do this in GTM? I must admit that it was a bit too much for me, because I usually didn't need the entire basket.

Related

Question from a beginner: Unexpected JS behavior [duplicate]

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

How can I sort this array of objects alphabetically in node js?

I have a question about how to order this array of objects alphabetically, knowing that some of the elements of the array are in lowercase and others have special characters.
The array is:
Product { id: 1, name: 'TV Samsung MP45', price: 325.9, units: 8 },
Product { id: 2, name: 'Ábaco de madera (nuevo modelo)', price: 245.95, units: 15 },
Product { id: 3, name: 'impresora Epson', price: 55.9, units: 8 },
Product { id: 4, name: 'USB Kingston 16GB', price: 5.95, units: 45 }
I must sort alphabetically with this shape:
Product { id: 2, name: 'Ábaco de madera (nuevo modelo)', price: 245.95, units: 15 },
Product { id: 3, name: 'impresora Epson', price: 55.9, units: 8 },
Product { id: 1, name: 'TV Samsung MP45', price: 325.9, units: 8 },
Product { id: 4, name: 'USB Kingston 16GB', price: 5.95, units: 45 }
But the result is the following:
Product { id: 1, name: 'TV Samsung MP45', price: 325.9, units: 8 },
Product { id: 2, name: 'Ábaco de madera (nuevo modelo)', price: 245.95, units: 15 },
Product { id: 3, name: 'impresora Epson', price: 55.9, units: 8 },
Product { id: 4, name: 'USB Kingston 16GB', price: 5.95, units: 45 }
To order the array, I use the sort function like this:
orderByName(){
return this.products.sort((a,b) => a.name - b.name);
return a.name - b.name;
}
I have tried many things to get it to order with some words with special characters and some are lowercase.
Does anyone know any solutions?
You can call String#normalize on both names before comparing them with localeCompare:
const arr = [
{ id: 1, name: 'TV Samsung MP45', price: 325.9, units: 8 },
{ id: 2, name: 'Ábaco de madera (nuevo modelo)', price: 245.95, units: 15 },
{ id: 3, name: 'impresora Epson', price: 55.9, units: 8 },
{ id: 4, name: 'USB Kingston 16GB', price: 5.95, units: 45 }
];
arr.sort((a, b) => a.name.normalize().localeCompare(b.name.normalize()));
console.log(arr);

ES6: Populate object value with a value from another object when ID is present

I would like to populate the value 'discount' for a product with the discount ID value from the array discounts, if the respective product ID exists as a value in the Discounts object.
const products = [{
id: "05cdb75d-7984-4dbf-b0f4-d6532163b66d",
name: "SANTO - Schnürstiefelette",
price: 199.95,
discount: 0,
},
{
id: "1b9b6c7e-c856-464c-ba64-98c9dd6733b5",
name: "AIR FORCE 1 07 LV8 - Sneaker low",
price: 109.95,
discount: 0,
},
{
id: "f831aaf4-347a-458f-bb0c-21cf02aeac2e",
name: "DUFF 9.0 - Sporttasche",
price: 34.95,
discount: 0,
},
{
id: "471ad894-150b-4a2b-881c-a9a4dbc4b401",
name: "Strickpullover",
price: 20.99,
discount: 0,
},
];
const discounts = [{
id: "5791ae04-a704-4f44-808b-de5ddb8812b5",
name: "Christmas discount",
productIds: ["1b9b6c7e-c856-464c-ba64-98c9dd6733b5", "f831aaf4-347a-458f-bb0c-21cf02aeac2e"],
active: true
},
{
id: "5791ae04-a704-4f44-808b-de5ddb8812e6",
name: "Christmas discount 2",
productIds: ["05cdb75d-7984-4dbf-b0f4-d6532163b66d"],
active: true
}
];
At the end i need it like:
const products = [{
id: "05cdb75d-7984-4dbf-b0f4-d6532163b66d",
name: "SANTO - Schnürstiefelette",
price: 199.95,
discount: '5791ae04-a704-4f44-808b-de5ddb8812e6',
},
...
...
You could use map() to transform products array. And find() and includes() to check if discount exists for a product.
const products = [{ id: "05cdb75d-7984-4dbf-b0f4-d6532163b66d", name: "SANTO - Schnürstiefelette", price: 199.95, discount: 0, }, { id: "1b9b6c7e-c856-464c-ba64-98c9dd6733b5", name: "AIR FORCE 1 07 LV8 - Sneaker low", price: 109.95, discount: 0, }, { id: "f831aaf4-347a-458f-bb0c-21cf02aeac2e", name: "DUFF 9.0 - Sporttasche", price: 34.95, discount: 0, }, { id: "471ad894-150b-4a2b-881c-a9a4dbc4b401", name: "Strickpullover", price: 20.99, discount: 0, }, ];
const discounts = [{ id: "5791ae04-a704-4f44-808b-de5ddb8812b5", name: "Christmas discount", productIds: ["1b9b6c7e-c856-464c-ba64-98c9dd6733b5", "f831aaf4-347a-458f-bb0c-21cf02aeac2e"], active: true }, { id: "5791ae04-a704-4f44-808b-de5ddb8812e6", name: "Christmas discount 2", productIds: ["05cdb75d-7984-4dbf-b0f4-d6532163b66d"], active: true } ];
let result = products.map(product => {
let discount = discounts.find(item => item.productIds.includes(product.id));
return {
...product,
"discount": discount ? discount.id : product.discount
};
});
console.log(result);
You could store the discounts in a Map and map the object with a new discount object, if necessary.
var products = [{ id: "05cdb75d-7984-4dbf-b0f4-d6532163b66d", name: "SANTO - Schnürstiefelette", price: 199.95, discount: 0 }, { id: "1b9b6c7e-c856-464c-ba64-98c9dd6733b5", name: "AIR FORCE 1 07 LV8 - Sneaker low", price: 109.95, discount: 0 }, { id: "f831aaf4-347a-458f-bb0c-21cf02aeac2e", name: "DUFF 9.0 - Sporttasche", price: 34.95, discount: 0 }, { id: "471ad894-150b-4a2b-881c-a9a4dbc4b401", name: "Strickpullover", price: 20.99, discount: 0 }],
discounts = [{ id: "5791ae04-a704-4f44-808b-de5ddb8812b5", name: "Christmas discount", productIds: ["1b9b6c7e-c856-464c-ba64-98c9dd6733b5", "f831aaf4-347a-458f-bb0c-21cf02aeac2e"], active: true }, { id: "5791ae04-a704-4f44-808b-de5ddb8812e6", name: "Christmas discount 2", productIds: ["05cdb75d-7984-4dbf-b0f4-d6532163b66d"], active: true }],
ids = discounts.reduce((m, { id, productIds }) => productIds.reduce((n, pid) => n.set(pid, id), m), new Map);
products = products.map(p => Object.assign({}, p, ids.has(p.id) && { discount: ids.get(p.id) }));
console.log(products);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nested loop through both products and discounts array, if the product id is included inside the productIds array of any of the objects in the discounts array, assign the discount id to the product discount.
for (let product of products) {
for (let discount of discounts) {
if (discount.productIds.includes(product.id)){
product.discount = discount.id;
break;
}
}
}

array of objects save in mongodb

i am facing problem when i insert array of object in mongodb
myarray = [
{ id: 213,
name: 'MonaCoin',
symbol: 'MONA',
price: 0.629564688986,
volume: 2226709.2039523,
last_update: '2018-12-03T18:45:04.000Z' },
{ id: 1169,
name: 'PIVX',
symbol: 'PIVX',
price: 0.706248118629,
volume: 310213.338790205,
last_update: '2018-12-03T18:45:08.000Z' },
{ id: 2062,
name: 'Aion',
symbol: 'AION',
price: 0.1502951806,
volume: 1827674.44495399,
last_update: '2018-12-03T18:45:17.000Z' },
{ id: 1727,
name: 'Bancor',
symbol: 'BNT',
price: 0.655718694418,
volume: 1598779.80773313,
last_update: '2018-12-03T18:45:11.000Z' }
]
and the function i used are "insertmany" "create" and "save" function:
myschema.insertmany(myarray,(err,obj)=>{
console.log("saved"obj)
})
it show saved but no data saved in mongodb,
please tell me what is the best approach for this scenario.

javascript group by in array

I want to group an array that I have. For example:
var shoppingCart = [
{
productId: '123',
price: 12,99
quantity: 1,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '457',
price: 83,33
quantity: 2,
shopId: 'asw',
shopName: 'shop 2'
},
{
productId: '4232',
price: 47,21
quantity: 1,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '3332',
price: 9,99
quantity: 4,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '33221',
price: 99,21
quantity: 1,
shopId: 'asw',
shopName: 'shop 2'
},
{
productId: '452113',
price: 22,45
quantity: 2,
shopId: 'asdj',
shopName: 'shop 3'
}
]
I want to show as follows:
Shop 1
ProductId: 123
ProductId: 4232
ProductId: 3332
Shop 2
ProductId: 457
ProductId: 33221
Shop 3
ProductId 452113
How can I do that? The idea is that there will be created seperate orders for every store.
Use reduce to create an object with a key for each unique shop name. Push items to these arrays as you go along:
// Group objects in an array by a property
var mapBy = function(arr, groupName, propName) {
return arr.reduce(function(result, item) {
result[item[groupName]] = result[item[groupName]] || [];
result[item[groupName]].push(item[propName]);
return result;
}, {});
};
var shoppingCart=[{productId:"123",price:12.99,quantity:1,shopId:"abc",shopName:"shop 1"},{productId:"457",price:83.33,quantity:2,shopId:"asw",shopName:"shop 2"},{productId:"4232",price:47.21,quantity:1,shopId:"abc",shopName:"shop 1"},{productId:"3332",price:9.99,quantity:4,shopId:"abc",shopName:"shop 1"},{productId:"33221",price:99.21,quantity:1,shopId:"asw",shopName:"shop 2"},{productId:"452113",price:22.45,quantity:2,shopId:"asdj",shopName:"shop 3"}];
console.log(mapBy(shoppingCart, "shopName", "productId"));
You can use reduce() like this and return object.
var shoppingCart = [{"productId":"123","price":12,"quantity":1,"shopId":"abc","shopName":"shop 1"},{"productId":"457","price":83,"quantity":2,"shopId":"asw","shopName":"shop 2"},{"productId":"4232","price":47,"quantity":1,"shopId":"abc","shopName":"shop 1"},{"productId":"3332","price":9,"quantity":4,"shopId":"abc","shopName":"shop 1"},{"productId":"33221","price":99,"quantity":1,"shopId":"asw","shopName":"shop 2"},{"productId":"452113","price":22,"quantity":2,"shopId":"asdj","shopName":"shop 3"}]
var r = shoppingCart.reduce(function(r, e) {
r[e.shopName] = (r[e.shopName] || []).concat({productId: e.productId})
return r;
}, {})
console.log(r)
I propose that using filter() method
var shoppingCart = [
{
productId: '123',
price: 12.99,
quantity: 1,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '457',
price: 83.33,
quantity: 2,
shopId: 'asw',
shopName: 'shop 2'
},
{
productId: '4232',
price: 47.21,
quantity: 1,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '3332',
price: 9.99,
quantity: 4,
shopId: 'abc',
shopName: 'shop 1'
},
{
productId: '33221',
price: 99.21,
quantity: 1,
shopId: 'asw',
shopName: 'shop 2'
},
{
productId: '452113',
price: 22.45,
quantity: 2,
shopId: 'asdj',
shopName: 'shop 3'
}
];
var shop1 = shoppingCart.filter(x => x.shopName === 'shop 1').map(x => ({productId: x.productId}));
console.log(shop1); // [ { productId: '123' }, { productId: '4232' },{ productId: '3332' } ]

Categories