Skip to content

Instantly share code, notes, and snippets.

@ilyautkin
Last active April 4, 2024 06:47
Show Gist options
  • Save ilyautkin/927f623dcb52cd76b615cd5ad943901e to your computer and use it in GitHub Desktop.
Save ilyautkin/927f623dcb52cd76b615cd5ad943901e to your computer and use it in GitHub Desktop.
<?php
define('MODX_API_MODE', true);
require 'index.php';
// Instantiate the Commerce class
$path = $modx->getOption('commerce.core_path', null, MODX_CORE_PATH . 'components/commerce/') . 'model/commerce/';
$params = ['mode' => $modx->getOption('commerce.mode')];
/** @var Commerce|null $commerce */
$commerce = $modx->getService('commerce', 'Commerce', $path, $params);
if (!($commerce instanceof Commerce)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load Commerce service in commerce.get_product snippet.');
return 'Could not load Commerce. Please try again later.';
}
$output = [];
$parents = $modx->getCollection('modResource', ['parent' => 98]);
foreach ($parents as $parent) {
$resources = $modx->getCollection('modResource', ['parent' => $parent->id]);
foreach ($resources as $resource) {
$row = array_merge($resource->toArray(), [
'stock' => ($resource->getTVValue('productStock') == 'instock' ? 100 : 0),
'image' => $resource->getTVValue('productImage')
]);
if ($product_id = $resource->getTVValue('product')) {
$product = $modx->getObject('comProduct', $product_id);
$row['price'] = $product->price;
$row['pricing'] = $product->pricing ?: json_encode(["EUR"=>["regular_price"=>$product->price,"price_types"=>[]]]);
$row['barcode'] = $product->barcode;
if ($product->stock) {
$row['stock'] = $product->stock;
}
}
if ($matrix_id = $resource->getTVValue('productMatrix')) {
if ($matrix = $modx->getObject('comProductMatrix', $matrix_id)) {
$row['matrix'] = $matrix->toArray();
$columns = $modx->getCollection('comProductMatrixColumn', ['matrix' => $matrix->id]);
$row['columns'] = [];
foreach ($columns as $column) {
$row['columns'][] = $column->toArray();
}
$matrixRows = $modx->getCollection('comProductMatrixRow', ['matrix' => $matrix->id]);
$row['rows'] = [];
foreach ($matrixRows as $matrixRow) {
$row['rows'][] = $matrixRow->toArray();
}
$cells = $modx->getCollection('comProductMatrixProduct', ['matrix' => $matrix->id]);
$row['cells'] = [];
foreach ($cells as $cell) {
$row['cells'][] = $cell->toArray();
}
}
}
$output[] = $row;
}
file_put_contents(__DIR__ . '/' . $parent->id . '.json', json_encode($output));
echo __DIR__ . '/' . $parent->id . '.json' . PHP_EOL;
}
<?php
define('MODX_API_MODE', true);
require 'index.php';
// Instantiate the Commerce class
$path = $modx->getOption('commerce.core_path', null, MODX_CORE_PATH . 'components/commerce/') . 'model/commerce/';
$params = ['mode' => $modx->getOption('commerce.mode')];
/** @var Commerce|null $commerce */
$commerce = $modx->getService('commerce', 'Commerce', $path, $params);
if (!($commerce instanceof Commerce)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load Commerce service in commerce.get_product snippet.');
return 'Could not load Commerce. Please try again later.';
}
$source = json_decode(file_get_contents(MODX_BASE_PATH . '115040.json'), true);
foreach ($source as $data) {
unset($data['id']);
unset($data['type']);
unset($data['class_key']);
$data['parent'] = 99128;
$data['template'] = 3;
$data['context_key'] = 'helfrichcompressiezorg';
$data['content'] = '<section id="section-1" data-animate class="block block--article cb-text block--light">
<div class="container">
<div class="article">
<div class="article__content">
<div class="richtext">
<h1>' . $data['pagetitle'] . '</h1>
' . $data['content'] . '
</div>
</div>
</div>
</div>
</section>';
if (!$resource = $modx->getObject(modResource::class, ['parent' => $data['parent'], 'pagetitle' => $data['pagetitle']])) {
$resource = $modx->newObject(modResource::class);
}
$resource->fromArray($data);
$resource->save();
$resource->setTVValue('overviewImage', json_encode(['image' => '/' . trim($data['image'], '/')]));
$resource->setTVValue('stock', $data['stock']);
$resource->setTVValue('price', $data['price']);
if (!empty($data['pricing'])) {
$resource->setTVValue('pricing', $data['pricing']);
}
if (!empty($data['matrix'])) {
unset($data['matrix']['id']);
unset($data['matrix']['tax_group']);
unset($data['matrix']['delivery_type']);
$data['matrix']['principal_id'] = $resource->id;
$data['matrix']['principal_field'] = 'product_matrix';
if (!$matrix = $modx->getObject('comProductMatrix', ['principal_id' => $data['matrix']['principal_id']])) {
$matrix = $modx->newObject('comProductMatrix');
}
$matrix->fromArray($data['matrix']);
$matrix->save();
if (!empty($data['columns'])) {
$cols_ids = [];
$modx->removeCollection('comProductMatrixColumn', ['matrix' => $matrix->id]);
foreach ($data['columns'] as $column) {
$column['old_id'] = $column['id'];
unset($column['id']);
$column['matrix'] = $matrix->id;
$comColumn = $modx->newObject('comProductMatrixColumn');
$comColumn->fromArray($column);
$comColumn->save();
$cols_ids[$column['old_id']] = $comColumn->id;
}
}
if (!empty($data['rows'])) {
$rows_ids = [];
$modx->removeCollection('comProductMatrixRow', ['matrix' => $matrix->id]);
foreach ($data['rows'] as $row) {
$row['old_id'] = $row['id'];
unset($row['id']);
$row['matrix'] = $matrix->id;
$comRow = $modx->newObject('comProductMatrixRow');
$comRow->fromArray($row);
$comRow->save();
$rows_ids[$row['old_id']] = $comRow->id;
}
}
if (!empty($data['cells'])) {
$modx->removeCollection('comProductMatrixProduct', ['matrix' => $matrix->id]);
foreach ($data['cells'] as $cell) {
unset($row['id']);
$cell['matrix'] = $matrix->id;
$cell['column'] = $cols_ids[$cell['column']];
$cell['row'] = $rows_ids[$cell['row']];
$cell['description'] = $cell['stock'];
$comCell = $modx->newObject('comProductMatrixProduct');
$comCell->fromArray($cell);
$comCell->save();
}
}
}
$resource->setTVValue('product_matrix', $matrix->id);
echo $resource->pagetitle . ' (' . $resource->id . ')' . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment