/** * Update product quantity * * @param integer $quantity Quantity to add (or substract) * @param $product_id * @param null $product_attribute_id * @param bool $customization_id * @param string $operator Indicate if quantity must be increased or decreased * @param int $address_delivery_id * @param JeproshopShopModelShop $shop * @param bool $auto_add_cart_rule * @return bool|int * @internal param int $id_product Product ID * @internal param int $id_product_attribute Attribute ID if needed */ public function updateQuantity($quantity, $product_id, $product_attribute_id = null, $customization_id = false, $operator = 'up', $address_delivery_id = 0, JeproshopShopModelShop $shop = null, $auto_add_cart_rule = true) { if (!$shop) { $shop = JeproshopContext::getContext()->shop; } $db = JFactory::getDBO(); if (JeproshopContext::getContext()->customer->customer_id) { if ($address_delivery_id == 0 && (int) $this->address_delivery_id) { // The $id_address_delivery is null, use the cart delivery address $address_delivery_id = $this->address_delivery_id; } elseif ($address_delivery_id == 0) { // The $id_address_delivery is null, get the default customer address $address_delivery_id = (int) JeproshopAddressModelAddress::getCustomerFirstAddressId((int) JeproshopContext::getContext()->customer->customer_id); } elseif (!JeproshopCustomerModelCustomer::customerHasAddress(JeproshopContext::getContext()->customer->customer_id, $address_delivery_id)) { // The $id_address_delivery must be linked with customer $address_delivery_id = 0; } } $quantity = (int) $quantity; $product_id = (int) $product_id; $product_attribute_id = (int) $product_attribute_id; $product = new JeproshopProductModelProduct($product_id, false, JeproshopSettingModelSetting::getValue('default_lang'), $shop->shop_id); if ($product_attribute_id) { $combination = new JeproshopCombinationModelCombination((int) $product_attribute_id); if ($combination->product_id != $product_id) { return false; } } /* If we have a product combination, the minimal quantity is set with the one of this combination */ if (!empty($product_attribute_id)) { $minimal_quantity = (int) JeproshopAttributeModelAttribute::getAttributeMinimalQty($product_attribute_id); } else { $minimal_quantity = (int) $product->minimal_quantity; } if (!JeproshopValidator::isLoadedObject($product, 'product_id')) { die(Tools::displayError()); } if (isset(self::$_nbProducts[$this->cart_id])) { unset(self::$_nbProducts[$this->cart_id]); } if (isset(self::$_totalWeight[$this->cart_id])) { unset(self::$_totalWeight[$this->cart_id]); } if ((int) $quantity <= 0) { return $this->deleteProduct($product_id, $product_attribute_id, (int) $customization_id); } elseif (!$product->available_for_order || JeproshopSettingModelSeting::getValue('catalog_mode')) { return false; } else { /* Check if the product is already in the cart */ $result = $this->containsProduct($product_id, $product_attribute_id, (int) $customization_id, (int) $address_delivery_id); /* Update quantity if product already exist */ if ($result) { if ($operator == 'up') { $query = "SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity FROM " . $db->quoteName('#__jeproshop_product') . " AS product" . JeproshopProductModelProduct::sqlStock('p', $product_attribute_id, true, $shop); $query .= " WHERE product.product_id = " . $product_id; $db->setQuery($query); $result2 = $db->loadObject(); $product_qty = (int) $result2->quantity; // Quantity for product pack if (JeproshopProductPack::isPack($product_id)) { $product_qty = JeproshopProductPack::getQuantity($product_id, $product_attribute_id); } $new_qty = (int) $result->quantity + (int) $quantity; $qty = '+ ' . (int) $quantity; if (!JeproshopProductModelProduct::isAvailableWhenOutOfStock((int) $result2->out_of_stock)) { if ($new_qty > $product_qty) { return false; } } } else { if ($operator == 'down') { $qty = '- ' . (int) $quantity; $new_qty = (int) $result->quantity - (int) $quantity; if ($new_qty < $minimal_quantity && $minimal_quantity > 1) { return -1; } } else { return false; } } /* Delete product from cart */ if ($new_qty <= 0) { return $this->deleteProduct((int) $product_id, (int) $product_attribute_id, (int) $customization_id); } else { if ($new_qty < $minimal_quantity) { return -1; } else { $query = "UPDATE " . $db->quoteName('#__jeproshop_cart_product') . " SET " . $db->quoteName('quantity') . " = " . $db->quoteName('quantity') . $qty . ", "; $query .= $db->quoteName('date_add') . " = NOW() WHERE " . $db->quoteName('product_id') . " = " . (int) $product_id; $query .= (!empty($product_attribute_id) ? " AND " . $db->quoteName('product_attribute_id') . " = " . (int) $product_attribute_id : "") . " AND " . $db->quoteName('cart_id') . " = " . (int) $this->cart_id; $query .= (JeproshopSettingModelSeting::getValue('allow_multi_shipping') && $this->isMultiAddressDelivery() ? " AND " . $db->quoteName('address_delivery_id') . " = " . (int) $address_delivery_id : "") . " LIMIT 1"; $db->setQuery($query); $db->query(); } } } elseif ($operator == 'up') { $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity FROM ' . _DB_PREFIX_ . 'product p ' . Product::sqlStock('p', $product_attribute_id, true, $shop) . ' WHERE p.id_product = ' . $product_id; $result2 = Db::getInstance()->getRow($sql); // Quantity for product pack if (Pack::isPack($product_id)) { $result2['quantity'] = Pack::getQuantity($product_id, $product_attribute_id); } if (!Product::isAvailableWhenOutOfStock((int) $result2['out_of_stock'])) { if ((int) $quantity > $result2['quantity']) { return false; } } if ((int) $quantity < $minimal_quantity) { return -1; } $result_add = Db::getInstance()->insert('cart_product', array('id_product' => (int) $product_id, 'id_product_attribute' => (int) $product_attribute_id, 'id_cart' => (int) $this->cart_id, 'id_address_delivery' => (int) $address_delivery_id, 'id_shop' => $shop->shop_id, 'quantity' => (int) $quantity, 'date_add' => date('Y-m-d H:i:s'))); if (!$result_add) { return false; } } } // refresh cache of self::_products $this->_products = $this->getProducts(true); $this->update(true); $context = Context::getContext()->cloneContext(); $context->cart = $this; Cache::clean('getContextualValue_*'); if ($auto_add_cart_rule) { CartRule::autoAddToCart($context); } if ($product->customizable) { return $this->updateCustomizationQuantity((int) $quantity, (int) $customization_id, (int) $product_id, (int) $product_attribute_id, (int) $address_delivery_id, $operator); } else { return true; } }
public function product() { // Update the cart ONLY if $this->cookies are available, in order to avoid ghost carts created by bots if (!$this->context) { $this->context = JeproshopContext::getContext(); } if ($this->context->cookie->exists() && !$this->has_errors && !($this->context->customer->isLogged() && !$this->isTokenValid())) { // Send noindex to avoid ghost carts by bots header("X-Robots-Tag: noindex, nofollow", true); if (!$this->isInitialized()) { $this->initialize(); } $app = JFactory::getApplication(); $product_id = $app->input->get('product_id', null); $customization_id = (int) $app->input->get('customization_id', null); $address_delivery_id = (int) $app->input->get('address_delivery_id', null); $product_attribute_id = $app->input->get('product_attribute_id', null); $mode = $app->input->get('task') == 'update' && $product_id ? 'update' : 'add'; if ($app->input->get('quantity') == 0) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_NULL_QUANTITY_MESSAGE') . ' ' . !$app->input->get('use_ajax'); } elseif (!$product_id) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_PRODUCT_ID_NOT_FOUND_MESSAGE') . ' ' . !$app->input->get('use_ajax'); } $product = new JeproshopProductModelProduct($product_id, true, $this->context->language->lang_id); if (!$product->product_id || !$product->published) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_THIS_PRODUCT_IS_NO_LONGER_AVAILABLE_MESSAGE.') . ' ' . !$app->input->get('use_ajax'); exit; } $quantity = abs($app->input->get('quantity', 1)); $qty_to_check = $quantity; $cart_products = $this->context->cart->getProducts(); if (is_array($cart_products)) { foreach ($cart_products as $cart_product) { if ((!isset($this->product_attribute_id) || $cart_product->product_attribute_id == $product_attribute_id) && (isset($product_id) && $cart_product->product_id == $product_id)) { $qty_to_check = $cart_product->cart_quantity; if ($app->input->get('op', 'up') == 'down') { $qty_to_check -= $quantity; } else { $qty_to_check += $quantity; } break; } } } // Check product quantity availability if ($product_attribute_id) { if (!JeproshopProductModelProduct::isAvailableWhenOutOfStock($product->out_of_stock) && !JeproshopAttributeModelAttribute::checkAttributeQty($product_attribute_id, $qty_to_check)) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax'); } } elseif ($product->hasAttributes()) { $minimumQuantity = $product->out_of_stock == 2 ? !JeproshopSettingModelSetting::getValue('order_out_of_stock') : !$product->out_of_stock; $product_attribute_id = JeproshopProductModelProduct::getDefaultAttribute($product->product_id, $minimumQuantity); // @todo do something better than a redirect admin !! if (!$product_attribute_id) { $app->redirect($this->getProductLink($product)); } elseif (!JeproshopProductModelProduct::isAvailableWhenOutOfStock($product->out_of_stock) && !JeproshopAttributeModelAttribute::checkAttributeQty($product_attribute_id, $qty_to_check)) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax'); } } elseif (!$product->checkQuantity($qty_to_check)) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_THERE_IS_NOT_ENOUGH_PRODUCT_IN_STOCK_MESSAGE') . ' ' . __LINE__ . !$app->input->get('use_ajax'); } // If no errors, process product addition if ($mode == 'add') { // Add cart if no cart found if (!$this->context->cart->cart_id) { if (JeproshopContext::getContext()->cookie->guest_id) { $guest = new JeproshopGuestModelGuest(JeproshopContext::getContext()->cookie->guest_id); $this->context->cart->mobile_theme = $guest->mobile_theme; } $this->context->cart->add(); if ($this->context->cart->cart_id) { $this->context->cookie->cart_id = (int) $this->context->cart->cart_id; } } // Check customizable fields if (!$product->hasAllRequiredCustomizableFields() && !$customization_id) { // $this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax')); } if (!$this->has_errors) { $cart_rules = $this->context->cart->getCartRules(); $update_quantity = $this->context->cart->updateQuantity($quantity, $product_id, $product_attribute_id, $customization_id, $app->input->get('op', 'up'), $address_delivery_id); if ($update_quantity < 0) { // If product has attribute, minimal quantity is set with minimal quantity of attribute $minimal_quantity = $product_attribute_id ? JeproshopAttributeModelAttribute::getAttributeMinimalQty($product_attribute_id) : $product->minimal_quantity; $this->has_errors = true; sprintf(Tools::displayError('You must add %d minimum quantity', !$app->input->get('use_ajax')), $minimal_quantity); } elseif (!$update_quantity) { $this->has_errors = true; echo JText::_('COM_JEPROSHOP_YOU_ALREADY_HAVE_THE_MAXIMUM_AVAILABLE_FOR_THIS_PRODUCT_MESSAGE') . ' ' . !$app->input->get('use_ajax'); } elseif ((int) $app->input->get('allow_refresh')) { // If the cart rules has changed, we need to refresh the whole cart $cart_rules2 = $this->context->cart->getCartRules(); if (count($cart_rules2) != count($cart_rules)) { $this->ajax_refresh = true; } else { $rule_list = array(); foreach ($cart_rules2 as $rule) { $rule_list[] = $rule->cart_rule_id; } foreach ($cart_rules as $rule) { if (!in_array($rule->cart_rule_id, $rule_list)) { $this->ajax_refresh = true; break; } } } } } } $removed = JeproshopCartRuleModelCartRule::autoRemoveFromCart(); JeproshopCartRuleModelCartRule::autoAddToCart(); if (count($removed) && (int) $app->input->get('allow_refresh')) { $this->ajax_refresh = true; } echo 'bonjour'; exit; } elseif (!$this->context->cookie->exists()) { echo 'bonjour cookkie'; exit; } elseif ($this->has_errors) { } elseif ($this->context->customer->isLogged() && !$this->isTokenValid()) { } }
private function initAttributesForm() { if (!JeproshopCombinationModelCombination::isFeaturePublished()) { $settingPanelLink = '<a href="#" >' . JText::_('COM_JEPROSHOP_PERFORMANCE_LABEL') . '</a>'; JError::raiseWarning(500, JText::_('COM_JEPROSHOP_FEATURE_HAS_BEEN_DISABLED_MESSAGE') . $settingPanelLink); } elseif (JeproshopTools::isLoadedObject($this->product, 'product_id')) { if ($this->product_exists_in_shop) { if ($this->product->is_virtual) { JError: raiseWarning(500, JText::_('COM_JEPROSHOP_VIRTUAL_PRODUCT_CANNOT_HAVE_COMBINATIONS')); } else { $attribute_js = array(); $attributes = JeproshopAttributeModelAttribute::getAttributes($this->context->language->lang_id, true); if ($attributes) { foreach ($attributes as $key => $attribute) { $attribute_js[$attribute->attribute_group_id][$attribute->attribute_id] = $attribute->name; } } $this->assignRef('attributeJs', $attribute_js); $attributes_groups = JeproshopAttributeGroupModelAttributeGroup::getAttributesGroups($this->context->language->lang_id); $this->assignRef('attributes_groups', $attributes_groups); $images = JeproshopImageModelImage::getImages($this->context->language->lang_id, $this->product->product_id); $weight_unit = JeproshopSettingModelSetting::getValue('weight_unit'); $this->assignRef('weight_unit', $weight_unit); $reasons = JeproshopStockMovementReasonModelStockMovementReason::getStockMovementReasons(); $this->assignRef('reasons', $reasons); //$this->assignRef('minimal_quantity', ); $this->assignRef('available_date', $available_date); $stock_mvt_default_reason = JeproshopSettingModelSetting::getValue('default_stock_mvt_reason'); $this->assignRef('default_stock_mvt_reason', $stock_mvt_default_reason); $i = 0; /*$type = JeproshopImageTypeModelImageType::getByNameNType('%', 'products', 'height'); if (isset($type->name)){ $data->assign('imageType', $type['name']); }else $data->assign('imageType', 'small_default'); */ //$this->assignRef('imageWidth', (isset($image_type->width) ? (int)($image_type->width) : 64) + 25); foreach ($images as $k => $image) { $images[$k]->obj = new JeproshopImageModelImage($image->image_id); ++$i; } $this->assignRef('attribute_images', $images); $attributeList = $this->renderAttributesList($this->product, $this->currency); $this->assignRef('list', $attributeList); $combination_exists = JeproshopShopModelShop::isFeaturePublished() && JeproshopShopModelShop::getContextShopGroup()->share_stock && count(JeproshopAttributeGroupModelAttributeGroup::getAttributesGroups($this->context->language->lang_id)) > 0 && $this->product->hasAttributes(); $this->assignRef('combination_exists', $combination_exists); } } } }