{% sw_extends '@Storefront/storefront/page/checkout/cart/index.html.twig' %}
{% block page_checkout_cart_add_product_and_shipping %}
{% set extensionFreeProducts = page.extensions['CogiFreeProducts'] %}
{% set FreeProducts = extensionFreeProducts.get('FreeProducts') %}
{% set totalPrice = cart_product_prices(context, page.cart, config('CogiFreeProducts.config.cogiFreeProductsCartPriceWithoutPromotions'), config('CogiFreeProducts.config.cogiFreeProductsExcludeFreeShippingProducts')) %}
{% set break = false %}
{% set isManufacturerInCart = false %}
{% set manufacturerPrice = 0 %}
{% set customerHasFreeProduct = false %}
{% set isFreeProductInCart = false %}
{% if config('CogiFreeProducts.config.cogiFreeProductsOneProductPerCustomer') and (config('CogiFreeProducts.config.cogiFreeProductsAutoToCart') == false) %}
{% set isFreeProductInCart = false %}
{% for item in page.cart.lineItems %}
{% if item.extensions["FreeProduct"] %}
{% set isFreeProductInCart = true %}
{% endif %}
{% endfor %}
{% if isFreeProductInCart %}
{% set message = 'CogiFreeProducts.cartInfoChangeFreeProduct'|trans %}
{% set messageTypeInfo = 'info' %}
{% sw_include "@Storefront/storefront/utilities/alert.html.twig" with { 'type': messageTypeInfo, 'content': message} %}
{% endif %}
{% endif %}
{% for freeProduct in FreeProducts %}
{% if freeProduct.manufacturerActive %}
{% set freeProductManufacturerNames = get_manufacturer_names(context ,freeProduct.productManufacturerIds) %}
{% endif %}
{% if freeProduct.categoryActive %}
{% set freeProductCategoryNames = get_category_names(context, freeProduct.categoryIds) %}
{% endif %}
{% for lineItem in page.cart.lineItems %}
{% for productManufacturerId in freeProduct.productManufacturerIds %}
{% if lineItem.payload['manufacturerId'] == productManufacturerId and freeProduct.manufacturerActive %}
{% set manufacturerPrice = manufacturerPrice + (lineItem.price.totalPrice * context.currency.factor) %}
{% endif %}
{% endfor %}
{% endfor %}
{% if manufacturerPrice > totalPrice %}
{% set isManufacturerInCart = true %}
{% endif %}
{% set isCategoryInCart = false %}
{% set categoryPrice = 0 %}
{% for lineItem in page.cart.lineItems %}
{% for productCategoryId in freeProduct.categoryIds %}
{% for categoryId in lineItem.payload['categoryIds'] %}
{% if categoryId == productCategoryId and freeProduct.categoryActive %}
{% set categoryPrice = categoryPrice + (lineItem.price.totalPrice * context.currency.factor) %}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
{% if categoryPrice > totalPrice %}
{% set isCategoryInCart = true %}
{% endif %}
{% set isSalesChannel = false %}
{% for salesChannelId in freeProduct.salesChannelIds %}
{% if context.salesChannel.id == salesChannelId %}
{% set isSalesChannel = true %}
{% endif %}
{% endfor %}
{% if check_customer_has_free_product(context) and config('CogiFreeProducts.config.cogiFreeProductsOneProductPerCustomer') %}
{% set customerHasFreeProduct = true %}
{% endif %}
{% if isSalesChannel and (customerHasFreeProduct != true) and config('CogiFreeProducts.config.cogiFreeProductsCartHint') == false %}
{% if (freeProduct.cartPrice * context.currency.factor) > manufacturerPrice and break == false and freeProduct.manufacturerActive and isManufacturerInCart == false %}
{% set message = 'CogiFreeProducts.cartInfoManufacturer'|trans({'%price%': ((freeProduct.cartPrice * context.currency.factor)|round(2, 'floor') - manufacturerPrice)|currency, '%manufacturerName%': freeProductManufacturerNames })|raw %}
{% set messageTypeInfo = 'info' %}
{% sw_include "@Storefront/storefront/utilities/alert.html.twig" with { 'type': messageTypeInfo, 'content': message} %}
{% set break = true %}
{% elseif (freeProduct.cartPrice * context.currency.factor) > categoryPrice and break == false and freeProduct.categoryActive and isCategoryInCart == false %}
{% set message = 'CogiFreeProducts.cartInfoCategory'|trans({'%price%': ((freeProduct.cartPrice * context.currency.factor)|round(2, 'floor') - categoryPrice)|currency, '%categoryName%': freeProductCategoryNames })|raw %}
{% set messageTypeInfo = 'info' %}
{% sw_include "@Storefront/storefront/utilities/alert.html.twig" with { 'type': messageTypeInfo, 'content': message} %}
{% set break = true %}
{% elseif (freeProduct.cartPrice * context.currency.factor) > totalPrice and break == false %}
{% set message = 'CogiFreeProducts.cartInfo'|trans({'%price%': ((freeProduct.cartPrice * context.currency.factor)|round(2, 'floor') - totalPrice)|currency })|raw %}
{% set messageTypeInfo = 'info' %}
{% sw_include "@Storefront/storefront/utilities/alert.html.twig" with { 'type': messageTypeInfo, 'content': message} %}
{% set break = true %}
{% endif %}
{% endif %}
{% set isManufacturerInCart = false %}
{% endfor %}
{{ parent() }}
{% endblock %}
{% block page_checkout_cart_add_promotion %}
{% set isFreeProduct = false %}
{% set configPromoCode = config('CogiFreeProducts.config.cogiFreeProductsPromoCode') %}
{% if configPromoCode == false %}
{% for item in page.cart.lineItems %}
{% if item.extensions["FreeProduct"] %}
{% set isFreeProduct = true %}
{% else %}
{% set isFreeProduct = false %}
{% endif %}
{% endfor %}
{% endif %}
{% if isFreeProduct == false %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block page_checkout_cart_action_proceed %}
{{ parent() }}
{% endblock %}