Source of file EcommerceCustomDeliveryModifier.php
Size: 8,140 Bytes - Last Modified: 2021-12-23T10:40:44+00:00
/var/www/docs.ssmods.com/process/src/code/model/modifiers/EcommerceCustomDeliveryModifier.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 | <?php /** * @author Nicolaas [at] sunnysideup.co.nz * @package: ecommerce * @sub-package: examples * @description: This is an example modifier that developers can use * as a starting point for writing their own modifiers. * **/ class EcommerceCustomDeliveryModifier extends OrderModifier { // ######################################## *** model defining static variables (e.g. $db, $has_one) /** * add extra fields as you need them. * **/ public static $db = array( "PostalCode" => "Varchar(10)", "SpecialProductCount" => "Int", "NonSpecialProductCount" => "Int" ); // ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields) /** * standard SS method */ public function getCMSFields() { $fields = parent::getCMSFields(); return $fields; } public static $singular_name = "Delivery Charge"; public function i18n_singular_name() { return self::$singular_name; } public static $plural_name = "Delivery Charges"; public function i18n_plural_name() { return self::$plural_name; } // ######################################## *** other (non) static variables (e.g. protected static $special_name_for_something, protected $order) // ######################################## *** CRUD functions (e.g. canEdit) // ######################################## *** init and update functions /** * For all modifers with their own database fields, we need to include this... * It will update each of the fields. * Within this method, we need to create the methods * Live{functionName} * e.g LiveMyField() and LiveMyReduction() in this case... * The OrderModifier already updates the basic database fields. * @param Bool $force - run it, even if it has run already */ public function runUpdate($force = false) { $this->checkField("PostalCode"); $this->checkField("SpecialProductCount"); $this->checkField("NonSpecialProductCount"); parent::runUpdate($force); } // ######################################## *** form functions (e. g. Showform and getform) /** * standard OrderModifier Method * Should we show a form in the checkout page for this modifier? */ public function ShowForm() { return false; } /** * Should the form be included in the editable form * on the checkout page? * @return Boolean */ public function ShowFormInEditableOrderTable() { return false; } /** * Should the form be included in the editable form * on the checkout page? * @return Boolean */ public function ShowFormOutsideEditableOrderTable() { return false; } // ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES /** * standard OrderModifer Method * Tells us if the modifier should take up a row in the table on the checkout page. * @return Boolean */ public function ShowInTable() { return true; } /** * standard OrderModifer Method * Tells us if the modifier can be removed (hidden / turned off) from the order. * @return Boolean */ public function CanBeRemoved() { return false; } // ######################################## *** inner calculations.... USES CALCULATED VALUES // ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES /** * if we want to change the default value for the Name field * (defined in the OrderModifer class) then we can do this * as shown in the method below. * You may choose to return an empty string or just a standard message. **/ protected function LiveName() { if ($obj = $this->MyPostalCodeObject()) { $title = $obj->Title; } else { $title = EcommerceDBConfig::current_ecommerce_db_config()->DeliveryChargeTitle; } if ($postalCode = $this->LivePostalCode()) { if ($postalCodeLabel = _t("EcommerceCustomDeliveryModifier.POSTAL_CODE", "postal code")) { $title .= " (".$postalCodeLabel.": ".$postalCode.")"; } } else { //do nothing... } return $title; } protected function LiveCalculatedTotal() { $specialCount = $this->LiveSpecialProductCount(); $nonSpecialCount = $this->LiveNonSpecialProductCount(); $postalCodeObjectOrDefaultConfig = $this->MyPostalCodeObject(); if (! $postalCodeObjectOrDefaultConfig) { $postalCodeObjectOrDefaultConfig = EcommerceDBConfig::current_ecommerce_db_config(); } $specialPrice = $postalCodeObjectOrDefaultConfig->PriceWithApplicableProducts; $nonSpecialPrice = $postalCodeObjectOrDefaultConfig->PriceWithoutApplicableProducts; if ($specialCount) { return $specialPrice; } else { return $nonSpecialPrice; } return 0; } // ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) // ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) public function onBeforeWrite() { parent::onBeforeWrite(); } // ######################################## *** debug functions public function LiveNonSpecialProductCount() { return $this->ProductCountForTotal(false); } public function LiveSpecialProductCount() { return $this->ProductCountForTotal(true); } /** * @return int */ protected function ProductCountForTotal($special = false) { $specialCount = 0; $nonSpecialCount = 0; $applicableProducts = $this->SelectedProductsArray(); if (count($applicableProducts)) { $order = $this->Order(); if ($order) { foreach ($order->OrderItems() as $item) { if ($special && in_array($item->Product()->ID, $applicableProducts)) { $specialCount += $item->Quantity; } else { $nonSpecialCount += $item->Quantity; } } } } if ($special) { return $specialCount; } else { return $nonSpecialCount; } } /** * * @return String */ public function LivePostalCode() { $postalCode = ""; $order = $this->Order(); if ($order) { $shippingAddress = $order->CreateOrReturnExistingAddress("ShippingAddress"); $postalCode = $shippingAddress->ShippingPostalCode; if (!$postalCode) { $billingAddress = $order->CreateOrReturnExistingAddress("BillingAddress"); $postalCode = $billingAddress->PostalCode; } } if (intval($postalCode) > 0) { return $postalCode; } return ""; } /** * * @array */ private function SelectedProductsArray() { $ecommerceConfig = EcommerceDBConfig::current_ecommerce_db_config(); return $ecommerceConfig->DeliverySpecialChargedProducts()->map("ID", "ID")->toArray(); } /** * * @return EcommerceCustomDeliveryPostalCode | null */ private function MyPostalCodeObject() { $ecommerceConfig = EcommerceDBConfig::current_ecommerce_db_config(); $postalCode = intval($this->LivePostalCode()); if ($postalCode) { return $ecommerceConfig->SpecialPricePostalCodes() ->where( "$postalCode >= \"PostalCodeLowestNumber\" AND $postalCode <= \"PostalCodeHighestNumber\" " )->First(); } return null; } } |