/** * @see ObjectModel::delete() */ public function delete() { if (!$this->hasMultishopEntries() || Shop::getContext() == Shop::CONTEXT_ALL) { $result = Db::getInstance()->executeS('SELECT id_product_attribute FROM ' . _DB_PREFIX_ . 'product_attribute_combination WHERE id_attribute = ' . (int) $this->id); $products = array(); foreach ($result as $row) { $combination = new Combination($row['id_product_attribute']); $newRequest = Db::getInstance()->executeS('SELECT id_product, default_on FROM ' . _DB_PREFIX_ . 'product_attribute WHERE id_product_attribute = ' . (int) $row['id_product_attribute']); foreach ($newRequest as $value) { if ($value['default_on'] == 1) { $products[] = $value['id_product']; } } $combination->delete(); } foreach ($products as $product) { $result = Db::getInstance()->executeS('SELECT id_product_attribute FROM ' . _DB_PREFIX_ . 'product_attribute WHERE id_product = ' . (int) $product . ' LIMIT 1'); foreach ($result as $row) { if (Validate::isLoadedObject($product = new Product((int) $product))) { $product->deleteDefaultAttributes(); $product->setDefaultAttribute($row['id_product_attribute']); } } } // Delete associated restrictions on cart rules CartRule::cleanProductRuleIntegrity('attributes', $this->id); /* Reinitializing position */ $this->cleanPositions((int) $this->id_attribute_group); } $return = parent::delete(); if ($return) { Hook::exec('actionAttributeDelete', array('id_attribute' => $this->id)); } return $return; }
public function delete() { if (!$this->hasMultishopEntries()) { $result = Db::getInstance()->executeS('SELECT id_product_attribute FROM ' . _DB_PREFIX_ . 'product_attribute_combination WHERE id_attribute = ' . (int) $this->id); foreach ($result as $row) { $combination = new Combination($row['id_product_attribute']); $combination->delete(); } // Delete associated restrictions on cart rules CartRule::cleanProductRuleIntegrity('attributes', $this->id); /* Reinitializing position */ $this->cleanPositions((int) $this->id_attribute_group); } $return = parent::delete(); if ($return) { Hook::exec('actionAttributeDelete', array('id_attribute' => $this->id)); } return $return; }
public static function cleanDeadCombinations() { $attribute_combinations = Db::getInstance()->executeS(' SELECT pac.`id_attribute`, pa.`id_product_attribute` FROM `' . _DB_PREFIX_ . 'product_attribute` pa LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON (pa.`id_product_attribute` = pac.`id_product_attribute`) '); $to_remove = array(); foreach ($attribute_combinations as $attribute_combination) { if ((int) $attribute_combination['id_attribute'] == 0) { $to_remove[] = (int) $attribute_combination['id_product_attribute']; } } $return = true; if (!empty($to_remove)) { foreach ($to_remove as $remove) { $combination = new Combination($remove); $return &= $combination->delete(); } } return $return; }
public function processUpdate() { $existing_product = $this->object; $this->checkProduct(); if (!empty($this->errors)) { $this->display = 'edit'; return false; } $id = (int) Tools::getValue('id_' . $this->table); /* Update an existing product */ if (isset($id) && !empty($id)) { $object = new $this->className((int) $id); $this->object = $object; if (Validate::isLoadedObject($object)) { $this->_removeTaxFromEcotax(); $product_type_before = $object->getType(); $this->copyFromPost($object, $this->table); $object->indexed = 0; if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_SHOP) { $object->setFieldsToUpdate((array) Tools::getValue('multishop_check')); } // Duplicate combinations if not associated to shop if ($this->context->shop->getContext() == Shop::CONTEXT_SHOP && !$object->isAssociatedToShop()) { $is_associated_to_shop = false; $combinations = Product::getProductAttributesIds($object->id); if ($combinations) { foreach ($combinations as $id_combination) { $combination = new Combination((int) $id_combination['id_product_attribute']); $default_combination = new Combination((int) $id_combination['id_product_attribute'], null, (int) $this->object->id_shop_default); $def = ObjectModel::getDefinition($default_combination); foreach ($def['fields'] as $field_name => $row) { $combination->{$field_name} = ObjectModel::formatValue($default_combination->{$field_name}, $def['fields'][$field_name]['type']); } $combination->save(); } } } else { $is_associated_to_shop = true; } if ($object->update()) { // If the product doesn't exist in the current shop but exists in another shop if (Shop::getContext() == Shop::CONTEXT_SHOP && !$existing_product->isAssociatedToShop($this->context->shop->id)) { $out_of_stock = StockAvailable::outOfStock($existing_product->id, $existing_product->id_shop_default); $depends_on_stock = StockAvailable::dependsOnStock($existing_product->id, $existing_product->id_shop_default); StockAvailable::setProductOutOfStock((int) $this->object->id, $out_of_stock, $this->context->shop->id); StockAvailable::setProductDependsOnStock((int) $this->object->id, $depends_on_stock, $this->context->shop->id); } PrestaShopLogger::addLog(sprintf($this->l('%s edition', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int) $this->object->id, true, (int) $this->context->employee->id); if (in_array($this->context->shop->getContext(), array(Shop::CONTEXT_SHOP, Shop::CONTEXT_ALL))) { if ($this->isTabSubmitted('Shipping')) { $this->addCarriers(); } if ($this->isTabSubmitted('Associations')) { $this->updateAccessories($object); } if ($this->isTabSubmitted('Suppliers')) { $this->processSuppliers(); } if ($this->isTabSubmitted('Features')) { $this->processFeatures(); } if ($this->isTabSubmitted('Combinations')) { $this->processProductAttribute(); } if ($this->isTabSubmitted('Prices')) { $this->processPriceAddition(); $this->processSpecificPricePriorities(); } if ($this->isTabSubmitted('Customization')) { $this->processCustomizationConfiguration(); } if ($this->isTabSubmitted('Attachments')) { $this->processAttachments(); } $this->updatePackItems($object); // Disallow avanced stock management if the product become a pack if ($product_type_before == Product::PTYPE_SIMPLE && $object->getType() == Product::PTYPE_PACK) { StockAvailable::setProductDependsOnStock((int) $object->id, false); } $this->updateDownloadProduct($object, 1); $this->updateTags(Language::getLanguages(false), $object); if ($this->isProductFieldUpdated('category_box') && !$object->updateCategories(Tools::getValue('categoryBox'))) { $this->errors[] = Tools::displayError('An error occurred while linking the object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('To categories'); } } if ($this->isTabSubmitted('Warehouses')) { $this->processWarehouses(); } if (empty($this->errors)) { if (in_array($object->visibility, array('both', 'search')) && Configuration::get('PS_SEARCH_INDEXATION')) { Search::indexation(false, $object->id); } // Save and preview if (Tools::isSubmit('submitAddProductAndPreview')) { $this->redirect_after = $this->getPreviewUrl($object); } else { // Save and stay on same form if ($this->display == 'edit') { $this->confirmations[] = $this->l('Update successful'); $this->redirect_after = self::$currentIndex . '&id_product=' . (int) $this->object->id . (Tools::getIsset('id_category') ? '&id_category=' . (int) Tools::getValue('id_category') : '') . '&updateproduct&conf=4&key_tab=' . Tools::safeOutput(Tools::getValue('key_tab')) . '&token=' . $this->token; } else { // Default behavior (save and back) $this->redirect_after = self::$currentIndex . (Tools::getIsset('id_category') ? '&id_category=' . (int) Tools::getValue('id_category') : '') . '&conf=4&token=' . $this->token; } } } else { $this->display = 'edit'; } } else { if (!$is_associated_to_shop && $combinations) { foreach ($combinations as $id_combination) { $combination = new Combination((int) $id_combination['id_product_attribute']); $combination->delete(); } } $this->errors[] = Tools::displayError('An error occurred while updating an object.') . ' <b>' . $this->table . '</b> (' . Db::getInstance()->getMsgError() . ')'; } } else { $this->errors[] = Tools::displayError('An error occurred while updating an object.') . ' <b>' . $this->table . '</b> (' . Tools::displayError('The object cannot be loaded. ') . ')'; } return $object; } }
/** * Webservice setter : set combination ids of current product for association * * @param $combinations combination ids */ public function setWsCombinations($combinations) { // No hook exec $ids_new = array(); foreach ($combinations as $combination) { $ids_new[] = (int) $combination['id']; } $ids_orig = array(); $original = Db::getInstance()->executeS('SELECT pa.`id_product_attribute` as id FROM `' . _DB_PREFIX_ . 'product_attribute` pa ' . Shop::addSqlAssociation('product_attribute', 'pa') . ' WHERE `id_product` = ' . (int) $this->id); if (is_array($original)) { foreach ($original as $id) { $ids_orig[] = $id['id']; } } $all_ids = array(); $all = Db::getInstance()->executeS('SELECT pa.`id_product_attribute` as id FROM `' . _DB_PREFIX_ . 'product_attribute` pa ' . Shop::addSqlAssociation('product_attribute', 'pa')); if (is_array($all)) { foreach ($all as $id) { $all_ids[] = $id['id']; } } $to_add = array(); foreach ($ids_new as $id) { if (!in_array($id, $ids_orig)) { $to_add[] = $id; } } $to_delete = array(); foreach ($ids_orig as $id) { if (!in_array($id, $ids_new)) { $to_delete[] = $id; } } // Delete rows if (count($to_delete) > 0) { foreach ($to_delete as $id) { $combination = new Combination($id); $combination->delete(); } } foreach ($to_add as $id) { // Update id_product if exists else create if (in_array($id, $all_ids)) { Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product_attribute` SET id_product = ' . (int) $this->id . ' WHERE id_product_attribute=' . $id); } else { Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'product_attribute` (`id_product`) VALUES (' . $this->id . ')'); } } return true; }