Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] [ADD] shopinvader_api_cart_options #1576

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from

Conversation

paradoxxxzero
Copy link
Contributor

@paradoxxxzero paradoxxxzero commented Dec 16, 2024

This PR depends on #1575
This PR replaces #1550 and is a rework of #1457
This PR tests depends on OCA/rest-framework#482 Merged

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.

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

@paradoxxxzero paradoxxxzero force-pushed the 16.0-add-shopinvader_api_cart_options branch 2 times, most recently from 97e3e82 to 53327ae Compare January 6, 2025 08:03
@paradoxxxzero paradoxxxzero force-pushed the 16.0-add-shopinvader_api_cart_options branch from 53327ae to 9c63db8 Compare January 6, 2025 08:05
@paradoxxxzero paradoxxxzero force-pushed the 16.0-add-shopinvader_api_cart_options branch from 9c63db8 to 911f550 Compare January 6, 2025 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants