@@ -81,31 +81,39 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
81
81
{
82
82
parent ::_afterSave ($ object );
83
83
84
- $ condition = [
84
+ $ conditions = [
85
85
'option_id = ? ' => $ object ->getId (),
86
86
'store_id = ? OR store_id = 0 ' => $ object ->getStoreId (),
87
87
'parent_product_id = ? ' => $ object ->getParentId ()
88
88
];
89
89
90
90
$ connection = $ this ->getConnection ();
91
- $ connection ->delete ($ this ->getTable ('catalog_product_bundle_option_value ' ), $ condition );
92
91
93
- $ data = new \Magento \Framework \DataObject ();
94
- $ data ->setOptionId ($ object ->getId ())
95
- ->setStoreId ($ object ->getStoreId ())
96
- ->setParentProductId ($ object ->getParentId ())
97
- ->setTitle ($ object ->getTitle ());
98
-
99
- $ connection ->insert ($ this ->getTable ('catalog_product_bundle_option_value ' ), $ data ->getData ());
100
-
101
- /**
102
- * also saving default value if this store view scope
103
- */
92
+ if ($ this ->isOptionPresent ($ conditions )) {
93
+ $ connection ->update (
94
+ $ this ->getTable ('catalog_product_bundle_option_value ' ),
95
+ [
96
+ 'title ' => $ object ->getTitle ()
97
+ ],
98
+ $ conditions
99
+ );
100
+ } else {
101
+ $ data = new \Magento \Framework \DataObject ();
102
+ $ data ->setOptionId ($ object ->getId ())
103
+ ->setStoreId ($ object ->getStoreId ())
104
+ ->setParentProductId ($ object ->getParentId ())
105
+ ->setTitle ($ object ->getTitle ());
104
106
105
- if ($ object ->getStoreId ()) {
106
- $ data ->setStoreId (0 );
107
- $ data ->setTitle ($ object ->getDefaultTitle ());
108
107
$ connection ->insert ($ this ->getTable ('catalog_product_bundle_option_value ' ), $ data ->getData ());
108
+
109
+ /**
110
+ * also saving default value if this store view scope
111
+ */
112
+ if ($ object ->getStoreId ()) {
113
+ $ data ->setStoreId (0 );
114
+ $ data ->setTitle ($ object ->getDefaultTitle ());
115
+ $ connection ->insert ($ this ->getTable ('catalog_product_bundle_option_value ' ), $ data ->getData ());
116
+ }
109
117
}
110
118
111
119
return $ this ;
@@ -210,4 +218,26 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
210
218
211
219
return $ this ;
212
220
}
221
+
222
+ /**
223
+ * Is Bundle option present in the database
224
+ *
225
+ * @param array $conditions
226
+ *
227
+ * @return bool
228
+ */
229
+ private function isOptionPresent ($ conditions )
230
+ {
231
+ $ connection = $ this ->getConnection ();
232
+
233
+ $ select = $ connection ->select ()->from ($ this ->getTable ('catalog_product_bundle_option_value ' ));
234
+ foreach ($ conditions as $ condition => $ conditionValue ) {
235
+ $ select ->where ($ condition , $ conditionValue );
236
+ }
237
+ $ select ->limit (1 );
238
+
239
+ $ rowSelect = $ connection ->fetchRow ($ select );
240
+
241
+ return (is_array ($ rowSelect ) && !empty ($ rowSelect ));
242
+ }
213
243
}
0 commit comments