def _get_price_info(shop, product=None, quantity=2): if not product: product = Product(sku='6.0745') # SKU of product defines the price :) price = shop.create_price(product.sku) return PriceInfo(quantity * price, quantity * 4 * price, quantity)
def price_info(self): return PriceInfo(self.price, self.base_price, quantity=1)
def discount_price(self, context, product, price_info): shop = context.shop potential_discounts = get_potential_discounts_for_product( context, product).values_list( "discounted_price_value", "discount_amount_value", "discount_percentage", ) discounted_prices = [] for discounted_price_value, discount_amount_value, discount_percentage in potential_discounts: if discounted_price_value: # Applies the new product price per item discounted_prices.append( min( price_info.price, max( shop.create_price(discounted_price_value) * price_info.quantity, shop.create_price(0)), )) if discount_amount_value: # Discount amount value per item discounted_prices.append( max( price_info.price - shop.create_price(discount_amount_value) * price_info.quantity, shop.create_price(0), )) if discount_percentage: # Discount percentage per item discounted_prices.append( max( price_info.price - price_info.price * discount_percentage, shop.create_price(0))) new_price_info = PriceInfo( price=price_info.price, base_price=price_info.base_price, quantity=price_info.quantity, expires_on=price_info.expires_on, ) if discounted_prices: product_id = product if isinstance( product, six.integer_types) else product.pk minimum_price_values = list( ShopProduct.objects.filter(product_id=product_id, shop=shop).values_list( "minimum_price_value", flat=True)) minimum_price_value = minimum_price_values[ 0] if minimum_price_values else 0 new_price_info.price = max( min(discounted_prices), shop.create_price(minimum_price_value or 0) or shop.create_price(0)) price_expiration = get_price_expiration(context, product) if price_expiration and (not price_info.expires_on or price_expiration < price_info.expires_on): new_price_info.expires_on = price_expiration return new_price_info
def get_price_info_mock(context, product, quantity=1): if context.customer.get_default_group() == AnonymousContact().get_default_group(): price = context.shop.create_price(anonymous_price) else: price = context.shop.create_price(customer_price) return PriceInfo(quantity * price, quantity * price, quantity)
def _get_price_info(price_cls, quantity=2): price = quantity * price_cls(240, 'USD') base_price = quantity * price_cls(330, 'USD') return PriceInfo(price, base_price, quantity)
def test_discount_percentage_special_cases(): pi1 = PriceInfo(price(10), price(0), quantity=1) assert pi1.discount_percentage == 0
def test_with_discounts(): pi = PriceInfo(price(75), price(100), 1) assert pi.is_discounted assert pi.discount_percentage == 25 assert pi.discount_amount == price(25)
def test_no_discount(): pi = PriceInfo(price(100), price(100), 1) assert not pi.is_discounted assert pi.discount_percentage == 0 assert pi.discount_amount == price(0)
def get_price_info(self, context, product, quantity=1): product_id = (product if isinstance(product, six.integer_types) else product.pk) shop = context.shop # By default let's use supplier passed to context. supplier = context.supplier if not supplier: # Since supplier is optional I am pretty sure # there is cases that supplier is not passed to # pricing context. This is not a problem. The # pricing module which decides to use supplier # for product prices mjust need to have some sane # fallback. supplier_strategy = cached_load( "SHUUP_SHOP_PRODUCT_SUPPLIERS_STRATEGY") kwargs = { "product_id": product_id, "shop": context.shop, "customer": context.customer, "quantity": quantity, "basket": context.basket } # Since this is custom pricing module it # requires also custom supplier strategy. # Some example is provided in # `shuup.testing.supplier_pricing.supplier_strategy:CheapestSupplierPriceSupplierStrategy` supplier = supplier_strategy().get_supplier(**kwargs) # Like now in customer group pricing let's take default price from shop product default_price_values = list( ShopProduct.objects.filter(product_id=product_id, shop=shop).values_list( "default_price_value", flat=True)) if len(default_price_values) == 0: # No shop product return PriceInfo(price=shop.create_price(0), base_price=shop.create_price(0), quantity=quantity) else: default_price = default_price_values[0] or 0 # Then the actual supplier price in case we have # been able to figure out some supplier. I guess # it is problem for supplier strategy if it allows # supplier to be None in some weird scenarios. # Not sure though what would happen in shop product # orderability checks and so on. price = None if supplier: result = SupplierPrice.objects.filter( shop=shop, product_id=product_id, supplier=supplier).order_by("amount_value")[:1].values_list( "amount_value", flat=True) if result: price = result[0] if not price: price = default_price return PriceInfo( price=shop.create_price(price * quantity), base_price=shop.create_price(price * quantity), quantity=quantity, )