I am creating a custom module for odoo17 in which I have a res.country.zone model
I have extended the following models:
res.partner: I add the "zone_id" field
delivery.carrier: I add the zone_ids field
The partner will always have a zone_id assigned, to the delivery methods I assign the zone_ids to know what is available.
So what I want to achieve is that on the /shop/payment page only the delivery methods that contain the partner's zone_id within its list of zone_ids are shown.
zones [1,2,3,4,5]
partner A - zone_id=1
delivery_method-A - zone_ids [2, 4]
delivery_method-B - zone_ids [1, 3, 5]
delivery_method-C - zone_ids [1, 4]
for partner A should only see delivery_method-B and delivery_method-C thats are available in his area
I have tried to do it by rewriting some methods of the inherited classes but I don't get the result I need.
class DeliveryCarrier(models.Model):
_inherit = 'delivery.carrier'
zone_ids = fields.Many2many('res.country.zone', string='Zone')
....
....
....
def available_carriers(self, partner):
return self.filtered(lambda c:
._is_available_for_order(partner.sale_order_id))
def _is_available_for_order(self, order):
self.ensure_one()
order.ensure_one()
if not self._match_address(order.partner_shipping_id):
return False
partner_zone_id = order.env.context.get('zone_id')
if partner_zone_id and partner_zone_id not in self.zone_ids.ids:
return False
if self.delivery_type == 'base_on_rule':
return self.rate_shipment(order).get('success')
return True
class ChooseDeliveryCarrier(models.TransientModel):
_inherit = 'choose.delivery.carrier'
@api.depends('partner_id')
def _compute_available_carrier(self):
for rec in self:
zone_id = self.env.context.get('zone_id')
carriers = self.env['delivery.carrier'].search([]).filtered(lambda carrier: carrier._is_available_for_order(rec.order_id))
rec.available_carrier_ids = carriers
On the other hand, since I don't need it, I want to make the street, phone fields not required in the shipping address and hide the zip field in website_sale.address
I tried some things by creating a custom view like the following, to make the zip field read-only, but it didn't work, it still appears enabled:
template id="address_form_inherit" inherit_id="website_sale.address"
"//input[@name='zip']" position="attributes"
attribute name="readonly">readonly
...
some suggest?