245 lines
8.6 KiB
PHP
245 lines
8.6 KiB
PHP
<?php
|
|
defined($security_key) or exit;
|
|
|
|
ini_set('display_errors', '1');
|
|
ini_set('display_startup_errors', '1');
|
|
error_reporting(E_ALL);
|
|
|
|
//------------------------------------------
|
|
// Catalog
|
|
//------------------------------------------
|
|
|
|
//------------------------------------------
|
|
//Create Catalog
|
|
//------------------------------------------
|
|
$catalog = []; // Main catalog array
|
|
|
|
//NEW ARRAY
|
|
$criterias = [];
|
|
$filter = '';
|
|
|
|
//Check for $_GET variables and build $_GET
|
|
if(isset($get_content) && $get_content!=''){
|
|
//GET VARIABLES FROM URL
|
|
$requests = explode("&", $get_content);
|
|
//Check for keys and values
|
|
foreach ($requests as $y){
|
|
$v = explode("=", $y);
|
|
//INCLUDE VARIABLES IN ARRAY
|
|
$criterias[$v[0]] = $v[1] ?? true;
|
|
|
|
if ($v[0] == 'category'){
|
|
//------------------------------------------
|
|
//IF CATEGORY IS RECEIVED ONLY GET RELATED PRODUCTS
|
|
//------------------------------------------
|
|
$url_input = '';
|
|
//Get all the related products from the database
|
|
$cat_products = ioAPIv2('/v2/products_categories/list=filter&category_id='.$v[1],'',$clientsecret);
|
|
$cat_products = json_decode($cat_products,true);
|
|
|
|
foreach($cat_products as $cat_product_id){
|
|
$url_input .= $cat_product_id['product_id'].',';
|
|
}
|
|
$filter .= '&product_id='.substr($url_input,0,-1);
|
|
|
|
}
|
|
elseif ($v[0] == 'product_id'){
|
|
$filter .= '&product_id='.$v[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
//------------------------------------------
|
|
//GET ACTIVE AND SALES RELATED PRODUCTS
|
|
//------------------------------------------
|
|
|
|
//GET PRODUCTS
|
|
$api_url = '/v2/products/salesflag=1&status=1'.$filter;
|
|
$products = ioApi($api_url,'',$clientsecret);
|
|
$products = json_decode($products,true);
|
|
|
|
foreach ($products as $product) {
|
|
|
|
//------------------------------------------
|
|
// Create product entry in catalog if it doesn't exist
|
|
//------------------------------------------
|
|
if (!isset($catalog[$product['rowID']])) {
|
|
$catalog[$product['rowID']] = $product;
|
|
$catalog[$product['rowID']]['versions'] = []; // Changed to versions array
|
|
}
|
|
|
|
//------------------------------------------
|
|
//Check for configurations and add to product
|
|
//------------------------------------------
|
|
if (isset($product['configurable']) && $product['configurable'] == 1){
|
|
|
|
//GET ACTIVE CONFIGURATIONS ITEMS BASED ON ACTIVE VERSIONS
|
|
$api_url = '/v2/products_configurations/status=1&version_status=1&productrowid='.$product['rowID'] ;
|
|
$product_config = ioApi($api_url,'',$clientsecret);
|
|
$product_config = json_decode($product_config,true);
|
|
|
|
//------------------------------------------
|
|
// Group configurations by version
|
|
//------------------------------------------
|
|
$version_configurations = [];
|
|
|
|
foreach ($product_config as $item) {
|
|
|
|
if ($item['productrowid'] == $product['rowID']) {
|
|
// Initialize version array if it doesn't exist
|
|
if (!isset($version_configurations[$item['version']])) {
|
|
|
|
$version_configurations[$item['version']] = [
|
|
'version_id' => $item['version'],
|
|
'config_setting' => $item['config'],
|
|
'main_option_for_display' => $item['measurement'] ?? '',
|
|
'configurations' => []
|
|
];
|
|
}
|
|
|
|
if ($item['type'] == 'product') {
|
|
|
|
$version_configurations[$item['version']]['configurations'][] = $item;
|
|
}
|
|
|
|
if ($item['type'] == 'group') {
|
|
$api_url = '/v2/products_attributes_items/item_status=1&list=catalog&group_id='.$item['assignment'];
|
|
$attributes = ioApi($api_url,'',$clientsecret);
|
|
$attributes = json_decode($attributes,true);
|
|
|
|
// Add attributes to the group item
|
|
$item['attributes'] = $attributes;
|
|
$version_configurations[$item['version']]['configurations'][] = $item;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add all version configurations to the catalog
|
|
$catalog[$product['rowID']]['versions'] = array_values($version_configurations);
|
|
}
|
|
}
|
|
|
|
//------------------------------------------
|
|
// Lookup pricing (active pricelist and pricelistitems only)
|
|
//------------------------------------------
|
|
$api_url = '/v2/pricelists/status=1&item_status=1&list=price';
|
|
$pricelist = ioApi($api_url,'',$clientsecret);
|
|
$pricelist = json_decode($pricelist,true);
|
|
|
|
foreach ($pricelist as $price) {
|
|
|
|
// Add price to product level
|
|
if (isset($catalog[$price['product_id']])) {
|
|
$catalog[$price['product_id']]['price'] = $price['price'];
|
|
$catalog[$price['product_id']]['rrp'] = $price['rrp'];
|
|
$catalog[$price['product_id']]['price_modifier'] = $price['price_modifier'];
|
|
}
|
|
|
|
//Check for configuration (can also include products as above)
|
|
foreach ($catalog as &$items) {
|
|
if (!empty($items['versions'])) {
|
|
foreach ($items['versions'] as &$version) {
|
|
foreach ($version['configurations'] as &$config) {
|
|
//UPDATE PRODUCT PRICES IN CONFIGURATION
|
|
if ($config['type'] == 'product' && $config['assignment'] == $price['product_id']) {
|
|
$config['price'] = $price['price'];
|
|
$config['rrp'] = $price['rrp'];
|
|
$config['price_modifier'] = $price['price_modifier'];
|
|
}
|
|
|
|
//UPDATE PRICES OF ATTRIBUTES IN GROUPS
|
|
if ($config['type'] == 'group') {
|
|
//check all attributes
|
|
foreach($config['attributes'] as &$attribute) {
|
|
if ($attribute['attribute_id'] == $price['product_id']) {
|
|
$attribute['price'] = $price['price'];
|
|
$attribute['rrp'] = $price['rrp'];
|
|
$attribute['price_modifier'] = $price['price_modifier'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//------------------------------------------
|
|
//REMOVE KEYS FROM OUTPUT
|
|
//------------------------------------------
|
|
$keys_to_remove = ['status','item_status','group_status','version','config','sn','build','softwareversion','healthindex','salesflag','configurable','updatedby','createdby','updated','created'];
|
|
removeKeysRecursive($catalog,$keys_to_remove);
|
|
|
|
//------------------------------------------
|
|
//Catalog processor to split versions as new product
|
|
//------------------------------------------
|
|
$messages = processProductCollection($catalog);
|
|
|
|
//------------------------------------------
|
|
//check for METAfeed request
|
|
//------------------------------------------
|
|
if (isset($criterias['meta'])){
|
|
//------------------------------------------
|
|
// Meta Feed Configuration
|
|
//------------------------------------------
|
|
$meta_config = [
|
|
'base_url' => 'https://www.morvalwatches.com', // Product page URL
|
|
'image_base_url' => 'https://cloud.soveliti.nl',
|
|
'brand' => 'Morval Watches',
|
|
'currency' => 'EUR',
|
|
'condition' => 'new',
|
|
'availability' => 'in stock',
|
|
'google_product_category' => 'Apparel & Accessories > Jewelry > Watches',
|
|
'output_format' => 'json' // Options: 'csv', 'xml', 'json'
|
|
];
|
|
|
|
//------------------------------------------
|
|
// Product Code Based Descriptions
|
|
// Keys can be: exact code (MWTH2NB) or pattern (MWTH1*, MWTH2*)
|
|
//------------------------------------------
|
|
$meta_descriptions = [
|
|
// Pattern based (will match any product starting with this)
|
|
'MWTH1' => 'The Thomas-I exudes elegance and sophistication. Classic dimensions combined with subtle details in the dial make it an special automatic watch that can be worn on all occasions.',
|
|
'MWTH2' => 'The Thomas-II provides a view of the beating heart of the Swiss timepiece. It marks the precision and perfection with which the time is displayed.',
|
|
'MWABR' => 'Handmade Italian calf leather bracelet',
|
|
];
|
|
|
|
//------------------------------------------
|
|
//Include meta functions
|
|
//------------------------------------------
|
|
include_once './assets/functions_meta.php';
|
|
|
|
$meta_feed = catalogToMetaFeed($messages, $meta_config);
|
|
//------------------------------------------
|
|
// Output based on format parameter
|
|
//------------------------------------------
|
|
if ($criterias['meta'] === true) {
|
|
$format = $meta_config['output_format'];
|
|
} else {
|
|
$format = $criterias['meta'];
|
|
}
|
|
|
|
switch ($format) {
|
|
case 'xml':
|
|
$messages = outputMetaFeedXML($meta_feed);
|
|
break;
|
|
case 'csv':
|
|
$messages = outputMetaFeedCSV($meta_feed);
|
|
break;
|
|
case 'json':
|
|
default:
|
|
$messages = outputMetaFeedJSON($meta_feed);
|
|
break;
|
|
}
|
|
exit();
|
|
}
|
|
//------------------------------------------
|
|
//JSON_ENCODE
|
|
//------------------------------------------
|
|
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);
|
|
|
|
//------------------------------------------
|
|
//Send results
|
|
//------------------------------------------
|
|
echo $messages;
|
|
|
|
?>
|