How to add custom benefit in django-oscar?

一笑奈何 提交于 2021-02-08 11:38:40

问题


Django-oscar provides multibuy benefit type.

class MultibuyDiscountBenefit(Benefit):
    _description = _("Cheapest product from %(range)s is free")

Now, I can add Buy 1 get 1 free offer with this benefit.

I have a little custom requirement here. I want to add 'Buy 1 get 50% off on second' offer. To do so, I need to add custom benefit.

I checked docs for adding custom benefit.

And as per doc says..A custom benefit can be used by creating a benefit class and registering it so it is available to be used..

Following docs, I created my custom benefit for that.

class MultiBuyCustom(Benefit):

    class Meta:
        proxy = True

    @property
    def description(self):
        """
        Describe what the benefit does.

        This is used in the dashboard when selecting benefits for offers.
        """
        return "But 1 and get 50% off"

Here I don't know how to register this custom benefit to using in dashboard.? I need this benefit in the dropdown at dashboard while creating the offer.

Any help would be greatly appreciated.


回答1:


First of all you will need to fork the offer app from oscar with the below command.

./manage.py oscar_fork_app offer apps/shop

One can add custom benefit in the benefits.py file. Below benefit class will give "cheapest product from the selected range with 50% off".

class NewCustomBenefit(benefits.Benefit):
    description = "Cheapest product from range is 50% off"

    @property
    def name(self):
        return self.description

    class Meta:
        app_label = 'offer'
        proxy = True
        verbose_name = _("Buy 1 get 50% off")
        verbose_name_plural = _("Buy 1 get 50% off")

    def apply(self, basket, condition, offer):
        line_tuples = self.get_applicable_lines(offer, basket, range=condition.range)
        if not line_tuples:
            return results.ZERO_DISCOUNT

        # Cheapest line gives 50% off on second product
        discount, line = line_tuples[0]
        discount /= 2
        apply_discount(line, discount, 1)

        affected_lines = [(line, discount, 1)]
        condition.consume_items(offer, basket, affected_lines)
        return results.BasketDiscount(discount)

    def __unicode__(self):
        return unicode(self.name)

Now, next step is to available this benefit while adding offer from the dashboard. You will have option to select predefined benefit/incentive from the the dropdown.

Now, to available this benefit here You'll need to register our custom benefit from the admin panel. So, follow below screenshot. You gotta enter the path to the custom benefit class in the custom class field. Other-than that keep everything blank as you'll add those info from dashboard while creating offer.

Once you save this, you'll have your benefit in the dropdown as shown in the first screenshot.

It works.! Ask If have any other query.



来源:https://stackoverflow.com/questions/42779583/how-to-add-custom-benefit-in-django-oscar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!