我需要这样做,因为如果只有 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)
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
块即可绕过正在设置运输方式(如果仅找到)的工作流程。