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

Product CMS Field design #374

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/Model/OptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
*/
class OptionItem extends DataObject
{
/**
* @var string
*/
private static $singular_name = 'Product Option';

/**
* @var string
*/
private static $plural_name = 'Product Options';

/**
* @var array
*/
Expand Down Expand Up @@ -145,8 +155,8 @@ public function getCMSFields()
->setEmptyString('')
->setDescription(_t(
'OptionItem.GroupDescription',
'Name of this group of options. Managed in <a href="admin/settings">
Settings > FoxyStripe > Option Groups
'Name of this group of options. Managed in <a href="admin/foxystripe">
FoxyStripe > Option Groups
</a>'
));
if (class_exists('QuickAddNewExtension')) {
Expand Down
78 changes: 44 additions & 34 deletions src/Page/ProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ class ProductPage extends \Page implements PermissionProvider
],
];

/**
* @var array
*/
private static $defaults = [
'ShowInMenus' => false,
'Available' => true,
'Weight' => '0.0',
];

/**
* @var array
*/
Expand All @@ -182,6 +173,27 @@ class ProductPage extends \Page implements PermissionProvider
*/
private static $table_name = 'ProductPage';

/**
* @var array
*/
private static $defaults = [
'ShowInMenus' => false,
'Available' => true,
'Weight' => '0.0',
];

/**
* @return \SilverStripe\ORM\DataObject|void
*/
public function populateDefaults()
{
if ($cat = ProductCategory::get()->filter('Code', 'DEFAULT')->first()) {
$this->CategoryID = $cat->ID;
}

parent::populateDefaults();
}

/**
* Construct a new ProductPage.
*
Expand Down Expand Up @@ -211,7 +223,7 @@ public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels();

$labels['Title'] = _t('ProductPage.TitleLabel', 'Name');
$labels['Title'] = _t('ProductPage.TitleLabel', 'Product Name');
$labels['Code'] = _t('ProductPage.CodeLabel', 'Code');
$labels['Price.Nice'] = _t('ProductPage.PriceLabel', 'Price');
$labels['Available.Nice'] = _t('ProductPage.AvailableLabel', 'Available');
Expand All @@ -228,27 +240,6 @@ public function fieldLabels($includerelations = true)
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Cateogry Dropdown field w/ add new
$source = function () {
return ProductCategory::get()->map()->toArray();
};
$catField = DropdownField::create('CategoryID', _t('ProductPage.Category', 'FoxyCart Category'), $source())
->setEmptyString('')
->setDescription(_t(
'ProductPage.CategoryDescription',
'Required, must also exist in
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank">
FoxyCart Categories
</a>.
Used to set category specific options like shipping and taxes. Managed in
<a href="admin/settings">
Settings > FoxyStripe > Categories
</a>'
));
if (class_exists('QuickAddNewExtension')) {
$catField->useAddNew('ProductCategory', $source);
}

$fields->addFieldsToTab(
'Root.Main',
[
Expand All @@ -271,7 +262,22 @@ public function getCMSFields()
'Base weight for this product in lbs. Can be modified using Product Options'
))
->setScale(2),
$catField,
DropdownField::create(
'CategoryID',
_t('ProductPage.Category', 'Product Category'),
ProductCategory::get()->map()->toArray()
)
->setDescription(_t(
'ProductPage.CategoryDescription',
'Required, must also exist in
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank">
FoxyCart Categories
</a>.
Used to set category specific options like shipping and taxes. Managed in
<a href="admin/foxystripe">
FoxyStripe > Categories
</a>'
)),
],
'Content'
);
Expand Down Expand Up @@ -334,15 +340,19 @@ public function getCMSFields()
}
});

return parent::getCMSFields();
$fields = parent::getCMSFields();

$fields->dataFieldByName('Content')->setTitle('Description');

return $fields;
}

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

/**
Expand Down