@@ -81,31 +81,39 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
8181 {
8282 parent ::_afterSave ($ object );
8383
84- $ condition = [
84+ $ conditions = [
8585 'option_id = ? ' => $ object ->getId (),
8686 'store_id = ? OR store_id = 0 ' => $ object ->getStoreId (),
8787 'parent_product_id = ? ' => $ object ->getParentId ()
8888 ];
8989
9090 $ connection = $ this ->getConnection ();
91- $ connection ->delete ($ this ->getTable ('catalog_product_bundle_option_value ' ), $ condition );
9291
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 ());
104106
105- if ($ object ->getStoreId ()) {
106- $ data ->setStoreId (0 );
107- $ data ->setTitle ($ object ->getDefaultTitle ());
108107 $ 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+ }
109117 }
110118
111119 return $ this ;
@@ -210,4 +218,26 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
210218
211219 return $ this ;
212220 }
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+ }
213243}
0 commit comments