Source of file ShoppingCartFactory.php
Size: 7,415 Bytes - Last Modified: 2021-12-23T10:24:42+00:00
/var/www/docs.ssmods.com/process/src/src/ShoppingCartFactory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 | <?php namespace SilverCommerce\ShoppingCart; use SilverStripe\Control\Cookie; use SilverStripe\Security\Security; use SilverStripe\Control\HTTPRequest; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\FieldType\DBDatetime; use SilverCommerce\ShoppingCart\Tasks\CleanExpiredEstimatesTask; use SilverCommerce\ShoppingCart\Model\ShoppingCart as ShoppingCartModel; use SilverCommerce\ShoppingCart\Control\ShoppingCart as ShoppingCartController; use SilverCommerce\OrdersAdmin\Factory\OrderFactory; /** * Factory to handle setting up and interacting with a ShoppingCart * object. * */ class ShoppingCartFactory extends OrderFactory { /** * Name of the test cookie used to check if cookies are allowed */ const TEST_COOKIE = "ShoppingCartFactoryTest"; /** * Name of Cookie/Session used to track cart access key */ const COOKIE_NAME = "ShoppingCart.Key"; /** * The default class that is used by the factroy * * @var string */ private static $estimate_class = ShoppingCartModel::class; /** * The default class that is used by the factroy * * @var string */ private static $controller = ShoppingCartController::class; /** * Should the cart globally check for stock levels on items added? * Using this setting will ignore individual "Stocked" settings * on Shopping Cart Items. * * @var string */ private static $check_stock_levels = false; /** * whether or not the cleaning task should be left to a cron job * * @var boolean * @config */ private static $cron_cleaner = false; /** * Allow the user to add multiple discounts to the cart * 0 = unlimited * * @var int * @config */ private static $discount_limit = 1; /** * Should a cookie be used to track a link to a cart (so it persists * between browser sessions)? * * @var boolean * @config */ private static $use_cookies = false; /** * Setup the shopping cart and return an instance * * @return ShoppingCart **/ public function __construct() { $member = Security::getCurrentUser(); $this->setIsInvoice(false); $cart = $this->findOrMake(); // If we don't have any discounts, a user is logged in and he has // access to discounts through a group, add the discount here if (!$cart->Discounts()->Count() > 0 && $member && $member->getDiscount()) { $discount = $member->getDiscount(); if ($discount->exists()) { $cart->addDiscount($discount); } } if (!$this->config()->cron_cleaner) { $this->cleanOld(); } $this->order = $cart; } /** * Legacy get current method * * @return \SilverCommerce\OrdersAdmin\Model\Estimate */ public function getCurrent() { return $this->getOrder(); } /** * Get the current session from the current request * * @return Session */ public function getSession() { $request = Injector::inst()->get(HTTPRequest::class); return $request->getSession(); } /** * Either find an existing cart, or create a new one. * * @return ShoppingCartModel */ public function findOrMake() { $cookies = $this->cookiesSupported(); $session = $this->getSession(); $classname = self::config()->get("estimate_class"); $cart = null; $member = Security::getCurrentUser(); if ($cookies) { $cart_id = Cookie::get(self::COOKIE_NAME); } else { $cart_id = $session->get(self::COOKIE_NAME); } // Try to get a cart from the the DB if (isset($cart_id)) { $cart = $classname::get()->find('AccessKey', $cart_id); } // Does the current member have a cart? if (empty($cart) && isset($member) && $member->getCart()) { $cart = $member->getCart(); } // Finally, if nothing is set, create a new instance to return if (empty($cart)) { $cart = $classname::create(); // Add new cart to current member (if existing) if (!empty($member)) { $member->setCart($cart); } } return $cart; } /** * Run the task to clean old shopping carts * * @return null */ public function cleanOld() { $siteconfig = SiteConfig::current_site_config(); $date = $siteconfig->dbobject("LastEstimateClean"); $request = Injector::inst()->get(HTTPRequest::class); if (!$date || ($date && !$date->IsToday())) { $task = Injector::inst()->create(CleanExpiredEstimatesTask::class); $task->setSilent(true); $task->run($request); $siteconfig->LastEstimateClean = DBDatetime::now()->Value; $siteconfig->write(); } } /** * Test to see if a cookie should be used, or * the current user supports cookies * * @return boolean */ public function cookiesSupported() { $use_cookies = $this->config()->use_cookies; if (!$use_cookies) { return false; } Cookie::set(self::TEST_COOKIE, 1); $cookie = Cookie::get(self::TEST_COOKIE); Cookie::force_expiry(self::TEST_COOKIE); return (empty($cookie)) ? false : true; } /** * Destroy current shopping cart * * @return self */ public function delete() { $cookies = $this->cookiesSupported(); $cart = $this->getOrder(); // Only delete the cart if it has been written to the DB if ($cart->exists()) { $cart->delete(); } if ($cookies) { Cookie::force_expiry(self::COOKIE_NAME); } else { $this->getSession()->clear(self::COOKIE_NAME); } return $this; } /** * Save the current shopping cart, by writing it to the DB and * generating a cookie/session (if user not logged in). * * @return self */ public function write() { $cookies = $this->cookiesSupported(); $session = $this->getSession(); $member = Security::getCurrentUser(); $cart = $this->getOrder(); $cart->recalculateDiscounts(); // If the cart exists and the current user's cart doesn't // match, they have just logged in, replace their cart with // the new one. if ($cart->exists() && isset($member) && $member->getCart() != $cart) { $member->setCart($cart); if ($cookies) { Cookie::force_expiry(self::COOKIE_NAME); } else { $session->clear(self::COOKIE_NAME); } } $cart->write(); if (!$member && $cookies) { Cookie::set(self::COOKIE_NAME, $cart->AccessKey); } elseif (!$member) { $session->set(self::COOKIE_NAME, $cart->AccessKey); } return $this; } /** * Shortcut for write * * @return self */ public function save() { return $this->write(); } } |