Browse Source

Duplicate also product attributes and categories

Vova Tkach 5 years ago
parent
commit
8c40c76f11
1 changed files with 67 additions and 0 deletions
  1. 67 0
      modules/module_shop_act_duplicate.go

+ 67 - 0
modules/module_shop_act_duplicate.go

@@ -78,6 +78,73 @@ func (this *Modules) RegisterAction_ShopDuplicate() *Action {
 				return err
 			}
 
+			// Block new product row
+			if _, err := tx.Exec("SELECT id FROM shop_products WHERE id = ? FOR UPDATE;", lastID); err != nil {
+				return err
+			}
+
+			// Duplicate categories
+			cat_sqls := []string{}
+			if cat_rows, err := tx.Query(
+				`SELECT
+					product_id,
+					category_id
+				FROM
+					shop_cat_product_rel
+				WHERE
+					product_id = ?
+				;`,
+				utils.StrToInt(pf_id),
+			); err == nil {
+				defer cat_rows.Close()
+				for cat_rows.Next() {
+					var product_id int
+					var category_id int
+					if err := cat_rows.Scan(&product_id, &category_id); err == nil {
+						cat_sqls = append(cat_sqls, `
+							INSERT INTO shop_cat_product_rel SET
+								product_id = `+utils.Int64ToStr(lastID)+`,
+								category_id = `+utils.IntToStr(category_id)+`
+							;
+						`)
+					}
+				}
+			}
+			for _, sql_query := range cat_sqls {
+				tx.Exec(sql_query)
+			}
+
+			// Duplicate attributes
+			attributes_sqls := []string{}
+			if attributes_rows, err := tx.Query(
+				`SELECT
+					product_id,
+					filter_value_id
+				FROM
+					shop_filter_product_values
+				WHERE
+					product_id = ?
+				;`,
+				utils.StrToInt(pf_id),
+			); err == nil {
+				defer attributes_rows.Close()
+				for attributes_rows.Next() {
+					var product_id int
+					var filter_value_id int
+					if err := attributes_rows.Scan(&product_id, &filter_value_id); err == nil {
+						attributes_sqls = append(attributes_sqls, `
+							INSERT INTO shop_filter_product_values SET
+								product_id = `+utils.Int64ToStr(lastID)+`,
+								filter_value_id = `+utils.IntToStr(filter_value_id)+`
+							;
+						`)
+					}
+				}
+			}
+			for _, sql_query := range attributes_sqls {
+				tx.Exec(sql_query)
+			}
+
 			return nil
 		}); err != nil {
 			wrap.MsgError(err.Error())