Back to PhocacartCart class

Method setFullItems

public
setFullItems
()

Method setFullItems - Source code

/*
public function getItems() {
    return $this->items;
}


public function updateSubTotal() {}
*/
/*
 * Set full items - items in cart, total(netto, tax, brutto), coupon,
 * We need to set it first, to count e.g. coupon
 * After counting all possible factors (items, count of items, netto, taxes, brutto, coupon, shipping)
 * we can render the cart (for example, free shipping by coupon needs to be set at the end as we check total netto amount)
 */
public function setFullItems()
{
    // posible to do fullitems[1];
    if (empty($this->fullitems)) {
        if (!empty($this->items)) {
            $app = Factory::getApplication();
            $paramsC = PhocacartUtils::getComponentParameters();
            $tax_calculation = $paramsC->get('tax_calculation', 0);
            $check_product_attributes = $paramsC->get('check_product_attributes', array(3));
            $price = new PhocacartPrice();
            $calc = new PhocacartCartCalculation();
            $calc->setType($this->type);
            // CHECK ACCESS OF ALL ITEMS IN CART, CHECK IF ATTRIBUTES STILL EXIST
            // (e.g. when someone open his/her cart after product changes were made
            foreach ($this->items as $k => $v) {
                $item = explode(':', $k);
                $itemId = $item[0];
                // CHECK PRODUCT
                $checkP = PhocacartProduct::checkIfAccessPossible((int) $itemId, (int) $v['catid'], $this->type);
                if (!$checkP) {
                    if ($app->getName() == 'administrator') {
                        $app->enqueueMessage(Text::_('COM_PHOCACART_ERROR_PRODUCT_STORED_IN_CUSTOMER_CART_NOT_EXISTS') . ' ' . Text::_('COM_PHOCACART_ERROR_PRODUCT_REMOVED_FROM_CUSTOMER_CART') . ' ' . Text::_('COM_PHOCACART_CUSTOMER_WILL_BE_INFORMED_OF_SITUATION_DURING_NEXT_VISIT_TO_STORE'), 'warning');
                    } else {
                        $app->enqueueMessage(Text::_('COM_PHOCACART_ERROR_PRODUCT_STORED_IN_CART_NOT_EXISTS') . ' ' . Text::_('COM_PHOCACART_ERROR_PRODUCT_REMOVED_FROM_CART') . ' ' . Text::_('COM_PHOCACART_PLEASE_RECHECK_PRODUCTS_IN_YOUR_CART'), 'error');
                    }
                    unset($this->items[$k]);
                    $this->updateItemsFromCheckout($k, 0);
                    // In case this all happens when order is made - stop the order and inform user
                    $this->updateProductsRemoved($k);
                } else {
                    // Product access is OK - product still in cart, check the attributes and options
                    // ATTRIBUTE AND OPTIONS CHECK
                    // Check if attributes and options of stored products in cart are available (no change between ordering)
                    // Takes a lot of resources, so it will be checked when making an order as default ($check_product_attributes = 3)
                    // ATTRIBUTES
                    if (in_array($this->instance, $check_product_attributes)) {
                        $attribs = array();
                        if (!empty($item[1])) {
                            $attribs = unserialize(base64_decode($item[1]));
                        }
                        $checkA = PhocacartProduct::checkIfProductAttributesOptionsExist((int) $itemId, $k, (int) $v['catid'], $this->type, $attribs);
                        if (!$checkA) {
                            $app->enqueueMessage(Text::_('COM_PHOCACART_ERROR_ATTRIBUTE_OF_PRODUCT_STORED_IN_CART_NOT_EXISTS') . ' ' . Text::_('COM_PHOCACART_ERROR_PRODUCT_REMOVED_FROM_CART') . ' ' . Text::_('COM_PHOCACART_PLEASE_RECHECK_PRODUCTS_IN_YOUR_CART'), 'error');
                            unset($this->items[$k]);
                            $this->updateItemsFromCheckout($k, 0);
                            // In case this all happens when order is made - stop the order and inform user
                            $this->updateProductsRemoved($k);
                        }
                    }
                }
            }
            if (empty($this->items)) {
                return false;
            }
            // END ACCESS
            // --------------------
            // 1) Basic Calculation
            // --------------------
            $calc->calculateBasicProducts($this->fullitems[1], $this->fullitemsgroup[1], $this->total[1], $this->stock, $this->minqty, $this->minmultipleqty, $this->items);
            //$calc->round($this->total[1]);
            // Fixed subtotal amount
            $this->total[1]['subtotalnetto'] = $this->total[1]['netto'];
            $this->total[1]['subtotalbrutto'] = $this->total[1]['brutto'];
            $this->fullitems[0] = $this->fullitems[4] = $this->fullitems[3] = $this->fullitems[2] = $this->fullitems[5] = $this->fullitems[1];
            $this->fullitemsgroup[0] = $this->fullitemsgroup[4] = $this->fullitemsgroup[3] = $this->fullitemsgroup[2] = $this->fullitemsgroup[5] = $this->fullitemsgroup[1];
            $this->total[0] = $this->total[4] = $this->total[3] = $this->total[2] = $this->total[5] = $this->total[1];
            // --------------------
            // 5) Reward Points
            // --------------------
            $calc->calculateRewardDiscounts($this->fullitems[5], $this->fullitemsgroup[5], $this->total[5], $this->reward);
            $this->fullitems[0] = $this->fullitems[4] = $this->fullitems[3] = $this->fullitems[2] = $this->fullitems[5];
            $this->fullitemsgroup[0] = $this->fullitemsgroup[4] = $this->fullitemsgroup[3] = $this->fullitemsgroup[2] = $this->fullitemsgroup[5];
            $this->total[0] = $this->total[4] = $this->total[3] = $this->total[2] = $this->total[5];
            // Subtotal after 2) Discount
            $this->total[5]['dnetto'] = $this->total[1]['netto'] - $this->total[5]['netto'];
            $this->total[5]['dbrutto'] = $this->total[1]['brutto'] - $this->total[5]['brutto'];
            // --------------------
            // 2) Product Discount
            // --------------------
            $calc->calculateProductDiscounts($this->fullitems[2], $this->fullitemsgroup[2], $this->total[2]);
            //$calc->round($this->total[2]);
            $this->fullitems[0] = $this->fullitems[4] = $this->fullitems[3] = $this->fullitems[2];
            $this->fullitemsgroup[0] = $this->fullitemsgroup[4] = $this->fullitemsgroup[3] = $this->fullitemsgroup[2];
            $this->total[0] = $this->total[4] = $this->total[3] = $this->total[2];
            // Subtotal after 2) Discount
            $this->total[2]['dnetto'] = $this->total[5]['netto'] - $this->total[2]['netto'];
            $this->total[2]['dbrutto'] = $this->total[5]['brutto'] - $this->total[2]['brutto'];
            // --------------------
            // 3) Cart Discount
            // --------------------
            $calc->calculateCartDiscounts($this->fullitems[3], $this->fullitemsgroup[3], $this->total[3], $this->cartdiscount);
            // 3b) Cart Discount - we need to divide fixed amount discount into products which meets the rules to get each discount
            if (!empty($this->total[3]['discountcartfixedamount'])) {
                $calc->recalculateCartDiscounts($this->fullitems[3], $this->fullitemsgroup[3], $this->total[3]);
            }
            $this->fullitems[0] = $this->fullitems[4] = $this->fullitems[3];
            $this->fullitemsgroup[0] = $this->fullitemsgroup[4] = $this->fullitemsgroup[3];
            $this->total[0] = $this->total[4] = $this->total[3];
            // Subtotal after 3) Discount
            $this->total[3]['dnetto'] = $this->total[2]['netto'] - $this->total[3]['netto'];
            $this->total[3]['dbrutto'] = $this->total[2]['brutto'] - $this->total[3]['brutto'];
            $calc->roundFixedAmountDiscount($this->total[3]);
            // Last because now we know the dnetto
            // --------------------
            // 4) Cart Coupon
            // --------------------
            $calc->calculateCartCoupons($this->fullitems[4], $this->fullitemsgroup[4], $this->total[4], $this->coupon);
            // 4b) Cart Coupon - we need to divide fixed amount coupon into products which meets the rules to get each coupon
            if (!empty($this->total[4]['couponcartfixedamount'])) {
                $calc->recalculateCartCoupons($this->fullitems[4], $this->fullitemsgroup[4], $this->total[4]);
            }
            $this->fullitems[0] = $this->fullitems[4];
            $this->fullitemsgroup[0] = $this->fullitemsgroup[4];
            $this->total[0] = $this->total[4];
            // Subtotal after 4) Coupon
            $this->total[4]['dnetto'] = $this->total[3]['netto'] - $this->total[4]['netto'];
            $this->total[4]['dbrutto'] = $this->total[3]['brutto'] - $this->total[4]['brutto'];
            $calc->roundFixedAmountCoupon($this->total[4]);
            //Subtotal after all discounts
            $this->total[0]['wdnetto'] = $this->total[1]['netto'] - $this->total[5]['dnetto'] - $this->total[2]['dnetto'] - $this->total[3]['dnetto'] - $this->total[4]['dnetto'];
            //$this->total[0]['subtotalafterdiscounts'] = $this->total[0]['netto'] - $this->total[5]['dnetto'] - $this->total[2]['dnetto'] - $this->total[3]['dnetto'] - $this->total[4]['dnetto'];
            //$calc->taxRecapitulation($this->total[0]);
            /*foreach($this->fullitems[0] as $k => $v) {
                                $item 	= explode(':', $k);
                                $attribs = unserialize(base64_decode($item[1]));
            
                            }*/
            // TOTAL still not ready
            // + Shipping addShippingCosts()
            // + Payment addPaymentCosts()
            if ($this->coupon['code'] != '' && $this->coupon['valid'] === 0) {
                // coupon is not valid and will be not counted
                // but it was added to database so remove it from db
                // E.g. user added a coupon when not logged in (e.g. he/she was in group "default"
                // then he/she was logged in and after log in the coupon was still here but e.g.
                // the user changed its group to "otherthandefault" and coupon does not meet rules
                // it will be not counted in calculation but even we can remove it from db
                // Remove it from database
                $this->removeCouponDb();
                // And after removing it and using the ide in instance - remove the instance
                $this->coupon['id'] = 0;
                $this->coupon['title'] = '';
                $this->coupon['code'] = '';
            }
        }
    }
}