Last active
November 11, 2022 05:47
-
-
Save ilyautkin/f3d302cbcd71bd3de475db79184e10d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$resource_id = 39; | |
$priceList = [ | |
'1000' => ['name' => '1 liter', 'price' => 2250], | |
'2500' => ['name' => '2,5 liter', 'price' => 5023], | |
]; | |
$resource = $modx->getObject('modResource', $resource_id); | |
// 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)) { | |
return '<p class="error">Oops! It is not possible to view your cart currently. We\'re sorry for the inconvenience. Please try again later.</p>'; | |
} | |
// Create matrix | |
if (!$matrix = $this->modx->getObject('comProductMatrix', ['principal_id' => $resource_id])) { | |
$matrix = $this->modx->newObject('comProductMatrix'); | |
} | |
$matrix->fromArray([ | |
'principal_id' => $resource_id, | |
'principal_field' => 'product_matrix', | |
'column_name' => 'Volume', | |
'row_name' => 'Color', | |
'sku_template' => '{{ resource.alias }}-{{ row.sku }}', | |
'name_template' => '{{ resource.pagetitle }} {{ row.name }}' | |
]); | |
$matrix->save(); | |
$resource->setTVValue('product_matrix', $matrix->id); | |
// Create column | |
$columnSKU = $resource->alias; | |
if (!$column = $this->modx->getObject('comProductMatrixColumn', ['matrix' => $matrix->id, 'sku' => $columnSKU])) { | |
$column = $this->modx->newObject('comProductMatrixColumn'); | |
} | |
$column->fromArray([ | |
'matrix' => $matrix->id, | |
'sku' => $columnSKU, | |
'name' => $resource->pagetitle | |
]); | |
$column->save(); | |
// Create rows | |
foreach ($priceList as $rowSku => $product) { | |
if (!$row = $this->modx->getObject('comProductMatrixRow', ['matrix' => $matrix->id, 'sku' => $rowSku])) { | |
$row = $this->modx->newObject('comProductMatrixRow'); | |
} | |
$row->fromArray([ | |
'matrix' => $matrix->id, | |
'sku' => $rowSku, | |
'name' => $product['name'], | |
'pricing' => json_encode(['EUR' => ['regular_price' => $product['price'], 'price_types' => []]]) | |
]); | |
$row->save(); | |
// Create cells | |
if (!$cell = $this->modx->getObject('comProductMatrixProduct', ['matrix' => $matrix->id, 'sku' => $rowSku])) { | |
$cell = $this->modx->newObject('comProductMatrixProduct'); | |
} | |
$cell->fromArray([ | |
'sku' => $resource->alias . '-' . $rowSku, | |
'name' => $resource->pagetitle . ' ' . $product['name'], | |
'price' => $product['price'], | |
'pricing' => json_encode(['EUR' => ['regular_price' => $product['price'], 'price_types' => []]]), | |
'stock' => 0, | |
'stock_infinite' => true, | |
'matrix' => $matrix->id, | |
'column' => $column->id, | |
'row' => $row->id, | |
]); | |
$cell->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment