Refactored coupons to reduce code duplication

This commit is contained in:
Franck Allimant
2014-06-12 13:14:46 +02:00
parent db29505fee
commit 95841809c4
13 changed files with 502 additions and 550 deletions

View File

@@ -0,0 +1,150 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CartItem;
/**
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
abstract class AbstractRemoveOnCategories extends CouponAbstract
{
const CATEGORIES_LIST = 'categories';
protected $category_list = array();
/**
* Set the value of specific coupon fields.
*
* @param Array $effects the Coupon effects params
*/
protected abstract function setFieldsValue($effects);
/**
* Get the discount for a specific cart item.
*
* @param CartItem $cartItem the cart item
* @return float the discount value
*/
protected abstract function getCartItemDiscount($cartItem);
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
$this->category_list = isset($effects[self::CATEGORIES_LIST]) ? $effects[self::CATEGORIES_LIST] : array();
if (! is_array($this->category_list)) $this->category_list = array($this->category_list);
return $this;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected categories.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$categories = $cartItem->getProduct()->getCategories();
/** @var Category $category */
foreach ($categories as $category) {
if (in_array($category->getId(), $this->category_list)) {
$discount += $this->getCartItemDiscount($cartItem);
break;
}
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBaseBackOfficeInputs($templateName, $otherFields)
{
return $this->facade->getParser()->render($templateName, array_merge($otherFields, [
// The categories list field
'categories_field_name' => $this->makeCouponFieldName(self::CATEGORIES_LIST),
'categories_values' => $this->category_list
]));
}
/**
* @inheritdoc
*/
protected function getBaseFieldList($otherFields)
{
return array_merge($otherFields, [self::CATEGORIES_LIST]);
}
/**
* @inheritdoc
*/
protected function checkBaseCouponFieldValue($fieldName, $fieldValue)
{
if ($fieldName === self::CATEGORIES_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one category'
)
);
}
}
return $fieldValue;
}
}

View File

@@ -0,0 +1,162 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Coupon\Type;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Model\CartItem;
/**
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Franck Allimant <franck@cqfdev.fr>
*/
abstract class AbstractRemoveOnProducts extends CouponAbstract
{
const CATEGORY_ID = 'category_id';
const PRODUCTS_LIST = 'products';
public $category_id = 0;
public $product_list = array();
/**
* Set the value of specific coupon fields.
*
* @param Array $effects the Coupon effects params
*/
protected abstract function setFieldsValue($effects);
/**
* Get the discount for a specific cart item.
*
* @param CartItem $cartItem the cart item
* @return float the discount value
*/
protected abstract function getCartItemDiscount($cartItem);
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
$this->product_list = isset($effects[self::PRODUCTS_LIST]) ? $effects[self::PRODUCTS_LIST] : array();
if (! is_array($this->product_list)) $this->product_list = array($this->product_list);
$this->category_id = isset($effects[self::CATEGORY_ID]) ? $effects[self::CATEGORY_ID] : 0;
$this->setFieldsValue($effects);
return $this;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected products.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (in_array($cartItem->getProduct()->getId(), $this->product_list)) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$discount += $this->getCartItemDiscount($cartItem);
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBaseBackOfficeInputs($templateName, $otherFields)
{
return $this->facade->getParser()->render($templateName, array_merge($otherFields, [
// The category ID field
'category_id_field_name' => $this->makeCouponFieldName(self::CATEGORY_ID),
'category_id_value' => $this->category_id,
// The products list field
'products_field_name' => $this->makeCouponFieldName(self::PRODUCTS_LIST),
'products_values' => $this->product_list,
'products_values_csv' => implode(', ', $this->product_list)
]));
}
/**
* @inheritdoc
*/
protected function getBaseFieldList($otherFields)
{
return array_merge($otherFields, [self::CATEGORY_ID, self::PRODUCTS_LIST]);
}
/**
* @inheritdoc
*/
protected function checkBaseCouponFieldValue($fieldName, $fieldValue)
{
if ($fieldName === self::CATEGORY_ID) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select a category'
)
);
}
} elseif ($fieldName === self::PRODUCTS_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one product'
)
);
}
}
return $fieldValue;
}
}

View File

@@ -21,56 +21,30 @@ use Thelia\Model\Category;
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemoveAmountOnCategories extends CouponAbstract
class RemoveAmountOnCategories extends AbstractRemoveOnCategories
{
const CATEGORIES_LIST = 'categories';
/** @var string Service Id */
protected $serviceId = 'thelia.coupon.type.remove_amount_on_categories';
public $category_list = array();
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
$this->category_list = isset($effects[self::CATEGORIES_LIST]) ? $effects[self::CATEGORIES_LIST] : array();
if (! is_array($this->category_list)) $this->category_list = array($this->category_list);
return $this;
protected function setFieldsValue($effects) {
// Nothing to do, the amount is processed by CouponAbstract.
}
/**
* @inheritdoc
*/
public function getCartItemDiscount($cartItem) {
return $cartItem->getQuantity() * $this->amount;
}
/**
* @inheritdoc
*/
public function getName()
{
return $this->facade
@@ -94,53 +68,15 @@ class RemoveAmountOnCategories extends CouponAbstract
return $toolTip;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected categories.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$categories = $cartItem->getProduct()->getCategories();
/** @var Category $category */
foreach ($categories as $category) {
if (in_array($category->getId(), $this->category_list)) {
$discount += $cartItem->getQuantity() * $this->amount;
break;
}
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
return $this->facade->getParser()->render('coupon/type-fragments/remove-amount-on-categories.html', [
// The standard "Amount" field
'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME),
'amount_value' => $this->amount,
// The categories list field
'categories_field_name' => $this->makeCouponFieldName(self::CATEGORIES_LIST),
'categories_values' => $this->category_list
]);
return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-amount-on-categories.html', [
'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME),
'amount_value' => $this->amount
]);
}
/**
@@ -148,7 +84,7 @@ class RemoveAmountOnCategories extends CouponAbstract
*/
protected function getFieldList()
{
return [self::AMOUNT_FIELD_NAME, self::CATEGORIES_LIST];
return $this->getBaseFieldList([self::AMOUNT_FIELD_NAME]);
}
/**
@@ -156,6 +92,8 @@ class RemoveAmountOnCategories extends CouponAbstract
*/
protected function checkCouponFieldValue($fieldName, $fieldValue)
{
$this->checkBaseCouponFieldValue($fieldName, $fieldValue);
if ($fieldName === self::AMOUNT_FIELD_NAME) {
if (floatval($fieldValue) < 0) {
@@ -166,17 +104,8 @@ class RemoveAmountOnCategories extends CouponAbstract
)
);
}
} elseif ($fieldName === self::CATEGORIES_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one category'
)
);
}
}
return $fieldValue;
}
}

View File

@@ -21,56 +21,25 @@ use Thelia\Model\Product;
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemoveAmountOnProducts extends CouponAbstract
class RemoveAmountOnProducts extends AbstractRemoveOnProducts
{
const CATEGORY_ID = 'category_id';
const PRODUCTS_LIST = 'products';
/** @var string Service Id */
protected $serviceId = 'thelia.coupon.type.remove_amount_on_products';
public $category_id = 0;
public $product_list = array();
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
protected function setFieldsValue($effects) {
// Nothing to do: amount is processed by CouponAbstract
}
$this->product_list = isset($effects[self::PRODUCTS_LIST]) ? $effects[self::PRODUCTS_LIST] : array();
if (! is_array($this->product_list)) $this->product_list = array($this->product_list);
$this->category_id = isset($effects[self::CATEGORY_ID]) ? $effects[self::CATEGORY_ID] : 0;
return $this;
/**
* @inheritdoc
*/
protected function getCartItemDiscount($cartItem) {
return $cartItem->getQuantity() * $this->amount;
}
/**
@@ -101,58 +70,23 @@ class RemoveAmountOnProducts extends CouponAbstract
return $toolTip;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected products.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (in_array($cartItem->getProduct()->getId(), $this->product_list)) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$discount += $cartItem->getQuantity() * $this->amount;
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
return $this->facade->getParser()->render('coupon/type-fragments/remove-amount-on-products.html', [
// The standard "Amount" field
'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME),
'amount_value' => $this->amount,
// The category ID field
'category_id_field_name' => $this->makeCouponFieldName(self::CATEGORY_ID),
'category_id_value' => $this->category_id,
// The products list field
'products_field_name' => $this->makeCouponFieldName(self::PRODUCTS_LIST),
'products_values' => $this->product_list,
'products_values_csv' => implode(', ', $this->product_list)
return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-amount-on-products.html', [
'amount_field_name' => $this->makeCouponFieldName(self::AMOUNT_FIELD_NAME),
'amount_value' => $this->amount
]);
}
}
/**
* @inheritdoc
*/
protected function getFieldList()
{
return [self::AMOUNT_FIELD_NAME, self::CATEGORY_ID, self::PRODUCTS_LIST];
return $this->getBaseFieldList([self::AMOUNT_FIELD_NAME]);
}
/**
@@ -160,6 +94,8 @@ class RemoveAmountOnProducts extends CouponAbstract
*/
protected function checkCouponFieldValue($fieldName, $fieldValue)
{
$this->checkBaseCouponFieldValue($fieldName, $fieldValue);
if ($fieldName === self::AMOUNT_FIELD_NAME) {
if (floatval($fieldValue) < 0) {
@@ -170,24 +106,8 @@ class RemoveAmountOnProducts extends CouponAbstract
)
);
}
} elseif ($fieldName === self::CATEGORY_ID) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select a category'
)
);
}
} elseif ($fieldName === self::PRODUCTS_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one product'
)
);
}
}
return $fieldValue;
}
}
}

View File

@@ -19,54 +19,28 @@ use Thelia\Model\Category;
/**
* @author Franck Allimant <franck@cqfdev.fr>
*
*/
class RemovePercentageOnCategories extends CouponAbstract
class RemovePercentageOnCategories extends AbstractRemoveOnCategories
{
const CATEGORIES_LIST = 'categories';
const PERCENTAGE = 'percentage';
/** @var string Service Id */
protected $serviceId = 'thelia.coupon.type.remove_percentage_on_categories';
protected $category_list = array();
protected $percentage = 0;
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
protected function setFieldsValue($effects) {
$this->percentage = $effects[self::PERCENTAGE];
}
$this->category_list = isset($effects[self::CATEGORIES_LIST]) ? $effects[self::CATEGORIES_LIST] : array();
$this->percentage = isset($effects[self::PERCENTAGE]) ? $effects[self::PERCENTAGE] : 0;
if (! is_array($this->category_list)) $this->category_list = array($this->category_list);
return $this;
/**
* @inheritdoc
*/
public function getCartItemDiscount($cartItem) {
return $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage;
}
/**
@@ -95,52 +69,15 @@ class RemovePercentageOnCategories extends CouponAbstract
return $toolTip;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected categories.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$categories = $cartItem->getProduct()->getCategories();
/** @var Category $category */
foreach ($categories as $category) {
if (in_array($category->getId(), $this->category_list)) {
$discount += $cartItem->getPrice() * $this->percentage;
break;
}
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
return $this->facade->getParser()->render('coupon/type-fragments/remove-percentage-on-categories.html', [
// The standard "Amount" field
'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE),
'percentage_value' => $this->percentage,
// The categories list field
'categories_field_name' => $this->makeCouponFieldName(self::CATEGORIES_LIST),
'categories_values' => $this->category_list
]);
return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-percentage-on-categories.html', [
'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE),
'percentage_value' => $this->percentage,
]);
}
/**
@@ -148,7 +85,7 @@ class RemovePercentageOnCategories extends CouponAbstract
*/
protected function getFieldList()
{
return [self::PERCENTAGE, self::CATEGORIES_LIST];
return $this->getBaseFieldList([self::PERCENTAGE]);
}
/**
@@ -156,6 +93,8 @@ class RemovePercentageOnCategories extends CouponAbstract
*/
protected function checkCouponFieldValue($fieldName, $fieldValue)
{
$this->checkBaseCouponFieldValue($fieldName, $fieldValue);
if ($fieldName === self::PERCENTAGE) {
$pcent = floatval($fieldValue);
@@ -168,14 +107,6 @@ class RemovePercentageOnCategories extends CouponAbstract
)
);
}
} elseif ($fieldName === self::CATEGORIES_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one category'
)
);
}
}
return $fieldValue;

View File

@@ -21,60 +21,30 @@ use Thelia\Model\Product;
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class RemovePercentageOnProducts extends CouponAbstract
class RemovePercentageOnProducts extends AbstractRemoveOnProducts
{
const CATEGORY_ID = 'category_id';
const PRODUCTS_LIST = 'products';
const PERCENTAGE = 'percentage';
const PERCENTAGE = 'percentage';
/** @var string Service Id */
protected $serviceId = 'thelia.coupon.type.remove_percentage_on_products';
public $category_id = 0;
public $product_list = array();
public $percentage = 0;
/**
* @inheritdoc
*/
public function set(
FacadeInterface $facade,
$code,
$title,
$shortDescription,
$description,
array $effects,
$isCumulative,
$isRemovingPostage,
$isAvailableOnSpecialOffers,
$isEnabled,
$maxUsage,
\DateTime $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
)
{
parent::set(
$facade, $code, $title, $shortDescription, $description, $effects,
$isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate,
$freeShippingForCountries,
$freeShippingForModules,
$perCustomerUsageCount
);
$this->product_list = isset($effects[self::PRODUCTS_LIST]) ? $effects[self::PRODUCTS_LIST] : array();
if (! is_array($this->product_list)) $this->product_list = array($this->product_list);
$this->category_id = isset($effects[self::CATEGORY_ID]) ? $effects[self::CATEGORY_ID] : 0;
protected function setFieldsValue($effects) {
$this->percentage = $effects[self::PERCENTAGE];
}
return $this;
/**
* @inheritdoc
*/
protected function getCartItemDiscount($cartItem) {
return $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage;
}
/**
@@ -105,49 +75,15 @@ class RemovePercentageOnProducts extends CouponAbstract
return $toolTip;
}
/**
* @inheritdoc
*/
public function exec()
{
// This coupon subtracts the specified amount from the order total
// for each product of the selected products.
$discount = 0;
$cartItems = $this->facade->getCart()->getCartItems();
/** @var CartItem $cartItem */
foreach ($cartItems as $cartItem) {
if (in_array($cartItem->getProduct()->getId(), $this->product_list)) {
if (! $cartItem->getPromo() || $this->isAvailableOnSpecialOffers()) {
$discount += $cartItem->getQuantity() * $cartItem->getPrice() * $this->percentage;
}
}
}
return $discount;
}
/**
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
return $this->facade->getParser()->render('coupon/type-fragments/remove-percentage-on-products.html', [
// The standard "Amount" field
'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE),
'percentage_value' => $this->percentage,
// The category ID field
'category_id_field_name' => $this->makeCouponFieldName(self::CATEGORY_ID),
'category_id_value' => $this->category_id,
// The products list field
'products_field_name' => $this->makeCouponFieldName(self::PRODUCTS_LIST),
'products_values' => $this->product_list,
'products_values_csv' => implode(', ', $this->product_list)
]);
return $this->drawBaseBackOfficeInputs('coupon/type-fragments/remove-percentage-on-products.html', [
'percentage_field_name' => $this->makeCouponFieldName(self::PERCENTAGE),
'percentage_value' => $this->percentage,
]);
}
/**
@@ -155,7 +91,7 @@ class RemovePercentageOnProducts extends CouponAbstract
*/
protected function getFieldList()
{
return [self::PERCENTAGE, self::CATEGORY_ID, self::PRODUCTS_LIST];
return $this->getBaseFieldList([self::PERCENTAGE]);
}
/**
@@ -163,6 +99,8 @@ class RemovePercentageOnProducts extends CouponAbstract
*/
protected function checkCouponFieldValue($fieldName, $fieldValue)
{
$this->checkBaseCouponFieldValue($fieldName, $fieldValue);
if ($fieldName === self::PERCENTAGE) {
$pcent = floatval($fieldValue);
@@ -175,24 +113,8 @@ class RemovePercentageOnProducts extends CouponAbstract
)
);
}
} elseif ($fieldName === self::CATEGORY_ID) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select a category'
)
);
}
} elseif ($fieldName === self::PRODUCTS_LIST) {
if (empty($fieldValue)) {
throw new \InvalidArgumentException(
Translator::getInstance()->trans(
'Please select at least one product'
)
);
}
}
return $fieldValue;
}
}
}

View File

@@ -0,0 +1,3 @@
{loop type="attribute_availability" attribute={$smarty.post.attribute_id} name="list-of-attribute_avs" backend_context="1"}
<option value="{$ID}">{$TITLE}</option>
{/loop}

View File

@@ -0,0 +1,14 @@
{block name="discount-field"}{/block}
<div class="form-group input-coupon-categories-id">
<label for="coupon-categories-id">{intl l="Applies to products in categories :"}</label>
<select required multiple size="10" class="form-control" id="coupon-categories-id" name="{$categories_field_name}[]">
{loop type="category-tree" category=0 name="list-of-categories" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $categories_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>

View File

@@ -0,0 +1,63 @@
{block name="discount-field"}{/block}
<div class="form-group input-coupon-category-id">
<label for="coupon-category-id">{intl l="Select product category :"}</label>
<select required class="form-control" id="coupon-category-id" name="{$category_id_field_name}">
<option value="0">{intl l="Please select..."}</option>
{loop type="category-tree" category=0 name="list-of-category" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if $ID == $category_id_value}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
</div>
<div class="loading" id="input-coupon-products-id-loading" style="display: none"></div>
<div class="form-group" id="input-coupon-products-id" {if $category_id_value == 0}style="display: none"{/if}>
<label for="coupon-products-id">{intl l="Applies to products :"}</label>
<select required multiple size="10" class="form-control" id="coupon-products-id" name="{$products_field_name}[]">
{loop type="product" category=$category_id_value name="list-of-products" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $products_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one product'}</span>
</div>
<script>
function couponInputFormSetup() {
var $catSelect = $('#coupon-category-id');
$catSelect.change(function(ev) {
var $category_id = $(this).val();
$('#input-coupon-products-id').hide();
$('#coupon-products-id').html('');
if ($category_id != 0) {
$('#input-coupon-products-id-loading').show();
$.ajax({
url: "{url path='/admin/coupon/type-fragments/ajax-products-list'}",
type: 'POST',
data: {
category_id: $category_id
},
success: function(options) {
$('#coupon-products-id').html(options);
$('#input-coupon-products-id').show();
},
complete: function() {
$('#input-coupon-products-id-loading').hide();
}
});
}
});
};
</script>

View File

@@ -1,26 +1,16 @@
{extends file="coupon/type-fragments/base-remove-on-categories.html"}
<div class="form-group input-coupon-amount">
{block name="discount-field"}
<div class="form-group input-coupon-amount">
<label for="coupon-amount" class="control-label">{intl l="Discount amount"}</label>
<label for="coupon-amount" class="control-label">{intl l="Discount amount"}</label>
<div class="input-group">
<input id="coupon-amount" type="money" class="form-control" name="{$amount_field_name}" value="{$amount_value}" placeholder="14.50">
<div class="input-group">
<input id="coupon-amount" type="money" class="form-control" name="{$amount_field_name}" value="{$amount_value}" placeholder="14.50">
{loop type="currency" name="get-symbol" default_only="true"}
<div class="input-group-addon">{$SYMBOL}</div>
{/loop}
{loop type="currency" name="get-symbol" default_only="true"}
<div class="input-group-addon">{$SYMBOL}</div>
{/loop}
</div>
</div>
</div>
<div class="form-group input-coupon-categories-id">
<label for="coupon-categories-id">{intl l="Applies to products in categories :"}</label>
<select required multiple size="10" class="form-control" id="coupon-categories-id" name="{$categories_field_name}[]">
{loop type="category-tree" category=0 name="list-of-categories" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $categories_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
{/block}

View File

@@ -1,77 +1,16 @@
{extends file="coupon/type-fragments/base-remove-on-products.html"}
<div class="form-group input-coupon-amount">
{block name="discount-field"}
<div class="form-group input-coupon-amount">
<label for="coupon-amount" class="control-label">{intl l="Discount amount"}</label>
<label for="coupon-amount" class="control-label">{intl l="Discount amount"}</label>
<div class="input-group">
<input id="coupon-amount" type="money" class="form-control" name="{$amount_field_name}" value="{$amount_value}" placeholder="14.50">
<div class="input-group">
<input id="coupon-amount" type="money" class="form-control" name="{$amount_field_name}" value="{$amount_value}" placeholder="14.50">
{loop type="currency" name="get-symbol" default_only="true"}
<div class="input-group-addon">{$SYMBOL}</div>
{/loop}
{loop type="currency" name="get-symbol" default_only="true"}
<div class="input-group-addon">{$SYMBOL}</div>
{/loop}
</div>
</div>
</div>
<div class="form-group input-coupon-category-id">
<label for="coupon-category-id">{intl l="Select product category :"}</label>
<select required class="form-control" id="coupon-category-id" name="{$category_id_field_name}">
<option value="0">{intl l="Please select..."}</option>
{loop type="category-tree" category=0 name="list-of-category" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if $ID == $category_id_value}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
<div class="loading" id="input-coupon-products-id-loading" style="display: none"></div>
<div class="form-group" id="input-coupon-products-id" {if $category_id_value == 0}style="display: none"{/if}>
<label for="coupon-products-id">{intl l="Applies to products :"}</label>
<select required multiple size="10" class="form-control" id="coupon-products-id" name="{$products_field_name}[]">
{loop type="product" category=$category_id_value name="list-of-products" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $products_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
<script>
function couponInputFormSetup() {
var $catSelect = $('#coupon-category-id');
$catSelect.change(function(ev) {
var $category_id = $(this).val();
$('#input-coupon-products-id').hide();
$('#coupon-products-id').html('');
if ($category_id != 0) {
$('#input-coupon-products-id-loading').show();
$.ajax({
url: "{url path='/admin/coupon/type-fragments/ajax-products-list'}",
type: 'POST',
data: {
category_id: $category_id
},
success: function(options) {
$('#coupon-products-id').html(options);
$('#input-coupon-products-id').show();
},
complete: function() {
$('#input-coupon-products-id-loading').hide();
}
});
}
});
};
</script>
{/block}

View File

@@ -1,24 +1,14 @@
{extends file="coupon/type-fragments/base-remove-on-categories.html"}
<div class="form-group input-coupon-percentage">
{block name="discount-field"}
<div class="form-group input-coupon-percentage">
<label for="coupon-percentage" class="control-label">{intl l="Discount percentage"}</label>
<label for="coupon-percentage" class="control-label">{intl l="Discount percentage"}</label>
<div class="input-group">
<input id="coupon-percentage" type="number" class="form-control" name="{$percentage_field_name}" value="{$percentage_value}" placeholder="5">
<div class="input-group">
<input id="coupon-percentage" type="number" class="form-control" name="{$percentage_field_name}" value="{$percentage_value}" placeholder="5">
<div class="input-group-addon">%</div>
<div class="input-group-addon">%</div>
</div>
</div>
</div>
<div class="form-group input-coupon-categories-id">
<label for="coupon-categories-id">{intl l="Applies to products in categories :"}</label>
<select required multiple size="10" class="form-control" id="coupon-categories-id" name="{$categories_field_name}[]">
{loop type="category-tree" category=0 name="list-of-categories" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $categories_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
{/block}

View File

@@ -1,75 +1,14 @@
{extends file="coupon/type-fragments/base-remove-on-products.html"}
<div class="form-group input-coupon-percentage">
{block name="discount-field"}
<div class="form-group input-coupon-percentage">
<label for="coupon-percentage" class="control-label">{intl l="Discount percentage"}</label>
<label for="coupon-percentage" class="control-label">{intl l="Discount percentage"}</label>
<div class="input-group">
<input id="coupon-percentage" type="number" class="form-control" name="{$percentage_field_name}" value="{$percentage_value}" placeholder="5">
<div class="input-group">
<input id="coupon-percentage" type="number" class="form-control" name="{$percentage_field_name}" value="{$percentage_value}" placeholder="5">
<div class="input-group-addon">%</div>
<div class="input-group-addon">%</div>
</div>
</div>
</div>
<div class="form-group input-coupon-category-id">
<label for="coupon-category-id">{intl l="Select product category :"}</label>
<select required class="form-control" id="coupon-category-id" name="{$category_id_field_name}">
<option value="0">{intl l="Please select..."}</option>
{loop type="category-tree" category=0 name="list-of-category" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if $ID == $category_id_value}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
<div class="loading" id="input-coupon-products-id-loading" style="display: none"></div>
<div class="form-group" id="input-coupon-products-id" {if $category_id_value == 0}style="display: none"{/if}>
<label for="coupon-products-id">{intl l="Applies to products :"}</label>
<select required multiple size="10" class="form-control" id="coupon-products-id" name="{$products_field_name}[]">
{loop type="product" category=$category_id_value name="list-of-products" backend_context="1"}
<option style="padding-left: {$LEVEL * 10}px" value="{$ID}" {if in_array($ID, $products_values)}selected="selected"{/if}>{$TITLE}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one category'}</span>
</div>
<script>
function couponInputFormSetup() {
var $catSelect = $('#coupon-category-id');
$catSelect.change(function(ev) {
var $category_id = $(this).val();
$('#input-coupon-products-id').hide();
$('#coupon-products-id').html('');
if ($category_id != 0) {
$('#input-coupon-products-id-loading').show();
$.ajax({
url: "{url path='/admin/coupon/type-fragments/ajax-products-list'}",
type: 'POST',
data: {
category_id: $category_id
},
success: function(options) {
$('#coupon-products-id').html(options);
$('#input-coupon-products-id').show();
},
complete: function() {
$('#input-coupon-products-id-loading').hide();
}
});
}
});
};
</script>
{/block}