You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo电商网站选择指定配送方式后隐藏对应支付方式问题咨询

实现方案

你之前新增的delivery.carrier关联payment.acquirer的多对多字段逻辑是正确的,不生效的核心原因是没有把允许的支付方式列表同步到支付渲染的上下文,也没有做对应的过滤逻辑,可按以下步骤修改:

1. 修正控制器逻辑,存储允许的支付方式到session

你现有的控制器只返回了允许的支付ID,没有持久化存储,页面刷新后数据就丢失了,修改后的代码如下:

class WebsiteSaleDeliveryExtraCost(WebsiteSale):
    @http.route(['/shop/change_shipping_method'], type='json', auth='public', methods=['POST'], website=True, csrf=False)
    def change_shipping_method(self, **post):
        carrier_id = post.get('carrier_id')
        carrier = request.env['delivery.carrier'].browse(int(carrier_id))
        acquirer_ids = carrier.payment_acquirer_ids.ids
        # 将允许的支付方式ID存入session
        request.session['allowed_payment_acquirer_ids'] = acquirer_ids
        return acquirer_ids

2. 重写支付页面渲染逻辑,过滤支付方式

继承Odoo默认的支付参数获取方法,在渲染支付方式前按session存储的允许列表过滤:

from odoo.addons.website_sale.controllers.main import WebsiteSale

class WebsiteSalePaymentFilter(WebsiteSale):
    def _get_shop_payment_values(self, order, **kwargs):
        values = super()._get_shop_payment_values(order, **kwargs)
        allowed_ids = request.session.get('allowed_payment_acquirer_ids', [])
        # 仅当配送方式绑定了指定支付方式时才做过滤,否则展示所有可用支付方式
        if allowed_ids:
            values['acquirers'] = [acq for acq in values['acquirers'] if acq.id in allowed_ids]
        return values

3. 调整前端JS逻辑(可选优化)

你现有的刷新页面逻辑可以正常生效,如果想优化体验无需全页刷新,可直接在前端控制支付方式的显示隐藏,修改后的JS代码如下:

odoo.define('website_sale_delivery_extra_cost.checkout', function (require) {
'use strict';
var publicWidget = require('web.public.widget');
require('website_sale_delivery.checkout')

publicWidget.registry.websiteSaleDelivery.include({
    selector: '.oe_website_sale',
    events: {
        'click #delivery_carrier .o_delivery_carrier_select': '_change_shipping_method',
    },
    _change_shipping_method: function (ev) {
        var self = this;
        var $radio = $(ev.currentTarget).find('input[type="radio"]');
        if ($radio.val()){
            this._rpc({
                route: '/shop/change_shipping_method',
                params: {
                    carrier_id: $radio.val(),
                }}).then(function (allowedAcquirerIds) {
                    // 隐藏所有支付方式
                    $('div.o_payment_method').hide();
                    // 仅展示允许的支付方式
                    allowedAcquirerIds.forEach(acqId => {
                        $(`div.o_payment_method[data-acquirer-id="${acqId}"]`).show();
                    });
                });
        }
    },
});
});

4. 补充边界处理

  • 当用户取消订单、清空购物车时,需要清空request.session['allowed_payment_acquirer_ids']字段,避免影响后续其他订单的支付逻辑
  • 建议修正模型字段的拼写错误:Payment Mathods改为Payment Methods

内容的提问来源于stack exchange,提问作者hassan abdullah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 07:48:03