40 lines
1.3 KiB
PHP
40 lines
1.3 KiB
PHP
<?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();
|
|
}
|
|
}
|
|
}
|