Source of file PageControllerExtension.php
Size: 5,833 Bytes - Last Modified: 2021-12-24T06:46:11+00:00
/var/www/docs.ssmods.com/process/src/src/Extension/PageControllerExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 | <?php namespace Dynamic\Foxy\Discounts\Extension; use Dynamic\Foxy\Discounts\DiscountHelper; use Dynamic\Foxy\Discounts\Model\Discount; use Dynamic\Foxy\Discounts\Model\DiscountTier; use Dynamic\Foxy\Form\AddToCartForm; use Dynamic\Products\Page\Product; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Extension; use SilverStripe\Dev\Debug; use SilverStripe\Forms\Form; use SilverStripe\Forms\HiddenField; use SilverStripe\View\Requirements; /** * Class PageControllerExtension * @package Dynamic\Foxy\Discounts\Extension */ class PageControllerExtension extends Extension { /** * @var array */ private static $allowed_actions = [ 'fetchprice', ]; /** * @var array */ private static $exempt_fields = [ 'price', 'quantity', 'h:product_id', 'isAjax', ]; /** * @param $form */ public function updateAddToCartForm(&$form) { $page = $this->owner->data(); if ($this->getIsDiscountable($page)) { /** @var DiscountHelper $discount */ if ($discount = $page->getBestDiscount()) { Requirements::javascript('dynamic/silverstripe-foxy-discounts: client/dist/javascript/discount.js'); $code = $page->Code; if ($form instanceof Form && $fields= $form->Fields()) { $fields->push( HiddenField::create(AddToCartForm::getGeneratedValue( $code, $discount->getDiscount()->getDiscountType(), $this->getDiscountFieldValue() ))->setValue($this->getDiscountFieldValue()) ->addExtraClass('product-discount') ); } } } } /** * @return string */ public function getDiscountFieldValue() { /** @var Discount $discount */ if ($discount = $this->owner->data()->getBestDiscount()->getDiscount()) { $tiers = $discount->DiscountTiers(); $bulkString = ''; foreach ($tiers as $tier) { if ($discount->Type == 'Percent') { $bulkString .= "|{$tier->Quantity}-{$tier->Percentage}"; $method = 'allunits'; } elseif ($discount->Type == 'Amount') { $bulkString .= "|{$tier->Quantity}-{$tier->Amount}"; $method = 'allunits'; } } return "{$discount->Title}{{$method}{$bulkString}}"; } return false; } public function fetchprice(HTTPRequest $request) { if (!$id = $request->getVar('h:product_id')) { return; } if (!$product = Product::get()->byID(explode('||', $id)[0])) { return; } if (!$this->getIsDiscountable($product)) { return; } $totalPrice = Discount::config()->get('calculate_total'); $quantity = (int)$request->getVar('quantity'); $cost = ($totalPrice) ? $product->Price * $quantity : $product->Price; $optionsQuery = $this->getOptionsQuery($request->getVars()); $options = $product->Options()->filter($optionsQuery); foreach ($options as $option) { switch ($option->PriceModifierAction) { case 'Add': if ($totalPrice) { $cost += ($option->PriceModifier * $quantity); } else { $cost += $option->PriceModifier; } break; case 'Subtract': if ($totalPrice) { $cost -= ($option->PriceModifier * $quantity); } else { $cost -= $option->ProceModifier; } break; case 'Set': if ($totalPrice) { $cost = ($option->PriceModifier * $quantity); } else { $cost = $option->PriceModifier; } break; } } $discount = $this->getDiscount($quantity); if ($discount instanceof DiscountTier && $discount->exists()) { if ($discount->Discount()->Type == 'Percent') { $discountAmount = $cost * ($discount->Percentage / 100); } elseif ($discount->Discount()->Type == 'Amount') { $discountAmount = $discount->Amount; } if (isset($discountAmount)) { $cost = $cost - $discountAmount; } } return $cost; } /** * @param $quantity * @return mixed */ protected function getDiscount($quantity) { /** @var DiscountHelper $best */ $best = $this->owner->data()->getBestDiscount(); $best->setDiscountTier($quantity); $tier = $best->getDiscountTier(); return $tier; } /** * @param $vars * @return array */ protected function getOptionsQuery($vars) { $exempt = $this->owner->config()->get('exempt_fields'); $filter['PriceModifierAction:not'] = null; foreach ($vars as $key => $val) { if (!in_array($key, $exempt)) { $filter['OptionModifierKey'][] = explode('||', $val)[0]; } } return $filter; } /** * @param $product * @return bool */ public function getIsDiscountable($product) { return $product->hasMethod('getHasDiscount') && $product->hasMethod('getBestDiscount') && $product->getHasDiscount(); } } |