ready link import
This commit is contained in:
parent
d4ff2a48b3
commit
7482e6181a
|
|
@ -360,6 +360,10 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
|
||||||
func calcPrice(variants []gm.ProductFlat, flat *gm.ProductFlat) {
|
func calcPrice(variants []gm.ProductFlat, flat *gm.ProductFlat) {
|
||||||
for _, variant := range variants {
|
for _, variant := range variants {
|
||||||
|
|
||||||
|
if !variant.Status {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if flat.MinPrice == 0 || flat.MinPrice > variant.MinPrice {
|
if flat.MinPrice == 0 || flat.MinPrice > variant.MinPrice {
|
||||||
flat.MinPrice = variant.MinPrice
|
flat.MinPrice = variant.MinPrice
|
||||||
}
|
}
|
||||||
|
|
@ -429,49 +433,107 @@ func (importer *Importer) updateVariant(product models.Product) (*gm.Product, er
|
||||||
}
|
}
|
||||||
|
|
||||||
if flat.Product.Type == "configurable" {
|
if flat.Product.Type == "configurable" {
|
||||||
//todo update variant prices
|
|
||||||
//todo create none existing variant
|
|
||||||
//todo update min max price
|
|
||||||
} else {
|
|
||||||
|
|
||||||
price := product.Price
|
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
||||||
|
|
||||||
if price.OriginalPrice.Value > price.DiscountedPrice.Value {
|
for _, variant := range *product.SizeVariants {
|
||||||
|
|
||||||
importer.baza.Model(&flat).Updates(map[string]interface{}{
|
found := false
|
||||||
"price": price.OriginalPrice.Value,
|
for _, flatVariant := range flat.Variants {
|
||||||
"special_price": price.DiscountedPrice.Value,
|
|
||||||
"min_price": price.DiscountedPrice.Value,
|
|
||||||
"max_price": price.OriginalPrice.Value,
|
|
||||||
})
|
|
||||||
|
|
||||||
importer.baza.Model(&gm.ProductAttributeValue{}).
|
if variant.AttributeValue == flatVariant.BoyutLabel || variant.AttributeValue == flatVariant.SizeLabel {
|
||||||
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
|
||||||
Update("float_value", price.OriginalPrice.Value)
|
|
||||||
|
|
||||||
importer.baza.Model(&gm.ProductAttributeValue{}).
|
if !variant.Sellable {
|
||||||
Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
importer.baza.Model(&flatVariant).Update("status", false)
|
||||||
Update("float_value", price.DiscountedPrice.Value)
|
} else {
|
||||||
} else {
|
|
||||||
|
|
||||||
importer.baza.Model(&flat).Updates(map[string]interface{}{
|
importer.updatePrice(variant.Price, &flatVariant)
|
||||||
"price": price.DiscountedPrice.Value,
|
|
||||||
"special_price": nil,
|
|
||||||
"min_price": price.DiscountedPrice.Value,
|
|
||||||
"max_price": price.DiscountedPrice.Value,
|
|
||||||
})
|
|
||||||
|
|
||||||
importer.baza.Model(&gm.ProductAttributeValue{}).
|
if !flatVariant.Status {
|
||||||
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
|
||||||
Update("float_value", price.DiscountedPrice.Value)
|
|
||||||
|
|
||||||
importer.baza.Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
importer.baza.Model(&flatVariant).Update("status", true)
|
||||||
Delete(&gm.ProductAttributeValue{})
|
}
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if variant.Sellable && !found {
|
||||||
|
// insert variant
|
||||||
|
var sizeOPtion gm.AttributeOption
|
||||||
|
|
||||||
|
if variant.AttributeName == "Beden" {
|
||||||
|
sizeOPtion = gm.GetAttributeOption(importer.baza, importer.AttributesMap["size"].ID, variant.AttributeValue)
|
||||||
|
} else {
|
||||||
|
sizeOPtion = gm.GetAttributeOption(importer.baza, importer.AttributesMap["boyut"].ID, variant.AttributeValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
sku := fmt.Sprintf("%s-%d", product.ProductNumber, variant.ItemNumber)
|
||||||
|
variantProduct := productRepo.makeVariant(flat.ProductID, flat.Product.AttributeFamilyID, sku)
|
||||||
|
|
||||||
|
variantProduct.AttributeValues = productRepo.getVariantAttributes(importer.AttributesMap, &variant, sizeOPtion.ID)
|
||||||
|
|
||||||
|
if err := importer.baza.Omit("Categories.*").Create(&variantProduct).Error; err != nil {
|
||||||
|
log.Println("Variant Product Create Error: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
variantFlat := productRepo.makeVariantFlat(variant, sizeOPtion.ID, flat.ID, variantProduct.ID)
|
||||||
|
|
||||||
|
if err := importer.baza.Create(&variantFlat).Error; err != nil {
|
||||||
|
log.Println("Variant Flat Create Error: " + err.Error())
|
||||||
|
|
||||||
|
}
|
||||||
|
flat.Variants = append(flat.Variants, variantFlat)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
calcPrice(flat.Variants, &flat)
|
||||||
|
importer.baza.Omit("ParentID", "CreatedAt", "Variants", "SpecialPrice").Save(&flat)
|
||||||
|
|
||||||
|
} else { //simple
|
||||||
|
|
||||||
|
importer.updatePrice(product.Price, &flat)
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (importer *Importer) updatePrice(price models.Price, flat *gm.ProductFlat) {
|
||||||
|
if price.OriginalPrice.Value > price.DiscountedPrice.Value {
|
||||||
|
|
||||||
|
importer.baza.Model(&flat).Updates(map[string]interface{}{
|
||||||
|
"price": price.OriginalPrice.Value,
|
||||||
|
"special_price": price.DiscountedPrice.Value,
|
||||||
|
"min_price": price.DiscountedPrice.Value,
|
||||||
|
"max_price": price.OriginalPrice.Value,
|
||||||
|
})
|
||||||
|
|
||||||
|
importer.baza.Model(&gm.ProductAttributeValue{}).
|
||||||
|
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
||||||
|
Update("float_value", price.OriginalPrice.Value)
|
||||||
|
|
||||||
|
importer.baza.Model(&gm.ProductAttributeValue{}).
|
||||||
|
Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
||||||
|
Update("float_value", price.DiscountedPrice.Value)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
importer.baza.Model(&flat).Updates(map[string]interface{}{
|
||||||
|
"price": price.DiscountedPrice.Value,
|
||||||
|
"special_price": nil,
|
||||||
|
"min_price": price.DiscountedPrice.Value,
|
||||||
|
"max_price": price.DiscountedPrice.Value,
|
||||||
|
})
|
||||||
|
|
||||||
|
importer.baza.Model(&gm.ProductAttributeValue{}).
|
||||||
|
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
||||||
|
Update("float_value", price.DiscountedPrice.Value)
|
||||||
|
|
||||||
|
importer.baza.Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
||||||
|
Delete(&gm.ProductAttributeValue{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
||||||
|
|
||||||
firstProduct, err := importer.importVariant(product)
|
firstProduct, err := importer.importVariant(product)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue