Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Additional Option Group Result in Error #357

Open
jacklimwenjie opened this issue Feb 13, 2021 · 0 comments
Open

Add Additional Option Group Result in Error #357

jacklimwenjie opened this issue Feb 13, 2021 · 0 comments

Comments

@jacklimwenjie
Copy link

jacklimwenjie commented Feb 13, 2021

Given a product with variants set up (with existing option groups),
trying to add a new option group to the product by calling POST /api/v1/products/:product_id/variants with the variants field but without the options field
result in an error.

Expected Behavior

For a product that has existing option groups i.e. option_data is not empty e.g.

{
"color": {
    "label": {
      "en": "Color"
    },
    "options": {
      "black": {
        "values": {
          "en": "black"
        },
        "position": 1
      },
      "white": {
        "values": {
          "en": "white"
        },
        "position": 2
      }
    },
    "position": 1
  }
}

attempt to add another entry to the option_data by calling POST /api/v1/products/:product_id/variants with a request body with a variants field but without an options field e.g.

{
	"variants": [
		{
			"label": "white s",
			"price": 1,
			"sku": "white-sss",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "black s",
			"price": "1.00",
			"sku": "black-sss",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "white m",
			"price": 1,
			"sku": "white-mmm",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		},
		{
			"label": "black m",
			"price": "1.00",
			"sku": "black-mmm",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		}
	]
}

updates the option_data column in DB to

{
  "size": {
    "label": {
      "en": "S"
    },
    "options": {
      "m": {
        "values": {
          "en": "m"
        },
        "position": 2
      },
      "s": {
        "values": {
          "en": "s"
        },
        "position": 1
      }
    },
    "position": 2
  },
  "color": {
    "label": {
      "en": "Color"
    },
    "options": {
      "black": {
        "values": {
          "en": "black"
        },
        "position": 1
      },
      "white": {
        "values": {
          "en": "white"
        },
        "position": 2
      }
    },
    "position": 1
  }
}

and the endpoint responds with success status.

Current Behavior

For a product that has existing option groups i.e. option_data is not empty e.g.

{
"color": {
    "label": {
      "en": "Color"
    },
    "options": {
      "black": {
        "values": {
          "en": "black"
        },
        "position": 1
      },
      "white": {
        "values": {
          "en": "white"
        },
        "position": 2
      }
    },
    "position": 1
  }
}

attempt to add another entry to the option_data by calling POST /api/v1/products/:product_id/variants with a request body with a variants field but without an options field e.g.

{
	"variants": [
		{
			"label": "white s",
			"price": 1,
			"sku": "white-sss",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "black s",
			"price": "1.00",
			"sku": "black-sss",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "white m",
			"price": 1,
			"sku": "white-mmm",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		},
		{
			"label": "black m",
			"price": "1.00",
			"sku": "black-mmm",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		}
	]
}

updates the option_data column in DB to

{
  "s": {
    "label": {
      "en": "S"
    },
    "options": {
      "m": {
        "values": {
          "en": "m"
        },
        "position": 2
      },
      "s": {
        "values": {
          "en": "s"
        },
        "position": 1
      }
    },
    "position": 2
  },
  "color": {
    "label": {
      "en": "Color"
    },
    "options": {
      "black": {
        "values": {
          "en": "black"
        },
        "position": 1
      },
      "white": {
        "values": {
          "en": "white"
        },
        "position": 2
      }
    },
    "position": 1
  }
}

and the endpoint responds with an error Undefined index: sizewith stack trace pointing to vendor/getcandy/candy-api/src/Core/Products/Models/ProductVariant.php:103.

Possible Solution

Steps to Reproduce

  1. Create a product
  2. Create a variant for the product: POST /api/v1/products/:product_id/variants with request body:
{
  "color": {
    "label": {
      "en": "Color"
    },
    "options": {
      "black": {
        "values": {
          "en": "black"
        },
        "position": 1
      },
      "white": {
        "values": {
          "en": "white"
        },
        "position": 2
      }
    },
    "position": 1
  },
  "variants": [
    {
	    "label": "white",
	    "price": 1,
	    "sku": "white",
	    "options": {
		    "color": {
			    "en": "white"
		    }
	    },
	    "inventory": 1
    },
    {
	    "label": "black",
	    "price": "1.00",
	    "sku": "black",
	    "options": {
		    "color": {
			    "en": "black"
		    }
	    },
	    "inventory": 1
    }
  ]
}
  1. Attempt to add a new option group by calling POST /api/v1/products/:product_id/variants with request body:
{
	"variants": [
		{
			"label": "white s",
			"price": 1,
			"sku": "white-sss",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "black s",
			"price": "1.00",
			"sku": "black-sss",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "s"
				}
			},
			"inventory": 1
		},
		{
			"label": "white m",
			"price": 1,
			"sku": "white-mmm",
			"options": {
				"color": {
					"en": "white"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		},
		{
			"label": "black m",
			"price": "1.00",
			"sku": "black-mmm",
			"options": {
				"color": {
					"en": "black"
				},
				"size": {
					"en": "m"
				}
			},
			"inventory": 1
		}
	]
}
  1. Observe the API endpoint responds with an error Undefined index: size.

Context (Environment)

getcandy/candy-api: version 0.11.5
OS: MacOS 11.1

I want to add a new option group to a product that already has existing option groups.
For example, an admin could decide to create a new option group size for a T-shirt product that already has a color option group.
On the current admin hub, I find it impossible to achieve such a feat - it's only possible to add new options to an existing option group. Using the same example above, this means an admin could only add more options to the existing color option group e.g. add grey color to the options, but not adding a new option group size.

Detailed Description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants