diff --git a/core/lib/Thelia/Condition/Implementation/CartContainsProducts.php b/core/lib/Thelia/Condition/Implementation/CartContainsProducts.php new file mode 100644 index 000000000..b4c7ceb81 --- /dev/null +++ b/core/lib/Thelia/Condition/Implementation/CartContainsProducts.php @@ -0,0 +1,196 @@ + + * + */ +class CartContainsProducts extends ConditionAbstract +{ + /** Condition 1st parameter : quantity */ + const PRODUCTS_LIST = 'products'; + + /** + * @inheritdoc + */ + public function __construct(FacadeInterface $facade) + { + $this->availableOperators = [ + self::PRODUCTS_LIST => [ + Operators::IN, + Operators::OUT + ] + ]; + + parent::__construct($facade); + } + + /** + * @inheritdoc + */ + public function getServiceId() + { + return 'thelia.condition.cart_contains_products'; + } + + /** + * @inheritdoc + */ + public function setValidatorsFromForm(array $operators, array $values) + { + $this->checkComparisonOperatorValue($operators, self::PRODUCTS_LIST); + + // Use default values if data is not defined. + if (! isset($operators[self::PRODUCTS_LIST]) || ! isset($values[self::PRODUCTS_LIST])) { + $operators[self::PRODUCTS_LIST] = Operators::IN; + $values[self::PRODUCTS_LIST] = []; + } + + // Be sure that the value is an array, make one if required + if (! is_array($values[self::PRODUCTS_LIST])) { + $values[self::PRODUCTS_LIST] = array($values[self::PRODUCTS_LIST]); + } + + // Check that at least one product is selected + if (empty($values[self::PRODUCTS_LIST])) { + throw new InvalidConditionValueException( + get_class(), self::PRODUCTS_LIST + ); + } + + $this->operators = [ self::PRODUCTS_LIST => $operators[self::PRODUCTS_LIST] ]; + $this->values = [ self::PRODUCTS_LIST => $values[self::PRODUCTS_LIST] ]; + + return $this; + } + + /** + * @inheritdoc + */ + public function isMatching() + { + $cartItems = $this->facade->getCart()->getCartItems(); + + /** @var CartItem $cartItem */ + foreach($cartItems as $cartItem) { + + if ($this->conditionValidator->variableOpComparison( + $cartItem->getProduct()->getId(), + $this->operators[self::PRODUCTS_LIST], + $this->values[self::PRODUCTS_LIST])) { + + return true; + } + } + + return false; + } + + /** + * @inheritdoc + */ + public function getName() + { + return $this->translator->trans( + 'Cart contains specific products', + [], + 'condition' + ); + } + + /** + * @inheritdoc + */ + public function getToolTip() + { + $toolTip = $this->translator->trans( + 'The coupon applies if the cart contains at least one product of the specified product list', + [], + 'condition' + ); + + return $toolTip; + } + + /** + * @inheritdoc + */ + public function getSummary() + { + $i18nOperator = Operators::getI18n( + $this->translator, $this->operators[self::PRODUCTS_LIST] + ); + + $prodStrList = ''; + + $prodIds = $this->values[self::PRODUCTS_LIST]; + + if (null !== $prodList = ProductQuery::create()->findPks($prodIds)) { + + /** @var Category $prod */ + foreach($prodList as $prod) { + $prodStrList .= $prod->getTitle() . ', '; + } + + $prodStrList = rtrim($prodStrList, ', '); + } + + $toolTip = $this->translator->trans( + 'Cart contains at least a product %op% %products_list%', [ + '%products_list%' => $prodStrList, + '%op%' => $i18nOperator + ], 'condition' + ); + + return $toolTip; + } + + /** + * @inheritdoc + */ + protected function generateInputs() + { + return array( + self::PRODUCTS_LIST => array( + 'availableOperators' => $this->availableOperators[self::PRODUCTS_LIST], + 'value' => '', + 'selectedOperator' => Operators::IN + ) + ); + } + + /** + * @inheritdoc + */ + public function drawBackOfficeInputs() + { + return $this->facade->getParser()->render('coupon/condition-fragments/cart-contains-products-condition.html', [ + 'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::PRODUCTS_LIST), + 'products_field_name' => self::PRODUCTS_LIST, + 'values' => isset($this->values[self::PRODUCTS_LIST]) ? $this->values[self::PRODUCTS_LIST] : array() + ] + ); + } +} diff --git a/templates/backOffice/default/coupon/condition-fragments/cart-contains-products-condition.html b/templates/backOffice/default/coupon/condition-fragments/cart-contains-products-condition.html new file mode 100644 index 000000000..9038a2a10 --- /dev/null +++ b/templates/backOffice/default/coupon/condition-fragments/cart-contains-products-condition.html @@ -0,0 +1,14 @@ +
+ + {$operatorSelectHtml nofilter} +
+ +
+ + + {intl l='Use Ctrl+click to select (or deselect) more that one product'} +
\ No newline at end of file