diff --git a/README.md b/README.md index 6902935..3b184b8 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,6 @@ Dynamic\Foxy\Model\FoxyHelper: product_classes: - Dynamic\Products\Page\Product include_product_subclasses: 1 # (optional) include subclasses of product_classes in queries - ``` Create a DataExtension `ProductOptionDataExtension`: @@ -60,7 +59,7 @@ namespace { use Dynamic\Products\Page\Product; use SilverStripe\ORM\DataExtension; - + class ProductOptionDataExtension extends DataExtension { private static $belongs_many_many = [ @@ -76,6 +75,15 @@ Dynamic\Foxy\Model\ProductOption: extensions: - ProductOptionDataExtension ``` +## Product option configuration +Product options can be set to trim whitespace off code modifications. +By default it will only trim spaces after the code and remove duplicate spaces. +Setting `trimAllWhitespace` to true will trim all excess whitespace. + +```yaml +Dynamic\Foxy\Model\ProductOption: + trimAllWhitespace: true +``` ## Templates @@ -83,20 +91,20 @@ To include the AddToCartForm on your page/object, use `<% include AddToCartForm ## Maintainers * [Dynamic](http://www.dynamicagency.com) () - + ## Bugtracker -Bugs are tracked in the issues section of this repository. Before submitting an issue please read over -existing issues to ensure yours is unique. - +Bugs are tracked in the issues section of this repository. Before submitting an issue please read over +existing issues to ensure yours is unique. + If the issue does look like a new bug: - + - Create a new issue - - Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots + - Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots and screencasts can help here. - - Describe your environment as detailed as possible: SilverStripe version, Browser, PHP version, + - Describe your environment as detailed as possible: SilverStripe version, Browser, PHP version, Operating System, any installed SilverStripe modules. - + Please report security issues to the module maintainers directly. Please don't file security issues in the bugtracker. - + ## Development and contribution If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers. diff --git a/lang/en.yml b/lang/en.yml index 01b128e..22a0bd7 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -7,7 +7,7 @@ en: AvailableLabelNice: 'Available' ImageLabel: 'Image' ReceiptTitleLabel: 'Product title for receipt' - CodeDescription: 'Required, must be unique. Product identifier used by FoxyCart in transactions' + CodeDescription: 'Required, must be unique. Product identifier used by FoxyCart in transactions. All leading and trailing spaces are removed on save.' PriceDescription: 'Base price for this product. Can be modified using Product Options' PriceRequired: 'You must set a product price' ReceiptTitleDescription: 'Optional. Alternate title to display on order receipt' @@ -29,4 +29,4 @@ en: Dynamic\Foxy\Model\FoxyCategory: TitleLabel: 'Title' - CodeLabel: 'Code' \ No newline at end of file + CodeLabel: 'Code' diff --git a/src/Extension/Purchasable.php b/src/Extension/Purchasable.php index e1d42e9..fd7924c 100644 --- a/src/Extension/Purchasable.php +++ b/src/Extension/Purchasable.php @@ -23,6 +23,18 @@ /** * Class Purchasable * @package Dynamic\Foxy\Extension + * + * @property double Price + * @property string Code + * @property string ReceiptTitle + * @property bool Available + * + * @property int FoxyCategoryID + * @method FoxyCategory FoxyCategory() + * + * @method \SilverStripe\ORM\ManyManyList Options() + * + * @property-read \SilverStripe\ORM\DataObject|Purchasable $owner */ class Purchasable extends DataExtension implements PermissionProvider { @@ -142,15 +154,15 @@ public function updateCMSFields(FieldList $fields) TextField::create('Code') ->setDescription(_t( __CLASS__ . '.CodeDescription', - 'Required, must be unique. Product identifier used by FoxyCart in transactions' + 'Required, must be unique. Product identifier used by FoxyCart in transactions. All leading and trailing spaces are removed on save.' )), DropdownField::create('FoxyCategoryID') ->setTitle($this->owner->fieldLabel('FoxyCategoryID')) ->setSource(FoxyCategory::get()->map()) ->setDescription(_t( __CLASS__ . '.FoxyCategoryDescription', - 'Required. Must also exist in - Foxy Categories . @@ -341,4 +353,14 @@ public function canArchive($member = null) return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); } + + /** + * + */ + public function onBeforeWrite() + { + // trim spaces and replace duplicate spaces + $trimmed = trim($this->owner->Code); + $this->owner->Code = preg_replace('/\s+/', ' ', $trimmed); + } } diff --git a/src/Extension/Shippable.php b/src/Extension/Shippable.php index 8d25f5f..b214599 100644 --- a/src/Extension/Shippable.php +++ b/src/Extension/Shippable.php @@ -7,6 +7,12 @@ use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\ValidationResult; +/** + * Class Shippable + * @package Dynamic\Foxy\Extension + * + * @property double Weight + */ class Shippable extends DataExtension { /** diff --git a/src/Model/FoxyCategory.php b/src/Model/FoxyCategory.php index 2efdff1..7283ca2 100644 --- a/src/Model/FoxyCategory.php +++ b/src/Model/FoxyCategory.php @@ -8,6 +8,13 @@ use SilverStripe\Security\Permission; use SilverStripe\Security\Security; +/** + * Class FoxyCategory + * @package Dynamic\Foxy\Model + * + * @property string Title + * @property string Code + */ class FoxyCategory extends DataObject { /** diff --git a/src/Model/OptionType.php b/src/Model/OptionType.php index 2961bbc..f23cafd 100644 --- a/src/Model/OptionType.php +++ b/src/Model/OptionType.php @@ -13,6 +13,12 @@ use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; +/** + * Class OptionType + * @package Dynamic\Foxy\Model + * + * @property string Title + */ class OptionType extends DataObject { /** diff --git a/src/Model/ProductOption.php b/src/Model/ProductOption.php index 5b74394..d2fb864 100644 --- a/src/Model/ProductOption.php +++ b/src/Model/ProductOption.php @@ -2,6 +2,7 @@ namespace Dynamic\Foxy\Model; +use Dynamic\Foxy\Extension\Purchasable; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CurrencyField; @@ -16,6 +17,24 @@ use SilverStripe\Security\Permission; use SilverStripe\Security\Security; +/** + * Class ProductOption + * @package Dynamic\Foxy\Model + * + * @property string Title + * + * The following are from many_many_extraFields + * @property-read double WeightModifier + * @property-read string CodeModifier + * @property-read double PriceModifier + * @property-read string WeightModifierAction + * @property-read string CodeModifierAction + * @property-read string PriceModifierAction + * @property-read bool Available + * @property-read int Type + * @property-read string OptionModifierKey + * @property-read int SortOrder + */ class ProductOption extends DataObject { /** @@ -108,7 +127,7 @@ public function getCMSFields() 'Does weight modify or replace base weight?' )), - // Price Modifier FIelds + // Price Modifier Fields HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 4), CurrencyField::create('ManyMany[PriceModifier]') ->setTitle(_t('OptionItem.PriceModifier', 'Price')), @@ -168,8 +187,26 @@ public function onBeforeWrite() { parent::onBeforeWrite(); - $field = 'ManyMany[OptionModifierKey]'; - $this->{$field} = $this->getGeneratedValue(); + $modifierKeyField = 'ManyMany[OptionModifierKey]'; + $this->{$modifierKeyField} = $this->getGeneratedValue(); + + $codeModifierField = 'ManyMany[CodeModifier]'; + switch ($this->CodeModifierAction) { + case 'Subtract': + case 'Add': + if ($this->config()->get('trimAllWhitespace') == false) { + // trim the right of the code - some companies use spaces to denote options + $trimmed = rtrim($this->{$codeModifierField}); + // replace duplicate spaces + $this->{$codeModifierField} = preg_replace('/\s+/', ' ', $trimmed); + break; + } + /* falls through */ + case 'Set': + $trimmed = trim($this->{$codeModifierField}); + $this->{$codeModifierField} = preg_replace('/\s+/', ' ', $trimmed); + break; + } } /** @@ -324,7 +361,7 @@ public function canDelete($member = null, $context = []) } /** - * @param $product + * @param Purchasable $product * @return mixed */ public function getPrice($product) @@ -332,15 +369,30 @@ public function getPrice($product) switch ($this->PriceModifierAction) { case 'Subtract': return $product->Price - $this->PriceModifier; - break; case 'Set': return $this->PriceModifier; - break; case 'Add': return $product->Price + $this->PriceModifier; - break; } return $product->Price; } + + /** + * @param Purchasable $product + * @return string + */ + public function getCode($product) + { + switch ($this->CodeModifierAction) { + case 'Subtract': + return rtrim($product->Code, $this->CodeModifier); + case 'Set': + return $this->CodeModifier; + case 'Add': + return $product->Code . $this->CodeModifier; + } + + return $product->Code; + } } diff --git a/src/Model/Setting.php b/src/Model/Setting.php index a0eb4b9..5e9a33c 100644 --- a/src/Model/Setting.php +++ b/src/Model/Setting.php @@ -23,9 +23,10 @@ * Class Setting * @package Dynamic\Foxy\Model * - * @property string $StoreKey - * @property string $StoreTitle - * @property string $StoreDomain + * @property string StoreKey + * @property bool EnableSidecart + * @property string StoreTitle + * @property string StoreDomain */ class Setting extends DataObject implements PermissionProvider, TemplateGlobalProvider { @@ -111,7 +112,7 @@ public function getCMSFields() if (self::store_name_warning() !== null) { $fields->addFieldToTab('Root.Main', LiteralField::create('StoreSubDomainHeaderWarning', _t( 'ProductPage.StoreSubDomainHeaderWarning', - '

Store domain must be entered in the + '

Store domain must be entered in the Foxy settings

' )), 'StoreDomain'); }