/**
  * Preferences
  *
  * @return void
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 03.03.2015
  */
 public function preferences()
 {
     $numberOfDecimalPlaces = false;
     if ($this->getProduct()->isInCart()) {
         $this->preferences['submitButtonTitle'] = _t('SilvercartProduct.CHANGE_QUANTITY_CART');
     } else {
         $this->preferences['submitButtonTitle'] = _t('SilvercartProduct.ADD_TO_CART');
     }
     $this->preferences['doJsValidationScrolling'] = false;
     $this->formFields['productQuantity']['title'] = _t('SilvercartProduct.QUANTITY');
     $backLink = Controller::curr()->getRequest()->getURL();
     if (Director::is_relative_url($backLink)) {
         $backLink = Director::absoluteURL($backLink, true);
     }
     $this->setCustomParameter('backLink', $backLink);
     // Get maxlength for quantity field
     $quantityFieldMaxLength = strlen((string) SilvercartConfig::addToCartMaxQuantity());
     if ($quantityFieldMaxLength == 0) {
         $quantityFieldMaxLength = 1;
     }
     if (array_key_exists('productID', $this->customParameters)) {
         $silvercartProduct = $this->getProduct();
         if ($silvercartProduct instanceof SilvercartProduct) {
             $numberOfDecimalPlaces = $silvercartProduct->SilvercartQuantityUnit()->numberOfDecimalPlaces;
         }
     }
     if ($numberOfDecimalPlaces !== false && $numberOfDecimalPlaces > 0) {
         if (array_key_exists('isNumbersOnly', $this->formFields['productQuantity']['checkRequirements'])) {
             unset($this->formFields['productQuantity']['checkRequirements']['isNumbersOnly']);
         }
         $this->formFields['productQuantity']['checkRequirements']['isDecimalNumber'] = $numberOfDecimalPlaces;
         $this->formFields['productQuantity']['maxLength'] = $quantityFieldMaxLength + 1 + $numberOfDecimalPlaces;
     } else {
         $this->formFields['productQuantity']['maxLength'] = $quantityFieldMaxLength;
     }
     parent::preferences();
 }
 /**
  * adds an product to the cart or increases its amount
  * If stock managament is activated:
  * -If the product's stock quantity is overbookable there are noc hanges in
  *  behaviour.
  * -If the stock quantity of a product is NOT overbookable and the $quantity
  *  is larger than the stock quantity $quantity will be set to stock quantity.
  * -If the stock quantity of a product is NOT overbookable and the products
  *  stock quantity is less than zero false will be returned.
  *
  * @param int     $cartID    ID of the users shopping cart
  * @param float   $quantity  Amount of products to be added
  * @param boolean $increment Set to true to increment the quantity instead
  *                           of setting it absolutely
  *
  * @return mixed SilvercartShoppingCartPosition|boolean false
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>,
  *         Roland Lehmann <*****@*****.**>
  * @since 16.06.2014
  */
 public function addToCart($cartID, $quantity = 1, $increment = false)
 {
     $addToCartAllowed = true;
     $this->extend('updateAddToCart', $addToCartAllowed);
     if ($this->IsNotBuyable) {
         return false;
     }
     if ($quantity == 0 || $cartID == 0) {
         return false;
     }
     if (!$addToCartAllowed) {
         return false;
     }
     if (!$this->isBuyableDueToStockManagementSettings()) {
         return false;
     }
     $shoppingCartPosition = SilvercartShoppingCartPosition::get()->filter(array('SilvercartProductID' => $this->ID, 'SilvercartShoppingCartID' => $cartID))->first();
     if (!$shoppingCartPosition) {
         $shoppingCartPosition = new SilvercartShoppingCartPosition();
         $shoppingCartPosition->castedUpdate(array('SilvercartShoppingCartID' => $cartID, 'SilvercartProductID' => $this->ID));
         $shoppingCartPosition->write();
         $shoppingCartPosition = SilvercartShoppingCartPosition::get()->filter(array('SilvercartProductID' => $this->ID, 'SilvercartShoppingCartID' => $cartID))->first();
     }
     $positionNotice = null;
     if ($shoppingCartPosition->Quantity < $quantity) {
         $quantityToAdd = $quantity - $shoppingCartPosition->Quantity;
         if ($shoppingCartPosition->isQuantityIncrementableBy($quantityToAdd)) {
             if ($quantity > SilvercartConfig::addToCartMaxQuantity()) {
                 $shoppingCartPosition->Quantity = SilvercartConfig::addToCartMaxQuantity();
                 $positionNotice = 'maxQuantityReached';
             } else {
                 $shoppingCartPosition->Quantity = $quantity;
             }
         } elseif ($this->StockQuantity > 0) {
             if ($shoppingCartPosition->Quantity + $this->StockQuantity > SilvercartConfig::addToCartMaxQuantity()) {
                 $shoppingCartPosition->Quantity = SilvercartConfig::addToCartMaxQuantity();
                 $positionNotice = 'maxQuantityReached';
             } else {
                 $shoppingCartPosition->Quantity = $this->StockQuantity;
                 $positionNotice = 'remaining';
             }
         } else {
             $shoppingCartPosition = false;
         }
     } else {
         if ($increment) {
             $shoppingCartPosition->Quantity += $quantity;
         } else {
             $shoppingCartPosition->Quantity = $quantity;
         }
     }
     if ($shoppingCartPosition instanceof SilvercartShoppingCartPosition) {
         $shoppingCartPosition->write();
         if (!is_null($positionNotice)) {
             SilvercartShoppingCartPositionNotice::setNotice($shoppingCartPosition->ID, $positionNotice);
         }
         SilvercartPlugin::call($this, 'onAfterAddToCart', array($shoppingCartPosition));
     }
     $this->extend('onAfterAddToCart', $shoppingCartPosition);
     return $shoppingCartPosition;
 }
 /**
  * Returns the maximum number of products that can be added to cart for one
  * product.
  *
  * @return int
  *
  * @author Sascha Koehler <*****@*****.**>
  * @since 21.11.2011
  */
 public static function addToCartMaxQuantity()
 {
     if (is_null(self::$addToCartMaxQuantity)) {
         self::$addToCartMaxQuantity = self::getConfig()->addToCartMaxQuantity;
     }
     return self::$addToCartMaxQuantity;
 }