This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,232 @@
<?php
namespace Stemcell\ProductImport\Model;
use Magento\Cms\Api\Data\PageInterfaceFactory;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Stemcell\ProductImport\Api\CmsPageImportInterface;
use Stemcell\ProductImport\Api\Data\ImportResultInterface;
class CmsPageImport implements CmsPageImportInterface
{
/**
* @var PageRepositoryInterface
*/
private $pageRepository;
/**
* @var PageInterfaceFactory
*/
private $pageFactory;
/**
* @var ImportResultInterface
*/
private $importResult;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var ResourceConnection
*/
private $resource;
/**
* @var PageCollectionFactory
*/
private $pageCollectionFactory;
/**
* CmsPageImport constructor.
*
* @param PageRepositoryInterface $pageRepository
* @param PageInterfaceFactory $pageFactory
* @param ImportResultInterface $importResult
* @param LoggerInterface $logger
* @param StoreManagerInterface $storeManager
* @param ResourceConnection $resource
* @param PageCollectionFactory $pageCollectionFactory
*/
public function __construct(
PageRepositoryInterface $pageRepository,
PageInterfaceFactory $pageFactory,
ImportResultInterface $importResult,
LoggerInterface $logger,
StoreManagerInterface $storeManager,
ResourceConnection $resource,
PageCollectionFactory $pageCollectionFactory
) {
$this->pageRepository = $pageRepository;
$this->pageFactory = $pageFactory;
$this->importResult = $importResult;
$this->logger = $logger;
$this->storeManager = $storeManager;
$this->resource = $resource;
$this->pageCollectionFactory = $pageCollectionFactory;
}
/**
* @inheritdoc
*/
public function importCmsPages($pageData)
{
try {
$successCount = 0;
$errorCount = 0;
$errors = [];
if (!is_array($pageData)) {
$this->logger->error('Invalid input: pageData is not an array, type is: ' . gettype($pageData));
throw new LocalizedException(__('Invalid input format.'));
}
try {
if (empty($pageData['identifier'])) {
$this->logger->error('Identifier is empty in page data');
throw new LocalizedException(__('Identifier is required.'));
}
$this->importPage($pageData);
$successCount++;
} catch (\Exception $e) {
$errorCount++;
$errors[] = $e->getMessage();
$this->logger->error('Error processing page: ' . $e->getMessage());
}
$this->importResult->setSuccessCount($successCount);
$this->importResult->setErrorCount($errorCount);
$this->importResult->setErrors($errors);
return $this->importResult;
} catch (\Throwable $e) {
$this->logger->error('General error in importCmsPages: ' . $e->getMessage());
$this->importResult->setSuccessCount(0);
$this->importResult->setErrorCount(1);
$this->importResult->setErrors([$e->getMessage()]);
return $this->importResult;
}
}
/**
* Import single CMS page
*
* @param array $pageData
* @return void
* @throws \Exception
*/
private function importPage($pageData)
{
try {
$identifier = $pageData['identifier'];
// Try to find existing page by identifier
$pageCollection = $this->pageCollectionFactory->create();
$pageCollection->addFieldToFilter('identifier', $identifier);
$page = $pageCollection->getFirstItem();
// If page exists and ID is less than 135, skip processing
if ($page->getId() && $page->getId() < 135) {
$this->logger->info('Skipping CMS page with ID ' . $page->getId() . ' as it is less than 135');
return;
}
if (!$page->getId()) {
$page = $this->pageFactory->create();
}
// Set basic page data
$page->setTitle($pageData['title'] ?? '');
$page->setMetaKeywords($pageData['meta_keywords'] ?? '');
$page->setMetaDescription($pageData['meta_description'] ?? '');
$page->setIdentifier($identifier);
$page->setContentHeading($pageData['content_heading'] ?? '');
$page->setContent($pageData['content'] ?? '');
$page->setMetaTitle($pageData['meta_title'] ?? '');
// Log the page data before saving
$this->logger->info('Attempting to save CMS page with data: ' . json_encode([
'identifier' => $identifier,
'title' => $pageData['title'] ?? '',
'content_heading' => $pageData['content_heading'] ?? '',
'meta_title' => $pageData['meta_title'] ?? ''
]));
// Save the page
$page = $this->pageRepository->save($page);
$this->logger->info('Successfully imported/updated CMS page with identifier: ' . $identifier);
// Set page layout directly in database
$connection = $this->resource->getConnection();
$tableName = $this->resource->getTableName('cms_page');
$connection->update(
$tableName,
['page_layout' => '1column'],
['page_id = ?' => $page->getId()]
);
$this->logger->info('Successfully set page layout for CMS page ID: ' . $page->getId());
// Associate page with all stores
$this->associatePageWithStores($page->getId());
} catch (\Exception $e) {
$this->logger->error('Error importing CMS page: ' . $e->getMessage());
throw $e;
}
}
/**
* Associate CMS page with all stores
*
* @param int $pageId
* @return void
*/
private function associatePageWithStores($pageId)
{
try {
$connection = $this->resource->getConnection();
$tableName = $this->resource->getTableName('cms_page_store');
// Get all store IDs
// $storeIds = array_keys($this->storeManager->getStores());
$storeIds = [0];
// Get existing store associations
$select = $connection->select()
->from($tableName, 'store_id')
->where('page_id = ?', $pageId);
$existingStores = $connection->fetchCol($select);
// Prepare data for insertion (only for stores that don't have association)
$data = [];
foreach ($storeIds as $storeId) {
if (!in_array($storeId, $existingStores)) {
$data[] = [
'page_id' => $pageId,
'store_id' => $storeId
];
}
}
// Insert only new store associations
if (!empty($data)) {
$connection->insertMultiple($tableName, $data);
$this->logger->info('Successfully associated CMS page ID ' . $pageId . ' with new stores: ' . implode(', ', array_column($data, 'store_id')));
} else {
$this->logger->info('CMS page ID ' . $pageId . ' already associated with all stores');
}
} catch (\Exception $e) {
$this->logger->error('Error associating CMS page with stores: ' . $e->getMessage());
throw $e;
}
}
}
@@ -0,0 +1,280 @@
<?php
namespace Stemcell\ProductImport\Model\Data;
use Stemcell\ProductImport\Api\Data\CmsPageDataInterface;
use Magento\Framework\Api\AbstractSimpleObject;
class CmsPageData extends AbstractSimpleObject implements CmsPageDataInterface
{
/**
* @inheritdoc
*/
public function getTitle()
{
return $this->_get('title');
}
/**
* @inheritdoc
*/
public function setTitle($title)
{
return $this->setData('title', $title);
}
/**
* @inheritdoc
*/
public function getPageLayout()
{
return $this->_get('page_layout');
}
/**
* @inheritdoc
*/
public function setPageLayout($pageLayout)
{
return $this->setData('page_layout', $pageLayout);
}
/**
* @inheritdoc
*/
public function getMetaKeywords()
{
return $this->_get('meta_keywords');
}
/**
* @inheritdoc
*/
public function setMetaKeywords($metaKeywords)
{
return $this->setData('meta_keywords', $metaKeywords);
}
/**
* @inheritdoc
*/
public function getMetaDescription()
{
return $this->_get('meta_description');
}
/**
* @inheritdoc
*/
public function setMetaDescription($metaDescription)
{
return $this->setData('meta_description', $metaDescription);
}
/**
* @inheritdoc
*/
public function getIdentifier()
{
return $this->_get('identifier');
}
/**
* @inheritdoc
*/
public function setIdentifier($identifier)
{
return $this->setData('identifier', $identifier);
}
/**
* @inheritdoc
*/
public function getContentHeading()
{
return $this->_get('content_heading');
}
/**
* @inheritdoc
*/
public function setContentHeading($contentHeading)
{
return $this->setData('content_heading', $contentHeading);
}
/**
* @inheritdoc
*/
public function getContent()
{
return $this->_get('content');
}
/**
* @inheritdoc
*/
public function setContent($content)
{
return $this->setData('content', $content);
}
/**
* @inheritdoc
*/
public function getIsActive()
{
return $this->_get('is_active');
}
/**
* @inheritdoc
*/
public function setIsActive($isActive)
{
return $this->setData('is_active', $isActive);
}
/**
* @inheritdoc
*/
public function getSortOrder()
{
return $this->_get('sort_order');
}
/**
* @inheritdoc
*/
public function setSortOrder($sortOrder)
{
return $this->setData('sort_order', $sortOrder);
}
/**
* @inheritdoc
*/
public function getLayoutUpdateXml()
{
return $this->_get('layout_update_xml');
}
/**
* @inheritdoc
*/
public function setLayoutUpdateXml($layoutUpdateXml)
{
return $this->setData('layout_update_xml', $layoutUpdateXml);
}
/**
* @inheritdoc
*/
public function getCustomTheme()
{
return $this->_get('custom_theme');
}
/**
* @inheritdoc
*/
public function setCustomTheme($customTheme)
{
return $this->setData('custom_theme', $customTheme);
}
/**
* @inheritdoc
*/
public function getCustomRootTemplate()
{
return $this->_get('custom_root_template');
}
/**
* @inheritdoc
*/
public function setCustomRootTemplate($customRootTemplate)
{
return $this->setData('custom_root_template', $customRootTemplate);
}
/**
* @inheritdoc
*/
public function getCustomLayoutUpdateXml()
{
return $this->_get('custom_layout_update_xml');
}
/**
* @inheritdoc
*/
public function setCustomLayoutUpdateXml($customLayoutUpdateXml)
{
return $this->setData('custom_layout_update_xml', $customLayoutUpdateXml);
}
/**
* @inheritdoc
*/
public function getLayoutUpdateSelected()
{
return $this->_get('layout_update_selected');
}
/**
* @inheritdoc
*/
public function setLayoutUpdateSelected($layoutUpdateSelected)
{
return $this->setData('layout_update_selected', $layoutUpdateSelected);
}
/**
* @inheritdoc
*/
public function getCustomThemeFrom()
{
return $this->_get('custom_theme_from');
}
/**
* @inheritdoc
*/
public function setCustomThemeFrom($customThemeFrom)
{
return $this->setData('custom_theme_from', $customThemeFrom);
}
/**
* @inheritdoc
*/
public function getCustomThemeTo()
{
return $this->_get('custom_theme_to');
}
/**
* @inheritdoc
*/
public function setCustomThemeTo($customThemeTo)
{
return $this->setData('custom_theme_to', $customThemeTo);
}
/**
* @inheritdoc
*/
public function getMetaTitle()
{
return $this->_get('meta_title');
}
/**
* @inheritdoc
*/
public function setMetaTitle($metaTitle)
{
return $this->setData('meta_title', $metaTitle);
}
}
@@ -0,0 +1,82 @@
<?php
namespace Stemcell\ProductImport\Model\Data;
use Stemcell\ProductImport\Api\Data\ImportResultInterface;
class ImportResult implements ImportResultInterface
{
/**
* @var int
*/
private $successCount = 0;
/**
* @var int
*/
private $errorCount = 0;
/**
* @var string[]
*/
private $errors = [];
/**
* @inheritdoc
*/
public function getSuccessCount()
{
return $this->successCount;
}
/**
* @inheritdoc
*/
public function setSuccessCount($count)
{
$this->successCount = $count;
return $this;
}
/**
* @inheritdoc
*/
public function getErrorCount()
{
return $this->errorCount;
}
/**
* @inheritdoc
*/
public function setErrorCount($count)
{
$this->errorCount = $count;
return $this;
}
/**
* @inheritdoc
*/
public function getErrors()
{
return $this->errors;
}
/**
* @inheritdoc
*/
public function setErrors(array $errors)
{
$this->errors = $errors;
return $this;
}
/**
* @inheritdoc
*/
public function addError($error)
{
$this->errors[] = $error;
return $this;
}
}
@@ -0,0 +1,232 @@
<?php
namespace Stemcell\ProductImport\Model\Data;
use Stemcell\ProductImport\Api\Data\ProductDataInterface;
use Magento\Framework\Api\AbstractSimpleObject;
class ProductData extends AbstractSimpleObject implements ProductDataInterface
{
/**
* @inheritdoc
*/
public function getSku()
{
return $this->_get('sku');
}
/**
* @inheritdoc
*/
public function setSku($sku)
{
return $this->setData('sku', $sku);
}
/**
* @inheritdoc
*/
public function getName()
{
return $this->_get('name');
}
/**
* @inheritdoc
*/
public function setName($name)
{
return $this->setData('name', $name);
}
/**
* @inheritdoc
*/
public function getDescription()
{
return $this->_get('description');
}
/**
* @inheritdoc
*/
public function setDescription($description)
{
return $this->setData('description', $description);
}
/**
* @inheritdoc
*/
public function getShortDescription()
{
return $this->_get('short_description');
}
/**
* @inheritdoc
*/
public function setShortDescription($shortDescription)
{
return $this->setData('short_description', $shortDescription);
}
/**
* @inheritdoc
*/
public function getPrice()
{
return $this->_get('price');
}
/**
* @inheritdoc
*/
public function setPrice($price)
{
return $this->setData('price', $price);
}
/**
* @inheritdoc
*/
public function getWeight()
{
return $this->_get('weight');
}
/**
* @inheritdoc
*/
public function setWeight($weight)
{
return $this->setData('weight', $weight);
}
/**
* @inheritdoc
*/
public function getTaxClassName()
{
return $this->_get('tax_class_name');
}
/**
* @inheritdoc
*/
public function setTaxClassName($taxClassName)
{
return $this->setData('tax_class_name', $taxClassName);
}
/**
* @inheritdoc
*/
public function getProductWebsites()
{
return $this->_get('product_websites');
}
/**
* @inheritdoc
*/
public function setProductWebsites($productWebsites)
{
return $this->setData('product_websites', $productWebsites);
}
/**
* @inheritdoc
*/
public function getIsInStock()
{
return $this->_get('is_in_stock');
}
/**
* @inheritdoc
*/
public function setIsInStock($isInStock)
{
return $this->setData('is_in_stock', $isInStock);
}
/**
* @inheritdoc
*/
public function getQty()
{
return $this->_get('qty');
}
/**
* @inheritdoc
*/
public function setQty($qty)
{
return $this->setData('qty', $qty);
}
/**
* @inheritdoc
*/
public function getManageStock()
{
return $this->_get('manage_stock');
}
/**
* @inheritdoc
*/
public function setManageStock($manageStock)
{
return $this->setData('manage_stock', $manageStock);
}
/**
* @inheritdoc
*/
public function getMinCartQty()
{
return $this->_get('min_cart_qty');
}
/**
* @inheritdoc
*/
public function setMinCartQty($minCartQty)
{
return $this->setData('min_cart_qty', $minCartQty);
}
/**
* @inheritdoc
*/
public function getMaxCartQty()
{
return $this->_get('max_cart_qty');
}
/**
* @inheritdoc
*/
public function setMaxCartQty($maxCartQty)
{
return $this->setData('max_cart_qty', $maxCartQty);
}
/**
* @inheritdoc
*/
public function getExtensionAttributes()
{
return $this->_get('extension_attributes');
}
/**
* @inheritdoc
*/
public function setExtensionAttributes(\Stemcell\ProductImport\Api\Data\ProductDataExtensionInterface $extensionAttributes)
{
return $this->setData('extension_attributes', $extensionAttributes);
}
}
@@ -0,0 +1,24 @@
<?php
namespace Stemcell\ProductImport\Model\Data;
use Stemcell\ProductImport\Api\Data\ProductDataExtensionInterface;
use Magento\Framework\Api\AbstractSimpleObject;
class ProductDataExtension extends AbstractSimpleObject implements ProductDataExtensionInterface
{
/**
* @inheritdoc
*/
public function getStockItem()
{
return $this->_get('stock_item');
}
/**
* @inheritdoc
*/
public function setStockItem($stockItem)
{
return $this->setData('stock_item', $stockItem);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,105 @@
<?php
namespace Stemcell\ProductImport\Model\Import;
class Result
{
/**
* @var int
*/
private $successCount = 0;
/**
* @var int
*/
private $errorCount = 0;
/**
* @var array
*/
private $errors = [];
/**
* Set success count
*
* @param int $count
* @return void
*/
public function setSuccessCount($count)
{
$this->successCount = $count;
}
/**
* Get success count
*
* @return int
*/
public function getSuccessCount()
{
return $this->successCount;
}
/**
* Set error count
*
* @param int $count
* @return void
*/
public function setErrorCount($count)
{
$this->errorCount = $count;
}
/**
* Get error count
*
* @return int
*/
public function getErrorCount()
{
return $this->errorCount;
}
/**
* Set errors
*
* @param array $errors
* @return void
*/
public function setErrors($errors)
{
$this->errors = $errors;
}
/**
* Get errors
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Add error
*
* @param string $error
* @return void
*/
public function addError($error)
{
$this->errors[] = $error;
$this->errorCount++;
}
/**
* Add success
*
* @return void
*/
public function addSuccess()
{
$this->successCount++;
}
}
@@ -0,0 +1,14 @@
<?php
namespace Stemcell\ProductImport\Model\Import;
enum Status:string
{
case PENDING = 'pending';
case PROCESSING = 'processing';
case SUCCESS = 'success';
case FAILED = 'failed';
}
@@ -0,0 +1,100 @@
<?php
namespace Stemcell\ProductImport\Model\Import;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ResourceConnection;;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
class TechnicalResourceFAQ
{
public function __construct(
protected ProductRepositoryInterface $productRepository,
protected Configurable $configurableResource,
protected ResourceConnection $resourceConnection
) {
}
public function execute(Product $virtualProduct){
$data = [];
if (str_starts_with($virtualProduct->getSku(), 'FAQ')) {
return;
}
if (is_numeric($virtualProduct->getSku())) {
if (str_starts_with($virtualProduct->getSku(), '100000')) {
$data['technical_resource_skus'] = $virtualProduct->getSku();
} else {
$data['publication_skus'] = $virtualProduct->getSku();
}
} else {
$data['technical_resource_skus'] = $virtualProduct->getSku();
}
$parentSkus = $this->getParentSkus(explode(",", $virtualProduct->getData('parent_skus')));
foreach ($parentSkus as $sku) {
try {
/* @var $product Product */
$product = $this->productRepository->get($sku, true);
foreach ($data as $attribute => $value) {
$values = [];
if ($product->getData($attribute)) {
$values = explode(",", $product->getData($attribute));
}
$values[] = $value;
$product->setData($attribute, implode(',', array_unique($values)));
}
$this->productRepository->save($product);
} catch (NoSuchEntityException | CouldNotSaveException | InputException | StateException $e) {
}
}
}
/**
* @return string[]
*/
public function getParentSkus(array $childSkus): array
{
$childSkus = array_values(array_unique(array_filter($childSkus)));
if (!$childSkus) {
return [];
}
$connection = $this->resourceConnection->getConnection();
$productTable = $this->resourceConnection->getTableName(
'catalog_product_entity'
);
$superLinkTable = $this->resourceConnection->getTableName(
'catalog_product_super_link'
);
$select = $connection->select()
->distinct()
->from(
['parent' => $productTable],
['sku']
)
->joinInner(
['link' => $superLinkTable],
'link.parent_id = parent.entity_id',
[]
)
->joinInner(
['child' => $productTable],
'child.entity_id = link.product_id',
[]
)
->where('child.sku IN (?)', $childSkus)
->order('parent.sku ASC');
return $connection->fetchCol($select);
}
}
@@ -0,0 +1,12 @@
<?php
namespace Stemcell\ProductImport\Model\Import;
enum TypeId: int
{
//导入产品库存
case STOCK = 1;
//导入产品价格
case PRICE = 2;
}
@@ -0,0 +1,13 @@
<?php
namespace Stemcell\ProductImport\Model;
use Magento\Framework\Model\AbstractModel;
class ImportTask extends AbstractModel
{
protected function _construct()
{
$this->_init(\Stemcell\ProductImport\Model\ResourceModel\ImportTask::class);
}
}
@@ -0,0 +1,13 @@
<?php
namespace Stemcell\ProductImport\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class ImportTask extends AbstractDb
{
protected function _construct()
{
$this->_init('stemcell_import_task', 'task_id');
}
}
@@ -0,0 +1,17 @@
<?php
namespace Stemcell\ProductImport\Model\ResourceModel\ImportTask;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Stemcell\ProductImport\Model\ImportTask;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(
ImportTask::class,
\Stemcell\ProductImport\Model\ResourceModel\ImportTask::class
);
}
}