added ProductLinkCheckerProductDataGenerateModuleFrontController
This commit is contained in:
120
controllers/front/productdatagenerate.php
Normal file
120
controllers/front/productdatagenerate.php
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Product Link Checker Product Data Generate Controller
|
||||||
|
*
|
||||||
|
* This controller generates a JSON feed of all products and their attribute combinations
|
||||||
|
* with detailed information for external services.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ProductLinkCheckerProductDataGenerateModuleFrontController extends ModuleFrontController
|
||||||
|
{
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
parent::init();
|
||||||
|
|
||||||
|
// Security check: Validate the token, same as the other controller
|
||||||
|
$token = Tools::getValue('token');
|
||||||
|
$storedToken = Configuration::get('PLC_SECURITY_TOKEN');
|
||||||
|
|
||||||
|
if (!$token || $token !== $storedToken) {
|
||||||
|
header('HTTP/1.1 403 Forbidden');
|
||||||
|
exit('Invalid security token.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initContent()
|
||||||
|
{
|
||||||
|
parent::initContent();
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
$id_shop_filter = (int)Tools::getValue('plc_id_shop');
|
||||||
|
$id_lang_filter = (int)Tools::getValue('plc_id_lang');
|
||||||
|
|
||||||
|
// Determine which shops to scan
|
||||||
|
if ($id_shop_filter) {
|
||||||
|
$shop_ids = [$id_shop_filter];
|
||||||
|
} else {
|
||||||
|
$shop_ids = Shop::getShops(true, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine which languages to scan
|
||||||
|
if ($id_lang_filter) {
|
||||||
|
$lang_ids = [$id_lang_filter];
|
||||||
|
} else {
|
||||||
|
$lang_ids = Language::getLanguages(true, false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$all_product_data = [];
|
||||||
|
|
||||||
|
foreach ($shop_ids as $id_shop) {
|
||||||
|
foreach ($lang_ids as $id_lang) {
|
||||||
|
// Get all active products for the current shop and language
|
||||||
|
$products = Product::getProducts($id_lang, 0, 0, 'id_product', 'ASC', false, true);
|
||||||
|
|
||||||
|
foreach ($products as $product_data) {
|
||||||
|
$product = new Product($product_data['id_product'], false, $id_lang, $id_shop);
|
||||||
|
|
||||||
|
// Skip invalid or unloaded products
|
||||||
|
if (!Validate::isLoadedObject($product)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Add the main product data (as a product without a specific combination)
|
||||||
|
$all_product_data[] = [
|
||||||
|
'id_product' => (int)$product->id,
|
||||||
|
'id_product_attribute' => 0, // 0 for the main product
|
||||||
|
'meta_title' => $product->meta_title,
|
||||||
|
// Grouping meta fields for clarity
|
||||||
|
'meta_description' => $product->meta_description,
|
||||||
|
'title' => $product->name,
|
||||||
|
'description' => $product->description,
|
||||||
|
'description_short' => $product->description_short,
|
||||||
|
'mpn' => $product->mpn,
|
||||||
|
'reference' => $product->reference,
|
||||||
|
'ean13' => $product->ean13,
|
||||||
|
'upc' => $product->upc,
|
||||||
|
'id_language' => (int)$id_lang,
|
||||||
|
'id_shop' => (int)$id_shop,
|
||||||
|
'link' => $this->context->link->getProductLink($product),
|
||||||
|
'link_rewrite' => $product->link_rewrite,
|
||||||
|
];
|
||||||
|
|
||||||
|
// 2. Add data for all attribute combinations
|
||||||
|
if ($product->hasAttributes()) {
|
||||||
|
// getAttributeCombinations provides detailed data for each combination
|
||||||
|
$combinations = $product->getAttributeCombinations($id_lang);
|
||||||
|
if ($combinations) {
|
||||||
|
foreach ($combinations as $combination) {
|
||||||
|
$all_product_data[] = [
|
||||||
|
'id_product' => (int)$product->id,
|
||||||
|
'id_product_attribute' => (int)$combination['id_product_attribute'],
|
||||||
|
'meta_title' => $product->meta_title, // Meta is usually product-level
|
||||||
|
'meta_description' => $product->meta_description,
|
||||||
|
// Title and descriptions are also from the main product
|
||||||
|
'title' => $product->name,
|
||||||
|
'description' => $product->description,
|
||||||
|
'description_short' => $product->description_short,
|
||||||
|
// These fields are specific to the combination
|
||||||
|
'mpn' => $combination['mpn'],
|
||||||
|
'reference' => $combination['reference'],
|
||||||
|
'ean13' => $combination['ean13'],
|
||||||
|
'upc' => $combination['upc'],
|
||||||
|
'id_language' => (int)$id_lang,
|
||||||
|
'id_shop' => (int)$id_shop,
|
||||||
|
'link' => $this->context->link->getProductLink($product, null, null, null, (int)$id_lang, (int)$id_shop, (int)$combination['id_product_attribute']),
|
||||||
|
'link_rewrite' => $product->link_rewrite,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output the final array as a JSON object
|
||||||
|
echo json_encode($all_product_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user