Source of file CouponModification.php
Size: 1,225 Bytes - Last Modified: 2021-12-23T10:48:02+00:00
/var/www/docs.ssmods.com/process/src/code/CouponModification.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 | <?php class CouponModification extends Modification { private static $has_one = array( 'Coupon' => 'Coupon' ); private static $defaults = array( 'SubTotalModifier' => false, 'SortOrder' => 200 ); public function add($order, $value = null) { //Get valid coupon for this order if($value !== null){ $code = Convert::raw2sql($value); }else{ $code = Convert::raw2sql($order->CouponCode); } $date = date('Y-m-d'); $coupon = Coupon::get() ->where("\"Code\" = '$code' AND \"Expiry\" >= '$date'") ->first(); if ($coupon && $coupon->exists()) { //Generate the Modification $mod = new CouponModification(); $mod->Price = $coupon->Amount($order)->getAmount(); $mod->Currency = $coupon->Currency; $mod->Description = $coupon->Label(); $mod->OrderID = $order->ID; $mod->Value = $coupon->ID; $mod->CouponID = $coupon->ID; $mod->write(); } } public function getFormFields() { $fields = new FieldList(); $coupon = $this->Coupon(); if ($coupon && $coupon->exists()) { $field = CouponModifierField::create($this, $coupon->Label(), $coupon->Code) ->setAmount($coupon->Price($this->Order())); $fields->push($field); } return $fields; } } |