def test_product_catalog_campaigns2(): shop = get_default_shop() product = create_product("test", shop, default_price=20) product_type = ProductType.objects.create(name="asdf") shop_product = product.get_shop_instance(shop) campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) assert CatalogCampaign.get_for_product(shop_product).count() == 0 type_filter = ProductTypeFilter.objects.create() type_filter.product_types.add(product_type) campaign.filters.add(type_filter) assert CatalogCampaign.get_for_product(shop_product).count() == 0 type_filter.product_types.add(product.type) assert type_filter.matches(shop_product) assert CatalogCampaign.get_for_product(shop_product).count() == 1 product.type = product_type product.save() assert type_filter.matches(shop_product) assert CatalogCampaign.get_for_product(shop_product).count() == 1 type_filter.product_types.remove(product_type) assert not type_filter.matches(shop_product) assert CatalogCampaign.get_for_product(shop_product).count() == 0
def test_product_catalog_campaigns3(): shop = get_default_shop() product = create_product("test", shop, default_price=20) shop_product = product.get_shop_instance(shop) campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) assert CatalogCampaign.get_for_product(shop_product).count() == 0 type_filter = ProductFilter.objects.create() type_filter.products.add(product) campaign.filters.add(type_filter) assert CatalogCampaign.get_for_product(shop_product).count() == 1
def discount_price(self, context, product, price_info): """ Get the discounted price for context. Best discount is selected. Minimum price will be selected if the cheapest price is under that. """ create_price = context.shop.create_price shop_product = product.get_shop_instance(context.shop) best_discount = None for campaign in CatalogCampaign.get_matching(context, shop_product): price = price_info.price # get first matching effect for effect in campaign.effects.all(): price -= effect.apply_for_product(context=context, product=product, price_info=price_info) if best_discount is None: best_discount = price if price < best_discount: best_discount = price if best_discount: if shop_product.minimum_price and best_discount < shop_product.minimum_price: best_discount = shop_product.minimum_price if best_discount < create_price("0"): best_discount = create_price("0") price_info.price = best_discount return price_info
def test_product_catalog_campaigns2(): shop = get_default_shop() product = create_product("test", shop, default_price=20) product_type = ProductType.objects.create(name="asdf") shop_product = product.get_shop_instance(shop) campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) assert CatalogCampaign.get_for_product(shop_product).count() == 0 type_filter = ProductTypeFilter.objects.create() type_filter.product_types.add(product_type) campaign.filters.add(type_filter) assert CatalogCampaign.get_for_product(shop_product).count() == 0 type_filter.product_types.add(product.type) assert CatalogCampaign.get_for_product(shop_product).count() == 1 product.type = product_type product.save() assert CatalogCampaign.get_for_product(shop_product).count() == 1 type_filter.product_types.remove(product_type) assert CatalogCampaign.get_for_product(shop_product).count() == 0
def discount_price(self, context, product, price_info: PriceInfo): """ Get the discounted price for context. Best discount is selected. Minimum price will be selected if the cheapest price is under that. """ create_price = context.shop.create_price shop_product = ShopProduct.objects.filter(shop=context.shop, product=product).first() if not shop_product: return price_info best_discount = None for campaign in CatalogCampaign.get_matching(context, shop_product): price = price_info.price # get first matching effect for effect in campaign.effects.all(): price -= effect.apply_for_product(context=context, product=product, price_info=price_info) if best_discount is None: best_discount = price if price < best_discount: best_discount = price if not best_discount: return price_info if shop_product.minimum_price and best_discount < shop_product.minimum_price: best_discount = shop_product.minimum_price if best_discount < create_price("0"): best_discount = create_price("0") return PriceInfo( best_discount, base_price=price_info.base_price, quantity=price_info.quantity, expires_on=price_info.expires_on, )
def test_product_catalog_campaigns(): shop = get_default_shop() product = create_product("test", shop, default_price=20) shop_product = product.get_shop_instance(shop) cat = Category.objects.create(name="test") campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) # no rules assert CatalogCampaign.get_for_product(shop_product).count() == 0 # category filter that doesn't match cat_filter = CategoryFilter.objects.create() cat_filter.categories.add(cat) campaign.filters.add(cat_filter) assert CatalogCampaign.get_for_product(shop_product).count() == 0 shop_product.primary_category = cat shop_product.save() assert CatalogCampaign.get_for_product(shop_product).count() == 1 shop_product.primary_category = None shop_product.save() assert CatalogCampaign.get_for_product(shop_product).count() == 0 # category filter that matches shop_product.categories.add(cat) assert CatalogCampaign.get_for_product(shop_product).count() == 1 # create other shop shop1 = Shop.objects.create(name="testshop", identifier="testshop", status=ShopStatus.ENABLED, public_name="testshop") sp = ShopProduct.objects.create(product=product, shop=shop1, default_price=shop1.create_price(200)) assert product.get_shop_instance(shop1) == sp campaign2 = CatalogCampaign.objects.create(shop=shop1, name="test1", active=True) cat_filter2 = CategoryFilter.objects.create() cat_filter2.categories.add(cat) campaign2.filters.add(cat_filter2) assert CatalogCampaign.get_for_product(sp).count() == 0 # add product to this category sp.primary_category = cat sp.save() assert CatalogCampaign.get_for_product(sp).count() == 1 # matches now sp.primary_category = None sp.save() assert CatalogCampaign.get_for_product(sp).count() == 0 # no match sp.categories.add(cat) assert CatalogCampaign.get_for_product(sp).count() == 1 # matches now campaign3 = CatalogCampaign.objects.create(shop=shop1, name="test1", active=True) cat_filter3 = CategoryFilter.objects.create() cat_filter3.categories.add(cat) campaign3.filters.add(cat_filter3) assert CatalogCampaign.get_for_product(sp).count() == 2 # there are now two matching campaigns in same shop assert CatalogCampaign.get_for_product(shop_product).count() == 1 # another campaign matches only once
def test_product_catalog_campaigns(): shop = get_default_shop() product = create_product("test", shop, default_price=20) parent_product = create_product("parent", shop, default_price=40) no_shop_child = create_product("child-no-shop") shop_child = create_product("child-shop", shop, default_price=60) shop_child.link_to_parent(parent_product) no_shop_child.link_to_parent(parent_product) shop_product = product.get_shop_instance(shop) parent_shop_product = parent_product.get_shop_instance(shop) child_shop_product =shop_child.get_shop_instance(shop) cat = Category.objects.create(name="test") campaign = CatalogCampaign.objects.create(shop=shop, name="test", active=True) # no rules assert CatalogCampaign.get_for_product(shop_product).count() == 0 assert CatalogCampaign.get_for_product(parent_shop_product).count() == 0 assert CatalogCampaign.get_for_product(child_shop_product).count() == 0 # category filter that doesn't match cat_filter = CategoryFilter.objects.create() cat_filter.categories.add(cat) campaign.filters.add(cat_filter) assert CatalogCampaign.get_for_product(shop_product).count() == 0 assert CatalogCampaign.get_for_product(parent_shop_product).count() == 0 assert CatalogCampaign.get_for_product(child_shop_product).count() == 0 for sp in [shop_product, parent_shop_product, child_shop_product]: sp.primary_category = cat sp.save() assert CatalogCampaign.get_for_product(sp).count() == 1 sp.categories.remove(cat) sp.primary_category = None sp.save() assert CatalogCampaign.get_for_product(sp).count() == 0 # category filter that matches sp.categories.add(cat) assert CatalogCampaign.get_for_product(sp).count() == 1 # create other shop shop1 = Shop.objects.create(name="testshop", identifier="testshop", status=ShopStatus.ENABLED, public_name="testshop") sp = ShopProduct.objects.create(product=product, shop=shop1, default_price=shop1.create_price(200)) assert product.get_shop_instance(shop1) == sp campaign2 = CatalogCampaign.objects.create(shop=shop1, name="test1", active=True) cat_filter2 = CategoryFilter.objects.create() cat_filter2.categories.add(cat) campaign2.filters.add(cat_filter2) assert CatalogCampaign.get_for_product(sp).count() == 0 # add product to this category sp.primary_category = cat sp.save() assert CatalogCampaign.get_for_product(sp).count() == 1 # matches now sp.categories.remove(cat) sp.primary_category = None sp.save() assert CatalogCampaign.get_for_product(sp).count() == 0 # no match sp.categories.add(cat) assert CatalogCampaign.get_for_product(sp).count() == 1 # matches now campaign3 = CatalogCampaign.objects.create(shop=shop1, name="test1", active=True) cat_filter3 = CategoryFilter.objects.create() cat_filter3.categories.add(cat) campaign3.filters.add(cat_filter3) assert CatalogCampaign.get_for_product(sp).count() == 2 # there are now two matching campaigns in same shop assert CatalogCampaign.get_for_product(shop_product).count() == 1 # another campaign matches only once