Deleting Specific Product Option Using Shopify API

peerdenhoed
Shopify Partner
4 0 0

Using the Python ShopifyAPI library, version 12.3.0 I want to delete a specific product option.

def remove_product_option(self, product_id):
        product = self.get_product_by_id(product_id)
        option_name = self.config['option_to_delete']

        for i, option in enumerate(product.options):
            if option.name == option_name:
                product.options.pop(i)
                product.save()

This code is not working. There are no error messages, but the product.save() returns false. What can be the problem, and how can I fix it?

Edit:
It seems like you cannot delete a product option if it has values. Is it possible to delete option values with the API?

Replies 3 (3)

Liam
Shopify Staff
2731 302 783

Hi Peerdenhoed,

 

If an option has associated variant values, you can't simply remove the option without addressing those values first.

 

Here's a step-by-step approach to handle this:

  1. Retrieve the Product: Fetch the product using the provided product_id.

  2. Identify the Option to Delete: Loop through the product's options to find the one you want to delete based on its name.

  3. Handle Variants: Before you can delete the option, you need to handle the variants that use this option.

    a. If the option has associated variant values, you'll need to either modify those variants to remove the specific option value or delete the variants entirely.

    b. For each variant, remove or modify the value associated with the option you're deleting.

  4. Delete the Option: Once all variants have been handled, you can safely remove the option from the product.

  5. Save the Product: Finally, save the product to persist the changes.

Here's a modified version of your code:

 

python
def remove_product_option(self, product_id): product = self.get_product_by_id(product_id) option_name = self.config['option_to_delete'] # Identify the option to delete option_to_delete = None for option in product.options: if option.name == option_name: option_to_delete = option break if not option_to_delete: return False # Handle variants for variant in product.variants: # Modify or delete the variant value associated with the option_to_delete # ... # Remove the option product.options.remove(option_to_delete) # Save the product return product.save()

 

Note: The above code is a general approach. You'll need to fill in the details, especially in the "Handle variants" section, based on your specific requirements.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

thinkcloud
Shopify Partner
8 0 0

Hello, I have the same problem, I have this error "could not delete option because it has more than 1 value"

So I tried what you said, going through the variants and dealing with the one that doesn't interest me, but after several hours of testing, I'm stuck.

 

Here is an example :

My product has 3 options: Color, Size, Shape
Each option has 3 variations (Size: Black, WHite, Red)

Let's imagine that I want to delete the Size option, the problem is that Shopify in its variants contains sizes in the 27 variants (3*3*3)

If I want to remove Size, I reduce the number of variants: 3*3.
How to know which variants to remove is where I'm stuck.

 

Thanks for your help

thinkcloud
Shopify Partner
8 0 0

I solved my problem, it doesn't matter which one is deleted as long as there is no duplicate and the option in question is deleted.