Skip to main content

Add product options to the cart API

Project description

Beta License: AGPL-3 shopinvader/odoo-shopinvader

This module allows to add options to the cart lines, grouping cart transaction on the combination of the product and the options.

It also handles the cart transfer merge by using the options to merge the cart lines.

Table of contents

Usage

This module is designed to be extended, so you can add your own options to the cart lines.

In order to do so, you need to extend the SaleLineOptions schema and add your own options:

class SaleLineOptions(BaseSaleLineOptions, extends=True):
    engraving: str | None = None
    special: bool = False

    @classmethod
    def from_sale_order_line(cls, odoo_rec):
        rv = super().from_sale_order_line(odoo_rec)
        rv.engraving = odoo_rec.carving or None
        rv.special = odoo_rec.special
        return rv

Then you will need to extend the SaleOrderLine model to add support for you options in the cart line matching and in the cart line transfer:

class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"
    carving = fields.Char()
    special = fields.Boolean()

    def _match_cart_line(
        self,
        product_id,
        carving=None,
        special=None,
        **kwargs,
    ):
        rv = super()._match_cart_line(
            product_id,
            carving=carving,
            special=special,
            **kwargs,
        )
        return rv and self.carving == carving and self.special == special

    def _prepare_cart_line_transfer_values(self):
        vals = super()._prepare_cart_line_transfer_values()
        vals["carving"] = self.carving
        vals["special"] = self.special
        return vals

And finally, you will need to extend the ShopinvaderApiCartRouterHelper to add support for your options in the cart line creation from the transaction API:

class ShopinvaderApiCartRouterHelper(
    models.AbstractModel
):  # pylint: disable=consider-merging-classes-inherited
    _inherit = "shopinvader_api_cart.cart_router.helper"

    @api.model
    def _apply_transactions_creating_new_cart_line_prepare_vals(
        self, cart: SaleOrder, transactions: list[CartTransaction], values: dict
    ):
        options = transactions[0].options
        if options:
            values["carving"] = options.engraving
            values["special"] = options.special

        return values

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

Credits

Authors

  • Akretion

Contributors

Maintainers

This module is part of the shopinvader/odoo-shopinvader project on GitHub.

You are welcome to contribute.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file odoo_addon_shopinvader_api_cart_options-16.0.1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_addon_shopinvader_api_cart_options-16.0.1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f963402315e52a304857f6043ba4a38c129c8ce7e6d210ba4ffe578e6b176169
MD5 e562cfc2817163f42e7770558ecbe51a
BLAKE2b-256 3fd52f4f9b2acf1cce863e164cb2dee4b46c72c21a4b22d70eb27092dd9f31f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page