Browse Source

Update product custom fields only if field is present in form, don't delete existed values in database

Vova Tkach 5 years ago
parent
commit
eb46a5b235
1 changed files with 31 additions and 5 deletions
  1. 31 5
      modules/module_shop_act_modify.go

+ 31 - 5
modules/module_shop_act_modify.go

@@ -236,9 +236,7 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 						category = ?,
 						category = ?,
 						briefly = ?,
 						briefly = ?,
 						content = ?,
 						content = ?,
-						active = ?,
-						custom1 = ?,
-						custom2 = ?
+						active = ?
 					WHERE
 					WHERE
 						id = ?
 						id = ?
 					;`,
 					;`,
@@ -254,13 +252,41 @@ func (this *Modules) RegisterAction_ShopModify() *Action {
 					pf_briefly,
 					pf_briefly,
 					pf_content,
 					pf_content,
 					utils.StrToInt(pf_active),
 					utils.StrToInt(pf_active),
-					pf_custom1,
-					pf_custom2,
 					utils.StrToInt(pf_id),
 					utils.StrToInt(pf_id),
 				); err != nil {
 				); err != nil {
 					return err
 					return err
 				}
 				}
 
 
+				// Update custom field 1
+				if _, ok := wrap.R.Form["custom1"]; ok {
+					if _, err := tx.Exec(
+						`UPDATE shop_products SET
+							custom1 = ?
+						WHERE
+							id = ?
+						;`,
+						pf_custom1,
+						utils.StrToInt(pf_id),
+					); err != nil {
+						return err
+					}
+				}
+
+				// Update custom field 2
+				if _, ok := wrap.R.Form["custom2"]; ok {
+					if _, err := tx.Exec(
+						`UPDATE shop_products SET
+							custom2 = ?
+						WHERE
+							id = ?
+						;`,
+						pf_custom2,
+						utils.StrToInt(pf_id),
+					); err != nil {
+						return err
+					}
+				}
+
 				// Delete product and categories relations
 				// Delete product and categories relations
 				if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
 				if _, err := tx.Exec("DELETE FROM shop_cat_product_rel WHERE product_id = ?;", utils.StrToInt(pf_id)); err != nil {
 					return err
 					return err