-
Notifications
You must be signed in to change notification settings - Fork 49
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
Stock Handling #18
Comments
I think the item validator would do the job if it could be raised on item deletes/ basket clears |
@IncreaseComputers could you perhaps use Django's The basket item validator is used just for validating the item, it is not aware of deletes. |
Signals wont work as getting the original values from the objects is problematic - Ie in basket save has the quantity changed? what was the old quantity what is the new quantity? - I have implemented a "stock _handler" override similar to the validators and hooked into it at various points in basket and basket item to deal with changes - it passes a operation "delete" / "add" /"update" the original object and the changed fields - it works but it means overriding core code making it more difficult to maintain |
I see what you're saying. I suppose you could swap the basket models and add the additional field that keeps track of the "previous quantity" on the basket item. You could do this using the swappable models feature: The thing with stock handling is that it can differ from the shop implementation making it hard to put specific logic into the core project. Maybe having dedicated signals for whenever an item is added/updated/deleted on the basket would solve this. I would also consider a different approach to stock management, one where it does not tie to the basket directly.
This way you're not updating the stock on every basket item add/remove, but rather when the intent to buy the items is created. |
one of the problems (which is specific to my implementation) is that I'm selling memberships - which have a limit - so I have to follow the basket rather than the order - I cant oversell... - I will stick with my overrides for now - I'm planning to also implement the methods into the order to also convert a "reservation" to a completed stock adjustment. |
To handle the abandoned item I have added an "expiry" timer to the item which removes it from the cart after a specified time |
@IncreaseComputers I believe you would still avoid overselling in my example by raising Either way, here's an example implementation using Django signals without overriding the basket item model: from django.dispatch import receiver
from django.db.models.signals import post_delete, post_init, post_save
from salesman.core.utils import get_salesman_model
BasketItem = get_salesman_model("BasketItem")
@receiver(post_init, sender=BasketItem)
def post_init_item(sender, instance, **kwargs):
# Remember current quantity on the instance
instance._current_quantity = 0 if instance.pk is None else instance.quantity
@receiver(post_save, sender=BasketItem)
def post_save_item(sender, instance, **kwargs):
quantity_diff = instance.quantity - instance._current_quantity
if quantity_diff > 0:
print(f"Decrease {instance.product} stock by {quantity_diff}")
elif quantity_diff < 0:
print(f"Increase {instance.product} stock by {quantity_diff * -1}")
@receiver(post_delete, sender=BasketItem)
def post_delete_item(sender, instance, **kwargs):
print(f"Increase {instance.product} stock for {instance.quantity}") You can of course override the I am considering adding the |
By the way, I would appreciate this item_quantity_changed signal ok
Thanks for this amazing job!
Em sex., 29 de abr. de 2022 às 09:06, Dino Perovic ***@***.***>
escreveu:
@IncreaseComputers <https://github.com/IncreaseComputers> I believe you
would still avoid overselling in my example by raising ValidationError in
validate_basket method on the payment if any one of the items is not
available.
Either way, here's an example implementation using Django signals without
overriding the basket item model:
from django.db.models.signals import post_delete, post_init, post_save
from salesman.core.utils import get_salesman_model
BasketItem = get_salesman_model("BasketItem")
@receiver(post_init, sender=BasketItem)def post_init_item(sender, instance, **kwargs):
# Remember current quantity on the instance
instance._current_quantity = 0 if instance.pk is None else instance.quantity
@receiver(post_save, sender=BasketItem)def post_save_item(sender, instance, **kwargs):
quantity_diff = instance.quantity - instance._current_quantity
if quantity_diff > 0:
print(f"Decrease {instance.product} stock by {quantity_diff}")
elif quantity_diff < 0:
print(f"Increase {instance.product} stock by {quantity_diff * -1}")
@receiver(post_delete, sender=BasketItem)def post_delete_item(sender, instance, **kwargs):
print(f"Increase {instance.product} stock for {instance.quantity}")
You can of course override the __init__, save and delete methods on
basket item directly using swappable models.
I am considering adding the item_quantity_changed signal that would
basically do this by default, but I'm still not sure if it would be helpful
for any other cases.
—
Reply to this email directly, view it on GitHub
<#18 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFQYBTZ3MR4Y55SCWLQVJ5DVHPGFPANCNFSM5UIWS6KQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
--
*¶ Pablo Valentini*
Nelson Boulangerie
***@***.***>(43) 3321 3954 comercial
(43) 8404 9009 celular
|
I'm looking to implement stock handling for order s/ basket items - handling the stock is fine the problem I'm having is catching basket quantity changes to adjust stock levels, any ideas or suggestions on doing this?
The text was updated successfully, but these errors were encountered: