如何在 Django Oscar 中重写类方法

问题描述 投票:0回答:1
目前我已经可以覆盖该类,但我无法覆盖方法 get() 我错过了什么?

我需要这样做,因为如果只有 1 种送货方式,它不会加载“送货方式”页面,而是重定向到付款页面,但我希望客户在进入付款页面之前看到运费。

apps.py import oscar.apps.checkout.apps as apps from oscar.core.loading import get_class from django.urls import path class CheckoutConfig(apps.CheckoutConfig): name = 'apps.checkout' def ready(self): self.shipping_method_view = get_class('checkout.views', 'ShippingMethodViewOverwritten') super().ready() def get_urls(self): urls = super(CheckoutConfig, self).get_urls() urls += [ path('shipping-method/', self.shipping_method_view.as_view(), name='shipping-method'), ] return urls
views.py

from oscar.apps.checkout import views
from oscar.core.loading import get_class
ShippingAddressForm, ShippingMethodForm, GatewayForm \
    = get_classes('checkout.forms', ['ShippingAddressForm', 'ShippingMethodForm', 'GatewayForm'])
from django.contrib import messages
Repository = get_class('shipping.repository', 'Repository')

class ShippingMethodViewOverwritten(views.ShippingMethodView):
    template_name = 'oscar/checkout/shipping_methods.html'
    form_class = ShippingMethodForm
    pre_conditions = ['check_basket_is_not_empty',
                      'check_basket_is_valid',
                      'check_user_email_is_captured']
    success_url = reverse_lazy('checkout:payment-method')

    def get(self, request, *args, **kwargs):

        if not request.basket.is_shipping_required():
            self.checkout_session.use_shipping_method(
                NoShippingRequired().code)
            return self.get_success_response()

        if not self.checkout_session.is_shipping_address_set():
            messages.error(request, _("Please choose a shipping address"))
            return redirect('checkout:shipping-address')

        self._methods = self.get_available_shipping_methods()
        if len(self._methods) == 0:
            # No shipping methods available for given address
            messages.warning(request, _(
                "Shipping is unavailable for your chosen address - please "
                "choose another"))
            return redirect('checkout:shipping-address')
        elif len(self._methods) == 1:
            # Only one shipping method - set this and redirect onto the next
            # step
            self.checkout_session.use_shipping_method(self._methods[0].code)
            return self.get_success_response()

        return super().get(request, *args, **kwargs)

    def get_available_shipping_methods(self):
        return Repository().get_shipping_methods(basket=self.request.basket, 
        user=self.request.user,shipping_addr=self.get_shipping_address(self.request.basket), 
        request=self.request)

    
django django-oscar
1个回答
0
投票
ShippingMethodView 中有一个显式逻辑,可以处理可用的单一运输方式的情况,并跳过“运输方式”选择屏幕。为了覆盖此行为,首先您需要自定义 Checkout 应用程序。请遵循

此处有关如何分叉 Oscar 应用程序的指南。

这是它在 Oscar 中的实现方式

File: oscar/apps/checkout/views.py def get(self, request, *args, **kwargs): ..... self._methods = self.get_available_shipping_methods() if len(self._methods) == 0: # No shipping methods available for given address messages.warning( request, _( "Shipping is unavailable for your chosen address - please " "choose another" ), ) return redirect("checkout:shipping-address") elif len(self._methods) == 1: # Only one shipping method - set this and redirect onto the next # step self.checkout_session.use_shipping_method(self._methods[0].code) return self.get_success_response() .....
只需删除 

elif

 块即可绕过正在设置运输方式(如果仅找到)的工作流程。

© www.soinside.com 2019 - 2024. All rights reserved.