diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index db199c23a..94c84775c 100644 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -633,8 +633,9 @@ class CouponController extends BaseAdminController } $response = new ResponseRest($couponManager->drawBackOfficeInputs()); - } - else { + } else { + // Return an empty response if the service ID is not defined + // Typically, when the user chooses "Please select a coupon type" $response = new ResponseRest(''); } diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index d04c29b89..02336cc2e 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -402,7 +402,8 @@ abstract class CouponAbstract implements CouponInterface * * @return string */ - public function getInputName() { + public function getInputName() + { return "Please override getInputName() method"; } @@ -434,33 +435,32 @@ abstract class CouponAbstract implements CouponInterface * @return mixed * @throws \InvalidArgumentException if the field valiue is not valid. */ - protected function checkCouponFieldValue($fieldName, $fieldValue) { + protected function checkCouponFieldValue($fieldName, $fieldValue) + { return $fieldValue; } /** * A helper to get the value of a standard field name * - * @param string $fieldName the field name - * @param array $data the input form data (e.g. $form->getData()) - * @param mixed $defaultValue the default value if the field is not found. + * @param string $fieldName the field name + * @param array $data the input form data (e.g. $form->getData()) + * @param mixed $defaultValue the default value if the field is not found. * * @return mixed the input value, or the default one * * @throws \InvalidArgumentException if the field is not found, and no default value has been defined. */ - protected function getCouponFieldValue($fieldName, $data, $defaultValue = null) { + protected function getCouponFieldValue($fieldName, $data, $defaultValue = null) + { if (isset($data[self::COUPON_DATASET_NAME][$fieldName])) { - return $this->checkCouponFieldValue( $fieldName, $data[self::COUPON_DATASET_NAME][$fieldName] ); - } - else if (null !== $defaultValue) { + } elseif (null !== $defaultValue) { return $defaultValue; - } - else { + } else { throw new \InvalidArgumentException(sprintf("The coupon field name %s was not found in the coupon form", $fieldName)); } } @@ -468,10 +468,11 @@ abstract class CouponAbstract implements CouponInterface /** * A helper to create an standard field name that will be used in the coupon form * - * @param string $fieldName the field name + * @param string $fieldName the field name * @return string the complete name, ready to be used in a form. */ - protected function makeCouponFieldName($fieldName) { + protected function makeCouponFieldName($fieldName) + { return sprintf("%s[%s][%s]", CouponCreationForm::COUPON_CREATION_FORM_NAME, self::COUPON_DATASET_NAME, $fieldName); } @@ -480,20 +481,22 @@ abstract class CouponAbstract implements CouponInterface * * @return array */ - protected function getFieldList() { + protected function getFieldList() + { return [self::AMOUNT_FIELD_NAME]; } /** * Create the effect array from the list of fields * - * @param array $data the input form data (e.g. $form->getData()) + * @param array $data the input form data (e.g. $form->getData()) * @return array a filedName => fieldValue array */ - public function getEffects($data) { + public function getEffects($data) + { $effects = []; - foreach($this->getFieldList() as $fieldName) { + foreach ($this->getFieldList() as $fieldName) { $effects[$fieldName] = $this->getCouponFieldValue($fieldName, $data); } diff --git a/core/lib/Thelia/Coupon/Type/CouponInterface.php b/core/lib/Thelia/Coupon/Type/CouponInterface.php index f9acc973d..a9382749f 100644 --- a/core/lib/Thelia/Coupon/Type/CouponInterface.php +++ b/core/lib/Thelia/Coupon/Type/CouponInterface.php @@ -15,7 +15,6 @@ namespace Thelia\Coupon\Type; use Propel\Runtime\Collection\ObjectCollection; use Thelia\Condition\ConditionCollection; use Thelia\Coupon\FacadeInterface; -use Thelia\Form\CouponCreationForm; /** * Represents a Coupon ready to be processed in a Checkout process diff --git a/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php b/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php index 9994219e2..665a02bff 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php +++ b/core/lib/Thelia/Coupon/Type/RemoveAmountOnCategories.php @@ -14,6 +14,8 @@ namespace Thelia\Coupon\Type; use Thelia\Core\Translation\Translator; use Thelia\Coupon\FacadeInterface; +use Thelia\Model\CartItem; +use Thelia\Model\Category; /** * Allow to remove an amount from the checkout total @@ -29,7 +31,7 @@ class RemoveAmountOnCategories extends CouponAbstract /** @var string Service Id */ protected $serviceId = 'thelia.coupon.type.remove_amount_on_categories'; - var $category_list = array(); + public $category_list = array(); /** * @inheritdoc @@ -105,8 +107,29 @@ class RemoveAmountOnCategories extends CouponAbstract */ public function exec() { - // TODO !!! - return $this->amount; + // 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) { + + $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; } public function drawBackOfficeInputs() @@ -131,11 +154,11 @@ class RemoveAmountOnCategories extends CouponAbstract * * @return array */ - protected function getFieldList() { + protected function getFieldList() + { return [self::AMOUNT_FIELD_NAME, self::CATEGORIES_LIST]; } - /** * @inheritdoc */ @@ -151,8 +174,7 @@ class RemoveAmountOnCategories extends CouponAbstract ) ); } - } - else if ($fieldName === self::CATEGORIES_LIST) { + } elseif ($fieldName === self::CATEGORIES_LIST) { if (empty($fieldValue)) { throw new \InvalidArgumentException( Translator::getInstance()->trans( diff --git a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php index fe5322e22..1f83c6eec 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXAmount.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXAmount.php @@ -69,7 +69,8 @@ class RemoveXAmount extends CouponAbstract * * @return array */ - protected function getFieldList() { + protected function getFieldList() + { return [self::AMOUNT_FIELD_NAME]; } @@ -93,4 +94,4 @@ class RemoveXAmount extends CouponAbstract return $fieldValue; } -} \ No newline at end of file +} diff --git a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php index 06c73981a..e1cc20252 100644 --- a/core/lib/Thelia/Coupon/Type/RemoveXPercent.php +++ b/core/lib/Thelia/Coupon/Type/RemoveXPercent.php @@ -152,7 +152,8 @@ class RemoveXPercent extends CouponAbstract * * @return array */ - protected function getFieldList() { + protected function getFieldList() + { return [self::INPUT_PERCENTAGE_NAME]; } }