[Fixed]-Create a canonical "parent" product in Django Oscar programmatically

5πŸ‘

To be able to correctly reflect the stock levels for any Product you need to have a Partner that will supply the Product and then you need to have StockRecord that links the Partner and the Products together.

First make sure that you have all that information in the database for each one of your Product variations.

Then you need to update your ProductClass and set the "track_stock" attribute as True since its None by default.

You also need to remove the ProductClass from your child products since they inherit the ProductClass from their Parent Product.

EDIT 1:

To add attributes to a Product you have to add a ProductAttribute for the ProductClass and then you can set the attributes directly on the Product like this example.

EDIT 2:

You also need to set the "net_stock_level" on the StockRecord.

To get a more in depth look into how Oscar gets the stock levels look into Selector. This class determines which pricing, tax and stock level strategies to use which you might need to customize in the future if you want to charge tax or offer different pricing based on the user.

πŸ‘€Alex Carlos

0πŸ‘

After some research from the test factory, I solved the issue by specifying

product.stucture = 'parent' 

On the parent object, and

product.structure = 'child'

on the child object. I also needed to change the custom attributes of my objects to a dict product_attributes, and then set each value on the object:

if product_attributes:
        for code, value in product_attributes.items():
            product_class.attributes.get_or_create(name=code, code=code)
            setattr(product.attr, code, value)

It was not necessary to create a stock record for each parent object, as they track the stock records of the child objects to which they are associated. It was also not necessary to set track_stock = True, as it is set to True by default when creating a Product()


This answer was posted as an edit to the question Create a canonical "parent" product in Django Oscar programmatically by the OP Tui Popenoe under CC BY-SA 3.0.

πŸ‘€vvvvv

Leave a comment