# Path: catalog/models.py
# class Modifier(TranslatableModel, CatalogModel):
#     """
#     Defines different amounts to be applied to a product or a category.
#     """
#     KIND_STANDARD = 'standard'
#     KIND_DISCOUNT = 'discount'
#     KIND_CART_MODIFIER = 'cart_modifier'
#     KIND_CHOICES = (
#         (KIND_STANDARD, _('Standard')),
#         (KIND_DISCOUNT, _('Discount')),
#         (KIND_CART_MODIFIER, _('Cart modifier')),
#     )
# 
#     code = models.SlugField(
#         _('Code'), max_length=128, unique=True,
#         help_text=scs.SLUG_FIELD_HELP_TEXT)
# 
#     amount = CurrencyField(
#         verbose_name=_('Amount'),
#         help_text=_('Absolute amount that should be applied. '
#                     'Can be negative.'))
# 
#     percent = models.DecimalField(
#         _('Percent'), blank=True, null=True,
#         max_digits=4, decimal_places=2,
#         help_text=_('If percent is set, it will override the absolute '
#                     'amount. Can be negative.'))
# 
#     kind = models.CharField(
#         _('Kind'), max_length=128, choices=KIND_CHOICES, default=KIND_STANDARD,
#         help_text=_('Select a modifier kind. If "Cart modifier" is selected, '
#                     'modifier will be constant and affect entire cart.'))
# 
#     translations = TranslatedFields(
#         name=models.CharField(_('Name'), max_length=128),
#     )
# 
#     objects = CatalogManager()
# 
#     class Meta:
#         db_table = 'catalog_modifiers'
#         verbose_name = _('Modifier')
#         verbose_name_plural = _('Modifiers')
# 
#     def get_name(self):
#         return self.safe_translation_getter('name')
# 
#     def get_slug(self):
#         return self.code
# 
#     def get_extra_cart_item_price_field(self, cart_item, request=None):
#         """
#         Returns extra price field for the given cart item.
#         """
#         if self.can_be_applied(cart_item=cart_item, request=request):
#             return (self.get_name(), self.calculate_add_price(
#                 cart_item.current_total, cart_item.quantity))
# 
#     def get_extra_cart_price_field(self, cart, request=None):
#         """
#         Returns extra price field for entire cart.
#         """
#         if self.can_be_applied(cart=cart, request=request):
#             return (self.get_name(), self.calculate_add_price(
#                 cart.current_total))
# 
#     def calculate_add_price(self, price, quantity=1):
#         """
#         Calculates and returns an amount to be added to the given price.
#         """
#         if self.percent:
#             add_price = (self.percent / 100) * price
#         else:
#             add_price = self.amount * quantity
#         return add_price
# 
#     def can_be_applied(self, cart_item=None, cart=None, request=None):
#         """
#         Checks if this modifier can be applied or not.
#         """
#         # Check that product is eligible if cart_item is specified.
#         if cart_item and not self.is_eligible_product(cart_item.product):
#             return False
# 
#         # Check that all conditions are met.
#         for con in self.conditions.select_related().all():
#             if not con.is_met(cart_item=cart_item, cart=cart, request=request):
#                 return False
# 
#         cart_id = cart_item.cart_id if cart_item else cart.id if cart else None
#         return self.is_code_applied(cart_id)
# 
#     def is_code_applied(self, cart_id):
#         """
#         Check for codes on this modifier, and if they exist make sure
#         that at least one is applied to the given cart. Codes have
#         alerady been validated so we just have to make sure they are
#         active and exist.
#         """
#         codes = self.codes.active()
# 
#         if codes.exists():
#             cart_codes = CartModifierCode.objects.filter(cart_id=cart_id)
#             cart_codes = list(set(cart_codes.values_list('code', flat=True)))
#             if cart_codes:
#                 for code in cart_codes:
#                     if codes.filter(code=code).exists():
#                         return True
# 
#             # Codes exist but not one is applied, return False.
#             return False
# 
#         # Codes don't exist, return True.
#         return True
# 
#     def is_eligible_product(self, product):
#         """
#         Returns if modifier can be applied to a given product.
#         """
#         if self.kind == self.KIND_DISCOUNT:
#             return product.is_discountable
#         return self.kind != self.KIND_CART_MODIFIER
# 
#     @classmethod
#     def get_cart_modifiers(cls):
#         """
#         Returns all active cart modifiers.
#         """
#         return cls.objects.active(kind=cls.KIND_CART_MODIFIER)


from shop.cart.cart_modifiers_base import BaseCartModifier
from catalog.models import Modifier

    def process_cart(self, cart, request):
        """
        Loops through extra cart fields and updates the
        total value of the cart.
        """
        fields = self.get_extra_cart_price_field(cart, request)
        for field in fields:
            price = field[1]
            new_total = cart.current_total + price
            cart.current_total = new_total if new_total > 0 else 0
            cart.extra_price_fields.append(field)
        return cart

    def get_extra_cart_item_price_field(self, cart_item, request):
        """
        Returns all modifiers for the cart item.
        """
        fields = []
        for mod in cart_item.product.get_modifiers():
            field = mod.get_extra_cart_item_price_field(cart_item)
            if field:
                fields.append(field)
        return fields

    def get_extra_cart_price_field(self, cart, request):
        """
        Returns all cart modifiers.
        """
        fields = []

# Path: catalog/models.py
# class Modifier(TranslatableModel, CatalogModel):
#     """
#     Defines different amounts to be applied to a product or a category.
#     """
#     KIND_STANDARD = 'standard'
#     KIND_DISCOUNT = 'discount'
#     KIND_CART_MODIFIER = 'cart_modifier'
#     KIND_CHOICES = (
#         (KIND_STANDARD, _('Standard')),
#         (KIND_DISCOUNT, _('Discount')),
#         (KIND_CART_MODIFIER, _('Cart modifier')),
#     )
# 
#     code = models.SlugField(
#         _('Code'), max_length=128, unique=True,
#         help_text=scs.SLUG_FIELD_HELP_TEXT)
# 
#     amount = CurrencyField(
#         verbose_name=_('Amount'),
#         help_text=_('Absolute amount that should be applied. '
#                     'Can be negative.'))
# 
#     percent = models.DecimalField(
#         _('Percent'), blank=True, null=True,
#         max_digits=4, decimal_places=2,
#         help_text=_('If percent is set, it will override the absolute '
#                     'amount. Can be negative.'))
# 
#     kind = models.CharField(
#         _('Kind'), max_length=128, choices=KIND_CHOICES, default=KIND_STANDARD,
#         help_text=_('Select a modifier kind. If "Cart modifier" is selected, '
#                     'modifier will be constant and affect entire cart.'))
# 
#     translations = TranslatedFields(
#         name=models.CharField(_('Name'), max_length=128),
#     )
# 
#     objects = CatalogManager()
# 
#     class Meta:
#         db_table = 'catalog_modifiers'
#         verbose_name = _('Modifier')
#         verbose_name_plural = _('Modifiers')
# 
#     def get_name(self):
#         return self.safe_translation_getter('name')
# 
#     def get_slug(self):
#         return self.code
# 
#     def get_extra_cart_item_price_field(self, cart_item, request=None):
#         """
#         Returns extra price field for the given cart item.
#         """
#         if self.can_be_applied(cart_item=cart_item, request=request):
#             return (self.get_name(), self.calculate_add_price(
#                 cart_item.current_total, cart_item.quantity))
# 
#     def get_extra_cart_price_field(self, cart, request=None):
#         """
#         Returns extra price field for entire cart.
#         """
#         if self.can_be_applied(cart=cart, request=request):
#             return (self.get_name(), self.calculate_add_price(
#                 cart.current_total))
# 
#     def calculate_add_price(self, price, quantity=1):
#         """
#         Calculates and returns an amount to be added to the given price.
#         """
#         if self.percent:
#             add_price = (self.percent / 100) * price
#         else:
#             add_price = self.amount * quantity
#         return add_price
# 
#     def can_be_applied(self, cart_item=None, cart=None, request=None):
#         """
#         Checks if this modifier can be applied or not.
#         """
#         # Check that product is eligible if cart_item is specified.
#         if cart_item and not self.is_eligible_product(cart_item.product):
#             return False
# 
#         # Check that all conditions are met.
#         for con in self.conditions.select_related().all():
#             if not con.is_met(cart_item=cart_item, cart=cart, request=request):
#                 return False
# 
#         cart_id = cart_item.cart_id if cart_item else cart.id if cart else None
#         return self.is_code_applied(cart_id)
# 
#     def is_code_applied(self, cart_id):
#         """
#         Check for codes on this modifier, and if they exist make sure
#         that at least one is applied to the given cart. Codes have
#         alerady been validated so we just have to make sure they are
#         active and exist.
#         """
#         codes = self.codes.active()
# 
#         if codes.exists():
#             cart_codes = CartModifierCode.objects.filter(cart_id=cart_id)
#             cart_codes = list(set(cart_codes.values_list('code', flat=True)))
#             if cart_codes:
#                 for code in cart_codes:
#                     if codes.filter(code=code).exists():
#                         return True
# 
#             # Codes exist but not one is applied, return False.
#             return False
# 
#         # Codes don't exist, return True.
#         return True
# 
#     def is_eligible_product(self, product):
#         """
#         Returns if modifier can be applied to a given product.
#         """
#         if self.kind == self.KIND_DISCOUNT:
#             return product.is_discountable
#         return self.kind != self.KIND_CART_MODIFIER
# 
#     @classmethod
#     def get_cart_modifiers(cls):
#         """
#         Returns all active cart modifiers.
#         """
#         return cls.objects.active(kind=cls.KIND_CART_MODIFIER)


# Path: catalog/cart_modifiers.py
from shop.cart.cart_modifiers_base import BaseCartModifier
from catalog.models import Modifier


    def process_cart(self, cart, request):
        """
        Loops through extra cart fields and updates the
        total value of the cart.
        """
        fields = self.get_extra_cart_price_field(cart, request)
        for field in fields:
            price = field[1]
            new_total = cart.current_total + price
            cart.current_total = new_total if new_total > 0 else 0
            cart.extra_price_fields.append(field)
        return cart

    def get_extra_cart_item_price_field(self, cart_item, request):
        """
        Returns all modifiers for the cart item.
        """
        fields = []
        for mod in cart_item.product.get_modifiers():
            field = mod.get_extra_cart_item_price_field(cart_item)
            if field:
                fields.append(field)
        return fields

    def get_extra_cart_price_field(self, cart, request):
        """
        Returns all cart modifiers.
        """
        fields = []

        for mod in Modifier.get_cart_modifiers():