Ecommerce Marketing Tips
Ask Pin Code from shipping company
to run advert in meta
this will help shipping company to dispatch deliveries
|||||||||||||||||
function syncShopifyProducts() {
const SHOPIFY_API_KEY = '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';
const SHOPIFY_STORE = 'uniteds-nst';
const API_VERSION = '2025-04';
const LOCATION_ID = '98863644947';
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const headers = [
'Timestamp', 'Product ID', 'Product Title', 'Variant ID',
'Inventory Item ID', 'SKU', 'Color', 'Size',
'Quantity', 'Price', 'Location ID', 'Action'
];
// STEP 1: If sheet has only headers, fetch from Shopify
const lastRow = sheet.getLastRow();
if (lastRow <= 1) {
sheet.clear();
sheet.appendRow(headers);
const url =
`https://${SHOPIFY_STORE}.myshopify.com/admin/api/${API_VERSION}/products.json?
limit=250`;
const options = {
method: 'GET',
headers: {
'X-Shopify-Access-Token': SHOPIFY_API_KEY,
'Content-Type': 'application/json'
}
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText());
const timestamp = new Date().toLocaleString();
data.products.forEach(product => {
product.variants.forEach(variant => {
const color = getOptionValue(product.options, variant, 'color');
const size = getOptionValue(product.options, variant, 'size');
sheet.appendRow([
timestamp,
product.id,
product.title,
variant.id,
variant.inventory_item_id,
variant.sku,
color,
size,
variant.inventory_quantity,
variant.price,
LOCATION_ID,
'' // Action column
]);
});
});
Logger.log("Shopify product data fetched and added to sheet.");
return;
}
// STEP 2: Push inventory updates to Shopify if 'Action' is 'Update'
const data = sheet.getRange(2, 1, lastRow - 1, headers.length).getValues();
data.forEach((row, index) => {
const action = row[headers.indexOf('Action')];
if (action.toLowerCase() !== 'update') return;
const inventoryItemId = row[headers.indexOf('Inventory Item ID')];
const available = row[headers.indexOf('Quantity')];
const locationId = row[headers.indexOf('Location ID')];
const url =
`https://${SHOPIFY_STORE}.myshopify.com/admin/api/${API_VERSION}/inventory_levels/
set.json`;
const payload = JSON.stringify({
location_id: locationId,
inventory_item_id: inventoryItemId,
available: available
});
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'X-Shopify-Access-Token': SHOPIFY_API_KEY
},
payload: payload
};
try {
const response = UrlFetchApp.fetch(url, options);
Logger.log(`✅ Updated row ${index + 2} → ${inventoryItemId}: $
{response.getContentText()}`);
// Clear action column after success
sheet.getRange(index + 2, headers.length).setValue('');
} catch (e) {
Logger.log(`❌ Failed row ${index + 2}: ${e.message}`);
}
});
Logger.log("Shopify updates pushed where Action = 'Update'");
}
function getOptionValue(options, variant, optionName) {
for (let i = 0; i < options.length; i++) {
if (options[i].name.toLowerCase() === optionName.toLowerCase()) {
return variant[`option${i + 1}`] || '';
}
}
return '';
}
|||||||||||||||||||||
function onChange(e) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orders"); //
Update with your sheet name
const lastRow = sheet.getLastRow();
const formulaCell = "M"; // Column where the formula goes
const startRow = 4; // Row where data starts
// Your formula (adjust the column references if needed)
const formula = '=IF(OR(UPPER(I4)="Y", UPPER(J4)="Y", UPPER(K4)="Y",
UPPER(L4)="Y"), "✓", IF(OR(UPPER(I4)="N", UPPER(J4)="N", UPPER(K4)="N",
UPPER(L4)="N"), "X", ""))';
// Apply formula to all rows
for (let row = startRow; row <= lastRow; row++) {
const cell = sheet.getRange(`${formulaCell}${row}`);
if (cell.getFormula() === "") {
cell.setFormula(formula.replace(/4/g, row)); // Update the formula for each
row
}
}
}
function onEdit(e) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orders");
const range = e.range;
const editedColumn = range.getColumn();
const protectedColumn = 13; // Column M (13th column)
const password = "password"; // Set your password
if (editedColumn === protectedColumn) {
const ui = SpreadsheetApp.getUi();
const response = ui.prompt("Password Required", "Enter the password to edit
Column M:", ui.ButtonSet.OK_CANCEL);
if (response.getSelectedButton() === ui.Button.OK) {
const inputPassword = response.getResponseText();
if (inputPassword !== password) {
ui.alert("Incorrect password. Edit canceled.");
range.setValue(e.oldValue); // Revert the change
}
} else {
ui.alert("Edit canceled.");
range.setValue(e.oldValue); // Revert the change
}
}
}