如何在django-oscar中添加自定义权益? [英] How to add custom benefit in django-oscar?

查看:49
本文介绍了如何在django-oscar中添加自定义权益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django-oscar提供 multibuy 福利类型.

  Class MultibuyDiscountBenefit(Benefit):_description = _(%(range)s个最便宜的产品是免费的") 

现在,我可以添加买一送一免费优惠.

我在这里有一些自定义要求.我想添加买一送一,第二折50%" 优惠.为此,我需要添加自定义权益.

我检查了

现在,要在此处使用此权益,您需要在管理面板中注册我们的自定义权益.因此,请遵循以下屏幕截图.您必须在定制类别"字段中输入定制收益类别的路径.除此之外-可以将所有内容保留为空白,因为您在创建报价时将从仪表板添加这些信息.

保存后,您将在第一张屏幕截图所示的下拉菜单中受益.

有效.!询问是否还有其他查询.

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.

解决方案

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.

这篇关于如何在django-oscar中添加自定义权益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆