Skip to content

Instantly share code, notes, and snippets.

@hailwood
Last active September 9, 2015 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hailwood/dcf54ab2fd2039b8b1e3 to your computer and use it in GitHub Desktop.
Save hailwood/dcf54ab2fd2039b8b1e3 to your computer and use it in GitHub Desktop.
<?php
/**
* Class ProductExtension
*
* @property Product $owner
*/
class ProductExtension extends SiteTreeExtension {
private static $db = [
'OriginalPrice' => 'Currency'
];
private static $has_one = [
'Brand' => 'ProductBrand'
];
private static $has_many = [
'ProductImages' => 'ProductImage'
];
public function updateCMSFields(FieldList $fields) {
$fields->dataFieldByName('Image')->setFolderName('Images/Products')->setTitle('Feature Image');
//TRYING: to get the AdditionalImages field added by the other extension
// Fatal error: Call to a member function setFolderName() on a non-object in /vagrant/httpdocs/mysite/code/Extensions/ProductExtension.php on line 30
$fields->fieldByName('AdditionalImages')->setFolderName('Images/Products');
if ($fields->dataFieldByName('BasePrice')) {
$fields->dataFieldByName('BasePrice')->setTitle('Selling Price')->setDescription('Price the product is currently selling at before modifiers');
}
/** @var DropdownField $ddf */
$fields->addFieldToTab('Root.Main', $ddf = DropdownField::create('BrandID', 'Brand', ProductBrand::get('ProductBrand')->map()->toArray()), 'ParentID');
$ddf->setHasEmptyDefault(true);
$fields->addFieldToTab('Root.Pricing',
TextField::create('OriginalPrice', 'Original price')
->setDescription('Original price to show crossed out if product is on sale')
->setMaxLength(12),
'CostPrice');
$fields->removeByName('Variations');
if ($fields->dataFieldByName('AllowEnquiry')) {
$fields->dataFieldByName('AllowEnquiry')->setTitle('Allow enquiries on this product (disables purchasing)');
}
return $fields;
}
/**
* Applies the same modifiers over OriginalSellingPrice as Price() does to give us an accurate measure
* ensuring we don't end up with the sale price showing as more expensive than the discount price
*
* @return \ShopCurrency
*/
public function getOriginalSellingPrice() {
$price = $this->owner->OriginalPrice;
$this->owner->extend("updateSellingPrice", $price);
//prevent negative values
$price = $price < 0 ? 0 : $price;
/** @var ShopCurrency $c */
$c = Currency::create('OriginalSellingPrice');
$c->setValue($price);
return $c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment