Skip to content

Instantly share code, notes, and snippets.

@leflings
Created August 26, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leflings/942042a43af34c59ed1687669ff96c98 to your computer and use it in GitHub Desktop.
Save leflings/942042a43af34c59ed1687669ff96c98 to your computer and use it in GitHub Desktop.
Product Options API: How to change position of values?

Create the option

POST http://127.0.0.1:8000/api/v1/product-options/ HTTP/1.1
Authorization: Bearer VALID_TOKEN
Content-Type: application/json

{
  "code": "my_test_option_for_testing",
  "position": 10,
  "translations": {
    "en_US": {
      "name": "Is this weird?"
    }
  },
  "values": [
    {
      "code": "my_test_option_value_yes",
      "translations": {
        "en_US": {
          "value": "Yes"
        }
      }
    },
    {
      "code": "my_test_option_value_no",
      "translations": {
        "en_US": {
          "value": "No"
        }
      }
    }
  ]
}

Update the option (with a new optionvalue at index 1, existing optionvalue "pushed" to index 2)

PUT http://127.0.0.1:8000/api/v1/product-options/my_test_option_for_testing HTTP/1.1
Authorization: Bearer VALID_TOKEN
Content-Type: application/json

{
  "code": "my_test_option_for_testing",
  "position": 10,
  "translations": {
    "en_US": {
      "name": "Is this weird?"
    }
  },
  "values": [
    {
      "code": "my_test_option_value_yes",
      "translations": {
        "en_US": {
          "value": "Yes"
        }
      }
    },
    {
      "code": "my_test_option_value_somewhat",
      "translations": {
        "en_US": {
          "value": "Yeah, sort of"
        }
      }
    },
    {
      "code": "my_test_option_value_no",
      "translations": {
        "en_US": {
          "value": "No"
        }
      }
    }
  ]
}

Observe the validation error

{
    "code": 400,
    "message": "Validation Failed",
    "errors": {
        "children": {
            "position": {},
            "translations": {
                "children": {
                    "en_US": {
                        "children": {
                            "name": {}
                        }
                    }
                }
            },
            "values": {
                "children": [
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {}
                        }
                    },
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {}
                        }
                    },
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {
                                "errors": [
                                    "The option value with given code already exists."
                                ]
                            }
                        }
                    }
                ]
            },
            "code": {}
        }
    }
}

My question is this

Is this really the expected behaviour?

Say I actually cared about changing the position of two values, it seems to me like I would have to first update the option to not include the two values I want to change, and then later update it again with the values in the order I want them. But what if said values are in use on a product variant, I'm assuming it would be illegal to remove them from the option.

Aside from aforementioned particular problem, there's issue of having to have local bookkeeping data of value positions, in order to formulate a valid PUT/PATCH query. If not having local bookkeeping, I'd need to first GET option details, synchronize the order of my values with the order of the values on the server.

I'm not saying this is a bug, but I'm just trying to understand.

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