Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code trimming for product and option codes #90

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -60,7 +59,7 @@ namespace {

use Dynamic\Products\Page\Product;
use SilverStripe\ORM\DataExtension;

class ProductOptionDataExtension extends DataExtension
{
private static $belongs_many_many = [
Expand All @@ -76,27 +75,36 @@ 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

To include the AddToCartForm on your page/object, use `<% include AddToCartForm %>`

## Maintainers
* [Dynamic](http://www.dynamicagency.com) (<dev@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.
4 changes: 2 additions & 2 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -29,4 +29,4 @@ en:

Dynamic\Foxy\Model\FoxyCategory:
TitleLabel: 'Title'
CodeLabel: 'Code'
CodeLabel: 'Code'
28 changes: 25 additions & 3 deletions src/Extension/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories"
'Required. Must also exist in
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories"
target="_blank">
Foxy Categories
</a>.
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/Extension/Shippable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
7 changes: 7 additions & 0 deletions src/Model/FoxyCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
6 changes: 6 additions & 0 deletions src/Model/OptionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
66 changes: 59 additions & 7 deletions src/Model/ProductOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
/**
Expand Down Expand Up @@ -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')),
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -324,23 +361,38 @@ public function canDelete($member = null, $context = [])
}

/**
* @param $product
* @param Purchasable $product
* @return mixed
*/
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;
}
}
9 changes: 5 additions & 4 deletions src/Model/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -111,7 +112,7 @@ public function getCMSFields()
if (self::store_name_warning() !== null) {
$fields->addFieldToTab('Root.Main', LiteralField::create('StoreSubDomainHeaderWarning', _t(
'ProductPage.StoreSubDomainHeaderWarning',
'<p class="message error">Store domain must be entered in the
'<p class="message error">Store domain must be entered in the
<a href="/admin/foxy/">Foxy settings</a></p>'
)), 'StoreDomain');
}
Expand Down