public function beforeUpdateOptions() { if (!Tools::getValue('PS_STOCK_MANAGEMENT')) { $_POST['PS_ORDER_OUT_OF_STOCK'] = 1; $_POST['PS_DISPLAY_QTIES'] = 0; } // if advanced stock management is disabled, updates concerned tables if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') == 1 && (int) Tools::getValue('PS_ADVANCED_STOCK_MANAGEMENT') == 0) { ObjectModel::updateMultishopTable('Product', array('advanced_stock_management' => 0), 'product_shop.`advanced_stock_management` = 1'); Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'stock_available` SET `depends_on_stock` = 0, `quantity` = 0 WHERE `depends_on_stock` = 1'); } }
public function beforeUpdateOptions() { if (!Tools::getValue('PS_STOCK_MANAGEMENT', true)) { $_POST['PS_ORDER_OUT_OF_STOCK'] = 1; $_POST['PS_DISPLAY_QTIES'] = 0; } // if advanced stock management is disabled, updates concerned tables if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') == 1 && (int) Tools::getValue('PS_ADVANCED_STOCK_MANAGEMENT') == 0 && Context::getContext()->shop->getContext() == Shop::CONTEXT_ALL) { ObjectModel::updateMultishopTable('Product', array('advanced_stock_management' => 0), 'product_shop.`advanced_stock_management` = 1'); Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'stock_available` SET `depends_on_stock` = 0, `quantity` = 0 WHERE `depends_on_stock` = 1'); } if (Tools::getIsset('PS_CATALOG_MODE')) { Tools::clearSmartyCache(); Media::clearCache(); } }
protected static function setProductsAsIndexed(&$products) { if (count($products)) { ObjectModel::updateMultishopTable('Product', array('indexed' => 1), 'a.id_product IN (' . implode(',', $products) . ')'); } }
public function setProducts($array) { $result = Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'product_tag WHERE id_tag = ' . (int) $this->id); if (is_array($array)) { $array = array_map('intval', $array); $result &= ObjectModel::updateMultishopTable('Product', array('indexed' => 0), 'a.id_product IN (' . implode(',', $array) . ')'); $ids = array(); foreach ($array as $id_product) { $ids[] = '(' . (int) $id_product . ',' . (int) $this->id . ')'; } if ($result) { $result &= Db::getInstance()->execute('INSERT INTO ' . _DB_PREFIX_ . 'product_tag (id_product, id_tag) VALUES ' . implode(',', $ids)); if (Configuration::get('PS_SEARCH_INDEXATION')) { $result &= Search::indexation(false); } } } return $result; }
/** * Post treatment for suppliers */ public function processSuppliers() { if ((int) Tools::getValue('supplier_loaded') === 1 && Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) { // Get all id_product_attribute $attributes = $product->getAttributesResume($this->context->language->id); if (empty($attributes)) { $attributes[] = array('id_product_attribute' => 0, 'attribute_designation' => ''); } // Get all available suppliers $suppliers = Supplier::getSuppliers(); // Get already associated suppliers $associated_suppliers = ProductSupplier::getSupplierCollection($product->id); $suppliers_to_associate = array(); $new_default_supplier = 0; if (Tools::isSubmit('default_supplier')) { $new_default_supplier = (int) Tools::getValue('default_supplier'); } // Get new associations foreach ($suppliers as $supplier) { if (Tools::isSubmit('check_supplier_' . $supplier['id_supplier'])) { $suppliers_to_associate[] = $supplier['id_supplier']; } } // Delete already associated suppliers if needed foreach ($associated_suppliers as $key => $associated_supplier) { if (!in_array($associated_supplier->id_supplier, $suppliers_to_associate)) { $associated_supplier->delete(); unset($associated_suppliers[$key]); } } // Associate suppliers foreach ($suppliers_to_associate as $id) { $to_add = true; foreach ($associated_suppliers as $as) { if ($id == $as->id_supplier) { $to_add = false; } } if ($to_add) { $product_supplier = new ProductSupplier(); $product_supplier->id_product = $product->id; $product_supplier->id_product_attribute = 0; $product_supplier->id_supplier = $id; if ($this->context->currency->id) { $product_supplier->id_currency = (int) $this->context->currency->id; } else { $product_supplier->id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT'); } $product_supplier->save(); $associated_suppliers[] = $product_supplier; } } // Manage references and prices foreach ($attributes as $attribute) { foreach ($associated_suppliers as $supplier) { if (Tools::isSubmit('supplier_reference_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier) || Tools::isSubmit('product_price_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier) && Tools::isSubmit('product_price_currency_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier)) { $reference = pSQL(Tools::getValue('supplier_reference_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier, '')); $price = (double) str_replace(array(' ', ','), array('', '.'), Tools::getValue('product_price_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier, 0)); $price = Tools::ps_round($price, 6); $id_currency = (int) Tools::getValue('product_price_currency_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier, 0); if ($id_currency <= 0 || (!($result = Currency::getCurrency($id_currency)) || empty($result))) { $this->errors[] = Tools::displayError('The selected currency is not valid'); } // Save product-supplier data $product_supplier_id = (int) ProductSupplier::getIdByProductAndSupplier($product->id, $attribute['id_product_attribute'], $supplier->id_supplier); if (!$product_supplier_id) { $product->addSupplierReference($supplier->id_supplier, (int) $attribute['id_product_attribute'], $reference, (double) $price, (int) $id_currency); if ($product->id_supplier == $supplier->id_supplier) { if ((int) $attribute['id_product_attribute'] > 0) { $data = array('supplier_reference' => pSQL($reference), 'wholesale_price' => (double) Tools::convertPrice($price, $id_currency)); $where = ' a.id_product = ' . (int) $product->id . ' AND a.id_product_attribute = ' . (int) $attribute['id_product_attribute']; ObjectModel::updateMultishopTable('Combination', $data, $where); } else { $product->wholesale_price = (double) Tools::convertPrice($price, $id_currency); //converted in the default currency $product->supplier_reference = pSQL($reference); $product->update(); } } } else { $product_supplier = new ProductSupplier($product_supplier_id); $product_supplier->id_currency = (int) $id_currency; $product_supplier->product_supplier_price_te = (double) $price; $product_supplier->product_supplier_reference = pSQL($reference); $product_supplier->update(); } } elseif (Tools::isSubmit('supplier_reference_' . $product->id . '_' . $attribute['id_product_attribute'] . '_' . $supplier->id_supplier)) { //int attribute with default values if possible if ((int) $attribute['id_product_attribute'] > 0) { $product_supplier = new ProductSupplier(); $product_supplier->id_product = $product->id; $product_supplier->id_product_attribute = (int) $attribute['id_product_attribute']; $product_supplier->id_supplier = $supplier->id_supplier; $product_supplier->save(); } } } } // Manage defaut supplier for product if ($new_default_supplier != $product->id_supplier) { $this->object->id_supplier = $new_default_supplier; $this->object->update(); } } }
public function updateOptionPsCurrencyDefault($value) { if ($value == Configuration::get('PS_CURRENCY_DEFAULT')) { return; } Configuration::updateValue('PS_CURRENCY_DEFAULT', $value); /* Set conversion rate of default currency to 1 */ ObjectModel::updateMultishopTable('Currency', array('conversion_rate' => 1), 'a.id_currency'); $tmp_context = Shop::getContext(); if ($tmp_context == Shop::CONTEXT_GROUP) { $tmp_shop = Shop::getContextShopGroupID(); } else { $tmp_shop = (int) Shop::getContextShopID(); } foreach (Shop::getContextListShopID() as $id_shop) { Shop::setContext(Shop::CONTEXT_SHOP, (int) $id_shop); Currency::refreshCurrencies(); } Shop::setContext($tmp_context, $tmp_shop); }
public static function resetEcoTax() { return ObjectModel::updateMultishopTable('product', array('ecotax' => 0), ''); }
public function updateOptionPsCurrencyDefault($value) { Configuration::updateValue('PS_CURRENCY_DEFAULT', $value); /* Set conversion rate of default currency to 1 */ ObjectModel::updateMultishopTable('Currency', array('conversion_rate' => 1), 'a.id_currency'); Currency::refreshCurrencies(); }
/** * @param ShopgateOrder $order * * @return array * @throws PrestaShopException * @throws ShopgateLibraryException */ public function addOrder(ShopgateOrder $order) { /** * check exits shopgate order */ if (ShopgateOrderPrestashop::loadByOrderNumber($order->getOrderNumber())->status == 1) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_DUPLICATE_ORDER, sprintf('external_order_id: %s', $order->getOrderNumber()), true); } /** @var CarrierCore $sgCarrier */ $sgCarrier = new Carrier(Configuration::get('SG_CARRIER_ID')); if (version_compare(_PS_VERSION_, '1.5.0', '<') && empty($sgCarrier->name)) { $sgCarrier->name = 'shopgate_tmp_carrier'; } if ($order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY) { if ($order->getShippingInfos()->getAmount() > 0) { if (version_compare(_PS_VERSION_, '1.5.0', '<')) { ShopgateModObjectModel::updateShippingPrice(pSQL($order->getShippingInfos()->getAmount())); } else { $data = array('price' => pSQL($order->getShippingInfos()->getAmount())); $where = 'a.id_carrier = ' . (int) Configuration::get('SG_CARRIER_ID'); ObjectModel::updateMultishopTable('Delivery', $data, $where); } $sgCarrier->is_free = 0; } else { $sgCarrier->is_free = 1; } } $sgCarrier->update(); $customerModel = new ShopgateItemsCustomerImportJson($this->getPlugin()); $paymentModel = new ShopgatePayment($this->getModule()); $shippingModel = new ShopgateShipping($this->getModule()); /** * read / check customer */ if (!($customerId = Customer::customerExists($order->getMail(), true, false))) { /** * prepare customer */ $shopgateCustomerItem = new ShopgateCustomer(); $shopgateCustomerItem->setLastName($order->getInvoiceAddress()->getLastName()); $shopgateCustomerItem->setFirstName($order->getInvoiceAddress()->getFirstName()); $shopgateCustomerItem->setGender($order->getInvoiceAddress()->getGender()); $shopgateCustomerItem->setBirthday($order->getInvoiceAddress()->getBirthday()); $shopgateCustomerItem->setNewsletterSubscription(Configuration::get('SG_SUBSCRIBE_NEWSLETTER') ? true : false); $customerId = $customerModel->registerCustomer($order->getMail(), md5(_COOKIE_KEY_ . time()), $shopgateCustomerItem); } /** @var CustomerCore $customer */ $customer = new Customer($customerId); /** * prepare cart */ if (!$order->getDeliveryAddress()->getPhone()) { $order->getDeliveryAddress()->setPhone($order->getPhone()); } if (!$order->getInvoiceAddress()->getPhone()) { $order->getInvoiceAddress()->setPhone($order->getPhone()); } $this->getCart()->id_address_delivery = $customerModel->createAddress($order->getDeliveryAddress(), $customer); $this->getCart()->id_address_invoice = $customerModel->createAddress($order->getInvoiceAddress(), $customer); $this->getCart()->id_customer = $customerId; // id_guest is a connection to a ps_guest entry which includes screen width etc. // is_guest field only exists in Prestashop 1.4.1.0 and higher if (version_compare(_PS_VERSION_, '1.4.1.0', '>=')) { $this->getCart()->id_guest = $customer->is_guest; } $this->getCart()->secure_key = $customer->secure_key; $this->getCart()->id_carrier = $shippingModel->getCarrierIdByApiOrder($order); $shopgateCustomFieldsHelper = new ShopgateCustomFieldsHelper(); $shopgateCustomFieldsHelper->saveCustomFields($this->getCart(), $order->getCustomFields()); $this->getCart()->add(); /** * add cart items */ $canCreateOrder = true; $errorMessages = array(); foreach ($order->getItems() as $item) { list($productId, $attributeId) = ShopgateHelper::getProductIdentifiers($item); if ($productId == 0) { continue; } $updateCart = $this->getCart()->updateQty($item->getQuantity(), $productId, $attributeId, false, 'up', $this->getCart()->id_address_delivery); if ($updateCart !== true) { $canCreateOrder = false; $errorMessages[] = array('product_id' => $productId, 'attribute_id' => $attributeId, 'quantity' => $item->getQuantity(), 'result' => $updateCart, 'reason' => $updateCart == -1 ? 'minimum quantity not reached' : ''); } } /** * coupons */ foreach ($order->getExternalCoupons() as $coupon) { /** @var CartRuleCore $cartRule */ $cartRule = new CartRule(CartRule::getIdByCode($coupon->getCode())); if (Validate::isLoadedObject($cartRule)) { $this->getCart()->addCartRule($cartRule->id); $this->getCart()->save(); } } if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) { /** * this field is not available in version 1.4.x.x * set delivery option */ $delivery_option = array($this->getCart()->id_address_delivery => $shippingModel->getCarrierIdByApiOrder($order) . ','); $this->getCart()->setDeliveryOption($delivery_option); } /** * store shopgate order */ $shopgateOrderItem = new ShopgateOrderPrestashop(); $shopgateOrderItem->fillFromOrder($this->getCart(), $order, $this->getPlugin()->getShopgateConfig()->getShopNumber()); if (version_compare(_PS_VERSION_, '1.6.0.0', '<')) { $shopgateOrderItem->add(); } /** * create order */ if ($canCreateOrder) { /** * get first item from order stats */ $this->getCart()->save(); $idOrderState = reset($paymentModel->getOrderStateId($order)); $validateOder = $this->getModule()->validateOrder($this->getCart()->id, $idOrderState, $this->getCart()->getOrderTotal(true, defined('Cart::BOTH') ? Cart::BOTH : 3), $paymentModel->getPaymentTitleByKey($order->getPaymentMethod()), null, array(), null, false, $this->getCart()->secure_key); if (version_compare(_PS_VERSION_, '1.5.0.0', '<') && (int) $this->getModule()->currentOrder > 0 && $order->getShippingType() != ShopgateShipping::DEFAULT_PLUGIN_API_KEY && $order->getShippingInfos()->getAmount() > 0) { ShopgateLogger::log('PS < 1.5.0.0: update shipping and payment cost', ShopgateLogger::LOGTYPE_DEBUG); // in versions below 1.5.0.0 the shipping and payment costs must be updated after the order was imported /** @var OrderCore $updateShopgateOrder */ $updateShopgateOrder = new Order($this->getModule()->currentOrder); $updateShopgateOrder->total_shipping = $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->total_paid += $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->total_paid_real += $order->getAmountShipping() + $order->getAmountShopPayment(); $updateShopgateOrder->update(); } /** * update shopgate order */ if ($validateOder) { $shopgateOrderItem->id_order = $this->getModule()->currentOrder; $shopgateOrderItem->status = 1; $shopgateOrderItem->save(); return array('external_order_id' => $shopgateOrderItem->id_order, 'external_order_number' => $shopgateOrderItem->id_order); } } $shopgateOrderItem->delete(); throw new ShopgateLibraryException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, 'Unable to create order:' . print_r($errorMessages, true), true); }
/** * Check if there is no default attribute and create it if not */ public function checkDefaultAttributes() { if (!$this->product_id) { return false; } $db = JFactory::getDBO(); $query = "SELECT COUNT(*) FROM " . $db->quoteName('#__jeproshop_product_attribute') . " AS product_attribute " . JeproshopShopModelShop::addSqlRestriction('product_attribute') . " WHERE product_attribute_shop."; $query .= $db->quoteName('default_on') . " = 1 AND product_attribute_attribute." . $db->quoteName('product_id') . " = " . (int) $this->product_id; $db->setQuery($query); if ($db->loadResult() > 1) { /*$query = "UPDATE " . $db->quoteName('#__jeproshop_product_attribute_shop') . " SET " . $db->quoteName('default_on') . " WHERE " . $db->quoteName('product_attribute_id'), ' . _DB_PREFIX_ . 'product_attribute pa SET product_attribute_shop.default_on=0, pa.default_on = 0 WHERE product_attribute_shop.id_product_attribute=pa.id_product_attribute AND pa.id_product=' . (int)$this->id . Shop::addSqlRestriction(false, 'product_attribute_shop')); $db->setQuery($query); $db->query();*/ } $query = "SELECT product_attribute." . $db->quoteName('product_id') . " FROM " . $db->quoteName('#__jeproshop_product_attribute') . " AS product_attribute " . JeproshopShopModelShop::addSqlAssociation('product_attribute'); $query .= " WHERE product_attribute_shop." . $db->quoteName('default_on') . " = 1 AND product_attribute." . $db->quoteName('product_id') . " = " . (int) $this->product_id; $db->setQuery($query); $row = $db->loadObject(); if ($row) { return true; } $query = "SELECT MIN (product_attribute." . $db->quoteName('product_attribute_id') . ") AS attribute_id FROM " . $db->quoteName('#__jeproshop_product_attribute') . " AS product_attribute WHERE " . $db->quoteName('product_id') . " = " . (int) $this->product_id; $db->setQuery($query); $mini = $db->loadObject(); if (!$mini) { return false; } if (!ObjectModel::updateMultishopTable('Combination', array('default_on' => 1), 'a.id_product_attribute = ' . (int) $mini['id_attr'])) { return false; } return true; }