public function afterSave(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject, $attribute) { $oldStores = $this->advSearchStores->getStoreIds($attribute->getId()); $newStores = (array) $attribute->getAdvancedSearchStoreIds(); // If no stores are selected or all stores and stores are selected set only to all stores value if (empty($newStores) || count($newStores) > 1 && in_array(0, $newStores)) { $newStores = [0]; } $insert = array_diff($newStores, $oldStores); $delete = array_diff($oldStores, $newStores); if ($delete) { $this->advSearchStores->deleteStoreIds($attribute->getId(), $delete); } if ($insert) { $this->advSearchStores->saveStoreIds($attribute->getId(), $insert); } }