Back to PhocacartCart class

Method __construct

public
__construct
()

Method __construct - Source code

// 1.cart 2.checkout 3.order
public function __construct()
{
    $app = Factory::getApplication();
    $session = Factory::getSession();
    $dUser = PhocacartUser::defineUser($this->user, $this->vendor, $this->ticket, $this->unit, $this->section);
    $this->guest = PhocacartUserGuestuser::getGuestUser();
    $this->pos = PhocacartPos::isPos();
    $this->coupon['id'] = 0;
    $this->coupon['title'] = '';
    $this->coupon['valid'] = 0;
    $this->coupon['code'] = '';
    $this->reward['used'] = '';
    $this->cartdiscount['id'] = 0;
    $this->cartdiscount['title'] = '';
    $this->shipping['id'] = 0;
    $this->shipping['title'] = '';
    $this->shipping['method'] = '';
    $this->shipping['costs'] = 0;
    $this->shipping['image'] = '';
    $this->shipping['params_shipping'] = array();
    // For example shipping branch info
    $this->payment['id'] = 0;
    $this->payment['title'] = '';
    $this->payment['method'] = '';
    $this->payment['costs'] = 0;
    $this->payment['calculation_type'] = 0;
    $this->payment['image'] = '';
    $this->payment['params_payment'] = array();
    $this->stock['valid'] = 1;
    // check stock - products, attributes (no matter if stock checking is disabled or enabled)
    $this->minqty['valid'] = 1;
    // check minimum order quantity
    $this->minmultipleqty['valid'] = 1;
    // check minimum multiple order quantity
    // Admin info - Administrator asks for information about user's cart
    if ($app->getName() == 'administrator') {
        $userid = $app->input->get('userid', 0, 'int');
        $vendorid = $app->input->get('vendorid', 0, 'int');
        $ticketid = $app->input->get('ticketid', 0, 'int');
        $unitid = $app->input->get('unitid', 0, 'int');
        $sectionid = $app->input->get('sectionid', 0, 'int');
        $cartDb = PhocacartCartDb::getCartDb($userid, $vendorid, $ticketid, $unitid, $sectionid);
        $this->items = $cartDb['cart'];
        $this->coupon['id'] = $cartDb['coupon'];
        $this->coupon['title'] = $cartDb['coupontitle'];
        $this->coupon['code'] = $cartDb['couponcode'];
        $this->shipping['id'] = $cartDb['shipping'];
        $this->shipping['title'] = $cartDb['shippingtitle'];
        $this->shipping['method'] = $cartDb['shippingmethod'];
        $this->shipping['image'] = $cartDb['shippingimage'];
        $this->shipping['params_shipping'] = $cartDb['params_shipping'];
        $this->payment['id'] = $cartDb['payment'];
        $this->payment['title'] = $cartDb['paymenttitle'];
        $this->payment['method'] = $cartDb['paymentmethod'];
        $this->payment['image'] = $cartDb['paymentimage'];
        $this->payment['params_payment'] = $cartDb['params_payment'];
        $this->reward['used'] = $cartDb['reward'];
        $this->loyalty_card_number = $cartDb['loyalty_card_number'];
        return;
    }
    // POS
    if ($this->pos && (int) $this->vendor->id > 0) {
        $cartDb = PhocacartCartDb::getCartDb((int) $this->user->id, (int) $this->vendor->id, (int) $this->ticket->id, (int) $this->unit->id, (int) $this->section->id);
        $this->items = $cartDb['cart'];
        $this->coupon['id'] = $cartDb['coupon'];
        $this->coupon['title'] = $cartDb['coupontitle'];
        $this->coupon['code'] = $cartDb['couponcode'];
        $this->shipping['id'] = $cartDb['shipping'];
        $this->shipping['title'] = $cartDb['shippingtitle'];
        $this->shipping['method'] = $cartDb['shippingmethod'];
        $this->shipping['image'] = $cartDb['shippingimage'];
        $this->shipping['params_shipping'] = $cartDb['params_shipping'];
        $this->payment['id'] = $cartDb['payment'];
        $this->payment['title'] = $cartDb['paymenttitle'];
        $this->payment['method'] = $cartDb['paymentmethod'];
        $this->payment['image'] = $cartDb['paymentimage'];
        $this->payment['params_payment'] = $cartDb['params_payment'];
        $this->reward['used'] = $cartDb['reward'];
        $this->loyalty_card_number = $cartDb['loyalty_card_number'];
        // Don't care about session (use in session is customer, user in pos in db is vendor)
    } else {
        if ((int) $this->user->id > 0) {
            // DATABASE - logged in user - Singleton because of not load data from database every time cart instance is loaded
            // 1. Not found in DATABASE - maybe user logged in now, so:
            // 2. We try to find the data in SESSION, if they are still in SESSION - load them to our cart class and then
            // 3. Store them to DATABASE as all loged in users have cart in database and:
            // 4. Remove them from SESSION as they are stored in DATABASE
            $cartDb = PhocacartCartDb::getCartDb((int) $this->user->id);
            // user logged in - try to get cart from db
            $this->items = $cartDb['cart'];
            $this->coupon['id'] = $cartDb['coupon'];
            $this->coupon['title'] = $cartDb['coupontitle'];
            $this->coupon['code'] = $cartDb['couponcode'];
            $this->shipping['id'] = $cartDb['shipping'];
            $this->shipping['title'] = $cartDb['shippingtitle'];
            $this->shipping['method'] = $cartDb['shippingmethod'];
            $this->shipping['image'] = $cartDb['shippingimage'];
            $this->shipping['params_shipping'] = $cartDb['params_shipping'];
            $this->payment['id'] = $cartDb['payment'];
            $this->payment['title'] = $cartDb['paymenttitle'];
            $this->payment['method'] = $cartDb['paymentmethod'];
            $this->payment['image'] = $cartDb['paymentimage'];
            $this->payment['params_payment'] = $cartDb['params_payment'];
            $this->reward['used'] = $cartDb['reward'];
            $this->loyalty_card_number = $cartDb['loyalty_card_number'];
            $sessionItems = $session->get('cart', array(), 'phocaCart');
            if (empty($this->items)) {
                $this->items = $session->get('cart', array(), 'phocaCart');
                // COUPONMOVE - we can move the cart items to logged in user or guest checkout, so we do with coupon
                $this->coupon['id'] = $session->get('guestcoupon', false, 'phocaCart');
                if ((int) $this->coupon['id'] > 0) {
                    $couponInfo = PhocacartCoupon::getCouponInfoById($this->coupon['id']);
                    $this->coupon['title'] = isset($couponInfo->title) ? $couponInfo->title : '';
                    $this->coupon['code'] = isset($couponInfo->code) ? $couponInfo->code : '';
                }
                if (!empty($this->items)) {
                    $this->updateItemsDb();
                    // Very important - clean the static variable
                    // because more instances of cart are loading the static variable once
                    // So if the FIRST instance loaded empty cart
                    // and then has filled this empty cart with data from session
                    // SECOND instance must load the static variables newly because if it does not do
                    // then it get the same data like FIRST instance (empty cart) But the cart
                    // was filled in with data from session so there will be contradiction
                    // between instances (FIRST INSTANCE - EMPTY CART BUT REFILLED BY SESSION = FULL CART, SECOND INSTANCE EMPTY CART SET BY FIRST INSTANCE)
                    PhocacartCartDb::clearCartDbVariable((int) $this->user->id);
                    $session->set('cart', array(), 'phocaCart');
                    $session->set('guestcoupon', array(), 'phocaCart');
                    // guestcoupon is variable for both: for still not logged in user or someone who enabled guest checkout
                }
            } else {
                // we have stored items in database from previously
                // and we have stored items in session now
                if (!empty($sessionItems)) {
                    // inform users and clean session
                    $message = Text::_('COM_PHOCACART_CART_UPDATED_BASED_ON_YOUR_PREVIOUS_VISIT');
                    $app->enqueueMessage($message, 'message');
                    $session->set('cart', array(), 'phocaCart');
                    $session->set('guestcoupon', array(), 'phocaCart');
                }
            }
        } else {
            if ($this->guest) {
                $this->items = $session->get('cart', array(), 'phocaCart');
                $this->shipping['id'] = $session->get('guestshipping', false, 'phocaCart');
                $this->shipping['params_shipping'] = $session->get('guestshippingparams', false, 'phocaCart');
                if ((int) $this->shipping['id'] > 0) {
                    $shippingObject = new PhocacartShipping();
                    $shippingObject->setType($this->type);
                    $shippingInfo = $shippingObject->getShippingMethod((int) $this->shipping['id']);
                    $this->shipping['title'] = isset($shippingInfo->title) ? $shippingInfo->title : '';
                    $this->shipping['method'] = isset($shippingInfo->method) ? $shippingInfo->method : '';
                    $this->shipping['image'] = isset($shippingInfo->image) ? $shippingInfo->image : '';
                }
                $this->payment['id'] = $session->get('guestpayment', false, 'phocaCart');
                $this->payment['params_payment'] = $session->get('guestpaymentparams', false, 'phocaCart');
                if ((int) $this->payment['id'] > 0) {
                    $paymentObject = new PhocacartPayment();
                    $paymentObject->setType($this->type);
                    $paymentInfo = $paymentObject->getPaymentMethod((int) $this->payment['id']);
                    $this->payment['title'] = isset($paymentInfo->title) ? $paymentInfo->title : '';
                    $this->payment['method'] = isset($paymentInfo->method) ? $paymentInfo->method : '';
                    $this->payment['image'] = isset($paymentInfo->image) ? $paymentInfo->image : '';
                }
                $this->coupon['id'] = $session->get('guestcoupon', false, 'phocaCart');
                // COUPONMOVE
                if ((int) $this->coupon['id'] > 0) {
                    $couponInfo = PhocacartCoupon::getCouponInfoById($this->coupon['id']);
                    $this->coupon['title'] = isset($couponInfo->title) ? $couponInfo->title : '';
                    $this->coupon['code'] = isset($couponInfo->code) ? $couponInfo->code : '';
                }
                $this->loyalty_card_number = $session->get('guestloyaltycardnumber', false, 'phocaCart');
            } else {
                // SESSION - not logged in user
                $this->items = $session->get('cart', array(), 'phocaCart');
                $this->coupon['id'] = $session->get('guestcoupon', false, 'phocaCart');
                // COUPONMOVE
                if ((int) $this->coupon['id'] > 0) {
                    $couponInfo = PhocacartCoupon::getCouponInfoById($this->coupon['id']);
                    $this->coupon['title'] = isset($couponInfo->title) ? $couponInfo->title : '';
                    $this->coupon['code'] = isset($couponInfo->code) ? $couponInfo->code : '';
                }
            }
        }
    }
}