From a6f240816c79484e45cc7231f46aed3a1e8c1001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20Krau=C3=9F?= Date: Mon, 26 Feb 2024 22:56:15 +0100 Subject: [PATCH] NumericField for ProductVariations and casting in Gridfield --- src/Extension/ProductVariationsExtension.php | 6 ++++++ src/Model/Variation/Variation.php | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Extension/ProductVariationsExtension.php b/src/Extension/ProductVariationsExtension.php index 90061118..2b270623 100644 --- a/src/Extension/ProductVariationsExtension.php +++ b/src/Extension/ProductVariationsExtension.php @@ -15,6 +15,7 @@ use SilverStripe\Forms\ListboxField; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataList; +use SilverStripe\ORM\FieldType\DBCurrency; use SilverStripe\Versioned\Versioned; use SilverStripe\View\ArrayData; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; @@ -58,6 +59,11 @@ public function updateCMSFields(FieldList $fields) ) ]); + $variationsDataColumns = $variationsGridField->getConfig()->getComponentByType('SilverStripe\Forms\GridField\GridFieldDataColumns'); + $variationsFieldCasting = $variationsDataColumns->getFieldCasting(); + $variationsFieldCasting['Price'] = DBCurrency::class . '->Nice'; + $variationsDataColumns->setFieldCasting($variationsFieldCasting); + $variationsGridField->getConfig()->addComponent($sort = new GridFieldOrderableRows('Sort')); if ($this->owner->Variations()->exists()) { diff --git a/src/Model/Variation/Variation.php b/src/Model/Variation/Variation.php index 87e5daf3..38a95344 100755 --- a/src/Model/Variation/Variation.php +++ b/src/Model/Variation/Variation.php @@ -2,6 +2,7 @@ namespace SilverShop\Model\Variation; +use SebastianBergmann\Comparator\NumericComparator; use SilverShop\Cart\ShoppingCart; use SilverShop\Model\Buyable; use SilverShop\Model\Order; @@ -10,6 +11,7 @@ use SilverStripe\Assets\Image; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LiteralField; +use SilverStripe\Forms\NumericField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; @@ -127,7 +129,8 @@ public function getCMSFields() { $fields = FieldList::create( TextField::create('InternalItemID', _t('SilverShop\Page\Product.Code', 'Product Code')), - TextField::create('Price', _t('SilverShop\Page\Product.db_BasePrice', 'Price')) + NumericField::create('Price', _t('SilverShop\Page\Product.db_BasePrice', 'Price')) + ->setScale(2) ); //add attributes dropdowns $attributes = $this->Product()->VariationAttributeTypes();