Skip to main content

Add product options to the cart API

Project description

Odoo Community Association

Shopinvader API Cart Options

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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_addon_shopinvader_api_cart_options-16.0.1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 170dbe4f0ca91aac9d99027074b829328adf20502e65df466771d9789bf9310e
MD5 0f533bc5f1061eae59735f5bc63693df
BLAKE2b-256 4f655454d5f5a0cecb1e1b8941b78f0f646429d83df07cc688002af0dace79ee

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