Source of file EcommerceTaskSetDefaultProductGroupValues.php
Size: 2,085 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Tasks/EcommerceTaskSetDefaultProductGroupValues.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | <?php namespace Sunnysideup\Ecommerce\Tasks; use SilverStripe\Dev\BuildTask; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; use Sunnysideup\Ecommerce\Pages\ProductGroup; /** * @description: resets fields in the product group class to "inherit" in case their value does not exist. * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: tasks */ class EcommerceTaskSetDefaultProductGroupValues extends BuildTask { protected $title = 'Set Default Product Group Values'; protected $description = 'Set default product group values such as DefaultSortOrder.'; protected $fieldsToCheck = [ 'SORT' => 'DefaultSortOrder', 'FILTER' => 'DefaultFilter', 'DISPLAY' => 'DisplayStyle', ]; public function run($request) { $productGroup = DataObject::get_one(ProductGroup::class); if ($productGroup) { foreach ($this->fieldsToCheck as $method => $fieldName) { $acceptableValuesArray = array_flip($productGroup->getUserPreferencesOptionsForDropdown($method)); $this->checkField($fieldName, $acceptableValuesArray, 'inherit'); } } else { DB::alteration_message('There are no ProductGroup pages to correct', 'created'); } } protected function checkField($fieldName, $acceptableValuesArray, $resetValue) { $faultyProductGroups = ProductGroup::get() ->exclude([$fieldName => $acceptableValuesArray]) ; if ($faultyProductGroups->exists()) { foreach ($faultyProductGroups as $faultyProductGroup) { $faultyProductGroup->{$fieldName} = $resetValue; $faultyProductGroup->writeToStage('Stage'); $faultyProductGroup->publish('Stage', 'Live'); DB::alteration_message("Reset {$fieldName} for " . $faultyProductGroup->Title, 'created'); } } else { DB::alteration_message("Could not find any faulty records for ProductGroup.{$fieldName}"); } } } |