fix colorvariant
This commit is contained in:
parent
d40cb4d8f8
commit
5a211c501f
2
main.go
2
main.go
|
|
@ -111,7 +111,7 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) {
|
|||
}
|
||||
|
||||
// Update the product in the database
|
||||
if err := importer.UpdateOrCreate(jsonProduct).Error; err != nil {
|
||||
if err := importer.UpdateOrCreate(jsonProduct, product.ProductID).Error; err != nil {
|
||||
log.Println("Error updateing product information:", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
|
|
@ -188,38 +187,9 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
|
|||
tx := importer.Baza.Begin()
|
||||
|
||||
if err := tx.Omit("Categories.*", "SuperAttributes.*", "ParentID").Create(&mainPorduct).Error; err != nil {
|
||||
//todo update categories
|
||||
byteErr, _ := json.Marshal(err)
|
||||
var newError GormErr
|
||||
|
||||
if err1 := json.Unmarshal((byteErr), &newError); err1 != nil {
|
||||
tx.Rollback()
|
||||
log.Println(err1, "err2")
|
||||
return nil, err1
|
||||
}
|
||||
|
||||
if newError.Number == 1062 {
|
||||
var barProduct gm.Product
|
||||
if err2 := tx.First(&barProduct, "sku = ?", mainPorduct.Sku).Error; err2 != nil {
|
||||
tx.Rollback()
|
||||
log.Println(err2, "err3")
|
||||
return nil, err2
|
||||
}
|
||||
|
||||
if err3 := tx.Model(&barProduct).Association("Categories").Append(mainPorduct.Categories); err3 != nil {
|
||||
tx.Rollback()
|
||||
log.Println(err3, "err4")
|
||||
return nil, err3
|
||||
}
|
||||
|
||||
if err4 := tx.Commit().Error; err4 == nil {
|
||||
return importer.updateVariant(product)
|
||||
}
|
||||
} else {
|
||||
tx.Rollback()
|
||||
log.Println(err, "er1")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -382,10 +352,10 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
|
|||
importer.SexMutex.Unlock()
|
||||
return option
|
||||
}
|
||||
func (importer *Importer) disableVariant(product models.Product) (instance *Importer) {
|
||||
func (importer *Importer) disableVariant(product models.Product, product_id uint) (instance *Importer) {
|
||||
var flat gm.ProductFlat
|
||||
importer.Error = importer.Baza.Preload("Product").Preload("Variants").
|
||||
First(&flat, "sku = ?", product.ProductNumber).Error
|
||||
First(&flat, "product_id = ?", product_id).Error
|
||||
|
||||
if importer.Error != nil {
|
||||
return importer
|
||||
|
|
@ -401,10 +371,10 @@ func (importer *Importer) disableVariant(product models.Product) (instance *Impo
|
|||
|
||||
}
|
||||
|
||||
func (importer *Importer) updateVariant(product models.Product) (*gm.Product, error) {
|
||||
func (importer *Importer) updateVariant(product models.Product, product_id uint) (*gm.Product, error) {
|
||||
|
||||
var flat gm.ProductFlat
|
||||
err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "sku = ?", product.ProductNumber).Error
|
||||
err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "product_id = ?", product_id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return importer.importVariant(product)
|
||||
|
|
@ -790,13 +760,13 @@ func (importer *Importer) UpdateOrCreateLCW(product models.Product) (instance *I
|
|||
return importer
|
||||
}
|
||||
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint) (instance *Importer) {
|
||||
|
||||
if !product.IsSellable {
|
||||
return importer.disableVariant(product)
|
||||
return importer.disableVariant(product, product_id)
|
||||
}
|
||||
|
||||
firstProduct, err := importer.updateVariant(product)
|
||||
firstProduct, err := importer.updateVariant(product, product_id)
|
||||
var newProducts []gm.Product
|
||||
if err != nil {
|
||||
importer.Error = err
|
||||
|
|
@ -812,30 +782,26 @@ func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Impo
|
|||
|
||||
var (
|
||||
variant *gm.Product
|
||||
variantFlat *gm.ProductFlat
|
||||
err error
|
||||
)
|
||||
if !colorVariant.IsSellable {
|
||||
|
||||
if err = importer.Baza.Model(&gm.ProductFlat{}).Where("sku=?", colorVariant.ProductNumber).Update("status", false).Error; err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
err = importer.Baza.Preload("Product").Preload("Variants").First(&variantFlat, "sku = ?", colorVariant.ProductNumber).Error
|
||||
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) && colorVariant.IsSellable {
|
||||
variant, err = importer.importVariant(colorVariant)
|
||||
newProducts = append(newProducts, *variant)
|
||||
} else if err == nil && !colorVariant.IsSellable {
|
||||
importer.disableVariant(colorVariant, variantFlat.ProductID)
|
||||
continue
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if variant, err = importer.importVariant(colorVariant); err != nil {
|
||||
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant); importer.Error != nil {
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant, variantFlat.ProductID); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
|
||||
} else {
|
||||
newProducts = append(newProducts, *variant)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if len(newProducts) > 0 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue