init
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user