init
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Observer;
|
||||
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
|
||||
class CategoryControllerAccess implements \Magento\Framework\Event\ObserverInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected CustomerSession $customerSession,
|
||||
protected \Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
protected \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
/* @var $category Category */
|
||||
$category = $observer->getEvent()->getData('category');
|
||||
$samplePackCategoryId = $this->scopeConfig->getValue(
|
||||
'catalog/product_settings/sample_pack_category_id',
|
||||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
/* @var $action \Magento\Catalog\Controller\Category\View */
|
||||
$action = $observer->getEvent()->getData('controller_action');
|
||||
|
||||
if ($category->getEntityId()==$samplePackCategoryId && !$this->customerSession->isLoggedIn()) {
|
||||
$action->getResponse()->setRedirect($this->storeManager->getStore()->getBaseUrl());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Observer;
|
||||
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
/**
|
||||
* 解决下拉多选属性值没被保存的问题
|
||||
*/
|
||||
class MultiselectAttributeSave implements ObserverInterface
|
||||
{
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
/* @var $controller \Magento\Catalog\Controller\Adminhtml\Product\Save */
|
||||
$controller = $observer->getEvent()->getData('controller');
|
||||
$data = $controller->getRequest()->getParam('product');
|
||||
|
||||
/* @var $product \Magento\Catalog\Model\Product */
|
||||
$product = $observer->getEvent()->getData('product');
|
||||
|
||||
$hasDiff = false;
|
||||
foreach ($product->getAttributes() as $attribute) {
|
||||
if ($attribute->getFrontendInput() === 'multiselect' && isset($data[$attribute->getAttributeCode()])) {
|
||||
|
||||
$requestData = is_string($data[$attribute->getAttributeCode()])?
|
||||
$data[$attribute->getAttributeCode()]:implode(',', $data[$attribute->getAttributeCode()]);
|
||||
|
||||
if (!empty($requestData)) {
|
||||
$product->setData($attribute->getAttributeCode(), $requestData);
|
||||
$hasDiff = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($hasDiff) {
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Observer;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
|
||||
/**
|
||||
* 产品新增、修改时, 如果parent_sku有改动,需要将虚拟产品的sku(search_sku)、关联词虚拟产品的产品名称(name)和产品sku,
|
||||
* 同步到product_manual_search表
|
||||
*
|
||||
* 这个event可以和product:manual:search:import命令配合使用
|
||||
*/
|
||||
class ProductManualSearchSync implements ObserverInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ProductResource\CollectionFactory $productCollectionFactory,
|
||||
protected ProductResource $productResource,
|
||||
) {
|
||||
}
|
||||
|
||||
protected function getProductFromSkus(array $skus){
|
||||
return $this->productCollectionFactory->create()->addAttributeToSelect('name', true)->addCategoryIds()
|
||||
->addAttributeToFilter('sku', ['in' => $skus]);
|
||||
}
|
||||
|
||||
protected function addNewRecord(array $record)
|
||||
{
|
||||
if (isset($record['name']) && isset($record['sku']) && isset($record['search_sku'])) {
|
||||
$sql = "INSERT INTO product_manual_search (name, sku, search_sku) select \"".$record['name']."\",'".$record['sku']
|
||||
."','".$record['search_sku'].'\' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM product_manual_search WHERE sku="'.$record['sku'].'" and search_sku="'.$record['search_sku'].'")';
|
||||
|
||||
$this->productResource->getConnection()->query($sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function createProductManualSearch(string $skus, Product $product)
|
||||
{
|
||||
foreach ($this->getProductFromSkus(explode(',', $skus)) as $item) {
|
||||
/* @var $item Product */
|
||||
$data = ['name' => $item->getName(), 'sku' => $item->getSku(), 'search_sku' => $product->getSku()];
|
||||
$this->addNewRecord($data);
|
||||
}
|
||||
}
|
||||
|
||||
protected function removeProductManualSearch(array $skus, Product $product)
|
||||
{
|
||||
foreach ($skus as $sku) {
|
||||
$sql = 'delete from product_manual_search WHERE sku="'.$sku.'" and search_sku="'.$product->getSku().'"';
|
||||
|
||||
$this->productResource->getConnection()->query($sql);
|
||||
}
|
||||
}
|
||||
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
|
||||
$entityType = $observer->getData('entity_type');
|
||||
/* @var $entity \Magento\Catalog\Model\Product */
|
||||
$entity = $observer->getData('entity');
|
||||
|
||||
if ($entityType == 'Magento\Catalog\Api\Data\ProductInterface') {
|
||||
|
||||
if ($entity->isObjectNew()) {
|
||||
$parentSkus = $entity->getData('parent_skus');
|
||||
if ($parentSkus) {
|
||||
$this->createProductManualSearch($parentSkus, $entity);
|
||||
}
|
||||
} else {
|
||||
$origin = trim((string)$entity->getOrigData('parent_skus'));
|
||||
$new = trim((string)$entity->getData('parent_skus'));
|
||||
if ($origin != $new) {
|
||||
$origin = explode(',', $origin);
|
||||
$new = explode(',', $new);
|
||||
$newItems = array_diff($new, $origin);
|
||||
|
||||
if ($newItems) {
|
||||
$this->createProductManualSearch(implode(',', $newItems), $entity);
|
||||
}
|
||||
|
||||
$removed = array_diff($origin, $new);
|
||||
if ($removed) {
|
||||
$this->removeProductManualSearch($removed, $entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Observer;
|
||||
|
||||
use Magento\Catalog\Model\Layer\Resolver;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\Collection;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use \Magento\Framework\App\RequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Stemcell\Catalog\Model\ProductManualSearch;
|
||||
|
||||
/**
|
||||
* this observer is used to search products in a product list with keyword key word
|
||||
*/
|
||||
class ProductSheetSearch implements ObserverInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Resolver $layerResolver,
|
||||
private RequestInterface $request,
|
||||
private ProductManualSearch $productManualSearch,
|
||||
protected Collection $productCollection,
|
||||
protected \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
$samplePackCategoryId = $this->scopeConfig->getValue(
|
||||
'catalog/product_settings/sample_pack_category_id',
|
||||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
|
||||
/* @var $collection \WeltPixel\LayeredNavigation\Model\ResourceModel\Fulltext\Collection */
|
||||
$collection = $observer->getEvent()->getData('collection');
|
||||
$categoryLayer = $this->layerResolver->get();
|
||||
$searchWord = $this->request->getParam('q');
|
||||
$category = $categoryLayer->getCurrentCategory();
|
||||
//产品手册、产品说明书分类页面内的搜索
|
||||
|
||||
|
||||
if(($keyword = $this->request->getParam('keyword')) && $category) {
|
||||
$keyword = trim($keyword);
|
||||
if ($category->getEntityId()==$samplePackCategoryId) {
|
||||
$collection->addAttributeToFilter([
|
||||
['attribute' => 'sku', 'eq' => $keyword],
|
||||
['attribute' => 'name', 'like' => '%' . $keyword . '%']
|
||||
]);
|
||||
} else {
|
||||
$collection->addAttributeToFilter([
|
||||
['attribute' => 'parent_skus', 'like' => '%' . $keyword . '%'],
|
||||
['attribute' => 'parent_names', 'like' => '%' . $keyword . '%'],
|
||||
], null, 'left');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//通过search_tags(小货号)搜索产品时,需要过滤掉虚拟产品
|
||||
// else if ($searchWord) {
|
||||
// // @todo 这只是临时方案。正确的做法应该需要改open search的查询,这个需要时间去研究
|
||||
// $productCollection = $this->productCollection->addAttributeToFilter([
|
||||
// ['attribute' => 'search_tags', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'search_words', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'search_terms', 'like' => '%' . $searchWord . '%']
|
||||
// ]);
|
||||
// if ($productCollection->getSize()) {
|
||||
// $collection->addAttributeToFilter([
|
||||
// ['attribute' => 'name', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'search_tags', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'search_words', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'search_terms', 'like' => '%' . $searchWord . '%'],
|
||||
// ['attribute' => 'sku', 'eq' => $searchWord],
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Observer;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* this observer is used to sync data to product_manual_search table when product parent_skus filed data updated
|
||||
*/
|
||||
class ProductSheetSearchSync implements ObserverInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ProductResource $productResource,
|
||||
private ProductResource\Collection $productCollection,
|
||||
private LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $skus
|
||||
* @return ProductResource\Collection
|
||||
*/
|
||||
protected function getProductFromSkus(array $skus){
|
||||
return $this->productCollection->addAttributeToSelect('name', true)
|
||||
->addAttributeToFilter('sku', ['in' => $skus]);
|
||||
}
|
||||
|
||||
protected function addNewRecord(array $record)
|
||||
{
|
||||
if (isset($record['name']) && isset($record['sku']) && isset($record['search_sku'])) {
|
||||
$sql = "INSERT INTO product_manual_search (name, sku, search_sku) select \"".$record['name']."\",'".$record['sku']
|
||||
."','".$record['search_sku'].'\' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM product_manual_search WHERE sku="'.$record['sku'].'" and search_sku="'.$record['search_sku'].'")';
|
||||
|
||||
$this->productResource->getConnection()->query($sql);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function removeProductSheetSearchRecord(string $searchSku){
|
||||
if ($searchSku) {
|
||||
$sql = "delete from product_manual_search where search_sku=\"".$searchSku."\"";
|
||||
|
||||
$this->productResource->getConnection()->query($sql);
|
||||
}
|
||||
}
|
||||
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
/* @var $product Product */
|
||||
$product = $observer->getEvent()->getData('product');
|
||||
$parentSkus = trim((string)$product->getData('parent_skus'));
|
||||
$origParentSkus = $product->getOrigData('parent_skus');
|
||||
// $this->logger->critical('original parent skus: '.var_export($origParentSkus, true));
|
||||
// $this->logger->critical('parent skus: '.var_export($parentSkus, true));
|
||||
if ($origParentSkus && !$parentSkus) {
|
||||
$this->removeProductSheetSearchRecord($product->getSku());
|
||||
} elseif ($origParentSkus != $parentSkus) {
|
||||
if ($origParentSkus) {
|
||||
$this->removeProductSheetSearchRecord($product->getSku());
|
||||
}
|
||||
foreach ($this->getProductFromSkus(explode(',', $parentSkus)) as $item) {
|
||||
/* @var $item Product */
|
||||
$data = ['name' => $item->getName(), 'sku' => $item->getSku(), 'search_sku' => $product->getSku()];
|
||||
$this->addNewRecord($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user