Skip to content

Commit

Permalink
FoxyStripePurchaseForm - only include Weight if greater than 0 (#372)
Browse files Browse the repository at this point in the history
fixes #317

Ensure Code is unique via index and validator

Move weight to Main tab due to being required
  • Loading branch information
jsirish committed Dec 5, 2018
1 parent 1a1c218 commit ec7e1c7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
12 changes: 7 additions & 5 deletions src/Form/FoxyStripePurchaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ protected function getProductFields(FieldList $fields)
'price',
$this->product->Price
))->setValue($this->product->Price));//can't override id
$fields->push(HiddenField::create(ProductPage::getGeneratedValue(
$code,
'weight',
$this->product->Weight
))->setValue($this->product->Weight));
if ($this->product->Weight > 0) {
$fields->push(HiddenField::create(ProductPage::getGeneratedValue(
$code,
'weight',
$this->product->Weight
))->setValue($this->product->Weight));
}

$image = null;
if ($this->product->Image() || ProductPage::has_extension(ProductPageLegacy::class)) {
Expand Down
79 changes: 46 additions & 33 deletions src/Page/ProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ class ProductPage extends \Page implements PermissionProvider
* @var array
*/
private static $indexes = [
'Code' => true, // make unique
'Code' => [
'type' => 'unique',
'columns' => ['Code'],
],
];

/**
Expand All @@ -150,7 +153,7 @@ class ProductPage extends \Page implements PermissionProvider
private static $defaults = [
'ShowInMenus' => false,
'Available' => true,
'Weight' => '1.0',
'Weight' => '0.0',
];

/**
Expand Down Expand Up @@ -261,6 +264,13 @@ public function getCMSFields()
'ProductPage.PriceDescription',
'Base price for this product. Can be modified using Product Options'
)),
NumericField::create('Weight')
->setTitle(_t('ProductPage.Weight', 'Weight'))
->setDescription(_t(
'ProductPage.WeightDescription',
'Base weight for this product in lbs. Can be modified using Product Options'
))
->setScale(2),
$catField,
],
'Content'
Expand All @@ -286,13 +296,6 @@ public function getCMSFields()
'ProductPage.AvailableDescription',
'If unchecked, will remove "Add to Cart" form and instead display "Currently unavailable"'
)),
NumericField::create('Weight')
->setTitle(_t('ProductPage.Weight', 'Weight'))
->setDescription(_t(
'ProductPage.WeightDescription',
'Base weight for this product in lbs. Can be modified using Product Options'
))
->setScale(2),
TextField::create('ReceiptTitle')
->setTitle(_t('ProductPage.ReceiptTitle', 'Product Title for Receipt'))
->setDescription(_t(
Expand Down Expand Up @@ -334,6 +337,40 @@ public function getCMSFields()
return parent::getCMSFields();
}

/**
* @return RequiredFields
*/
public function getCMSValidator()
{
return new RequiredFields(['CategoryID', 'Price', 'Weight', 'Code']);
}

/**
* @return \SilverStripe\ORM\ValidationResult
*/
public function validate()
{
$result = parent::validate();

if (ProductPage::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) {
$result->addError('Code must be unique for each product.');
}

/*if($this->ID>0){
if ($this->Price <= 0) {
$result->addError('Price must be a positive value');
}
if($this->Weight <= 0){
$result->error('Must set a positive weight value');
}
if($this->Code == ''){
$result->error('Must set a product code');
}
}*/

return $result;
}

/**
* @return \SilverStripe\ORM\ManyManyList
*/
Expand Down Expand Up @@ -421,30 +458,6 @@ public function onBeforeDelete()
parent::onBeforeDelete();
}

public function validate()
{
$result = parent::validate();

/*if($this->ID>0){
if($this->Price <= 0) {
$result->error('Must set a positive price value');
}
if($this->Weight <= 0){
$result->error('Must set a positive weight value');
}
if($this->Code == ''){
$result->error('Must set a product code');
}
}*/

return $result;
}

public function getCMSValidator()
{
return new RequiredFields(['CategoryID', 'Price', 'Weight', 'Code']);
}

/**
* @param null $productCode
* @param null $optionName
Expand Down

0 comments on commit ec7e1c7

Please sign in to comment.