/** * Delete a product attribute * * @param int $idAttribute The attribute ID * @param int $idProduct The product ID * * @return array */ public function ajaxProcessDeleteProductAttribute($idAttribute, $idProduct) { if (!\CombinationCore::isFeatureActive()) { return false; } if ($idProduct && \ValidateCore::isUnsignedId($idProduct) && \ValidateCore::isLoadedObject($product = new \ProductCore($idProduct))) { if (($depends_on_stock = \StockAvailableCore::dependsOnStock($idProduct)) && \StockAvailableCore::getQuantityAvailableByProduct($idProduct, $idAttribute)) { return array('status' => 'error', 'message' => 'It is not possible to delete a combination while it still has some quantities in the Advanced Stock Management. You must delete its stock first.'); } else { $product->deleteAttributeCombination((int) $idAttribute); $product->checkDefaultAttributes(); \ToolsCore::clearColorListCache((int) $product->id); if (!$product->hasAttributes()) { $product->cache_default_attribute = 0; $product->update(); } else { \ProductCore::updateDefaultAttribute($idProduct); } if ($depends_on_stock && !\StockCore::deleteStockByIds($idProduct, $idAttribute)) { return array('status' => 'error', 'message' => 'Error while deleting the stock'); } else { return array('status' => 'ok', 'message' => 'Successful deletion'); } } } else { return array('status' => 'error', 'message' => 'You cannot delete this attribute.'); } }
/** * Get all attributes ids for a given group * * @param int $id_group Attribute group id * @param bool $not_null Get only not null fields if true * @return array Attributes */ public static function getAttributeIdsByGroup($id_group, $not_null = false) { if (!\CombinationCore::isFeatureActive()) { return array(); } $result = \DbCore::getInstance()->executeS(' SELECT DISTINCT a.`id_attribute` FROM `' . _DB_PREFIX_ . 'attribute_group` ag LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute_group` = ag.`id_attribute_group` ' . \ShopCore::addSqlAssociation('attribute_group', 'ag') . ' ' . \ShopCore::addSqlAssociation('attribute', 'a') . ' WHERE ag.`id_attribute_group` = ' . (int) $id_group . ' ' . ($not_null ? 'AND a.`id_attribute` IS NOT NULL' : '') . ' ORDER BY a.`position` ASC '); return array_map(function ($a) { return $a['id_attribute']; }, $result); }
function save_product_combination($ProductId,$itemNumber,$list, $decoration,$color_name){ global $webService,$webservice_exi,$result_xml, $count; deleteImagesToProducts($ProductId); $comb_ids = CombinationCore::getIdsByProductId($ProductId); StockAvailableCore::deleteStockInventoryBYProductID($ProductId); $opt = array('resource' => 'combinations'); foreach ($comb_ids as $comb){ $opt['id'] = (int)$comb['id_product_attribute']; $xml = $webService->delete($opt); } $product_xml = $result_xml->ProductListResult->ProductListResult; $parameter = array("ItemNumber"=>$itemNumber, "key"=>"8770471727"); $i=0; $inventory_details = $webservice_exi->GetInventory($parameter); $color_array = array(); if(isset($inventory_details->GetInventoryResult->InventoryData->SKU)) $inventory_details = $inventory_details->GetInventoryResult; else $inventory_details = $inventory_details->GetInventoryResult->InventoryData; foreach ($inventory_details as $inventory){ attachImages($inventory->SKU, $ProductId); $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/combinations?schema=synopsis')); $resources = $xml -> children() -> children(); $resources->id_product = $ProductId; $resources->quantity = $inventory->Available; if($i==0){ $resources->default_on = true; $i++; } $resources->minimal_quantity = 1; $resources->reference = $inventory->SKU; $color_id = AttributeCore::getColorAttributeIdByValue($inventory->HexValue); if(!$color_id){ $xml1 = $webService -> get(array('url' => PS_SHOP_PATH . '/api/product_option_values?schema=synopsis')); $resources1 = $xml1 -> children() -> children(); $resources1->id_attribute_group = 3; $resources1->color = "#".$inventory->HexValue; $resources1->name->language[0] = $product_xml[$count-1]->Color; $resources1->name->language[1] = $product_xml[$count-1]->Color; try { $opt1 = array('resource' => 'product_option_values'); $opt1['postXml'] = $xml1->asXML(); $xml1 = $webService->add($opt1); $result1 = $xml1->children()->children(); $color_id = $result1->{'id'}; }catch (PrestaShopWebserviceException $ex) { echo "<b>Error al setear la cantidad ->Error : </b>".$ex->getMessage().'<br>'; } } if(in_array($color_id,$color_array)) continue; else array_push($color_array,$color_id); var_dump($color_id); $resources->associations->product_option_values->product_option_values->id=$color_id; try { $opt = array('resource' => 'combinations'); $opt['postXml'] = $xml->asXML(); $xml = $webService->add($opt); getIdStockAvailableAndSet($ProductId, $inventory->Available, $inventory->OnTransit,$inventory->Date,$color_id); $count++; }catch (PrestaShopWebserviceException $ex) { echo "<b>Error al setear la cantidad ->Error : </b>".$ex->getMessage().'<br>'; } $intransit_date = $inventory->Date; if($intransit_date=='N/A') $intransit_date = date("Y-m-d"); StockAvailableCore::setStockInventory($ProductId, 1, 0,$inventory->Available,$color_id, $inventory->OnTransit,$intransit_date); } }
/** * Updates product combinations on Prestashop. * * @param integer $id_product * @param string $url_photo * @param array $triple_cod_col_siz * @param array $array_combinations * @param integer $language * @return bool * @see $this->createAttributeGroups * @see $this->setArrayElementForLinkRewrite * @see $this->getCodeColor * @see PrestashopImageHandler getIdImageByName() * @see PrestashopImageHandler insertImageInPrestashop() * */ private function updateCombinantionsForPrestashop($id_product, $url_photo, $triple_cod_col_siz, $array_combinations, $language = 1) { $product = new Product((int) $id_product); $id_new_images = array(); $price = '0.000'; //(float)$product->price; $reference = $product->reference; $id_supplier = (int) $product->id_supplier; foreach ($triple_cod_col_siz as $triple) { $array_attributes_and_values = $array_combinations[$triple]; $attributes = $array_attributes_and_values["Attributi"]; $values = $array_attributes_and_values["Valori"]; $image = trim($array_attributes_and_values["Immagine"]); $quantity = (int) $array_attributes_and_values["Qta"]; $variable_tmp_attributes = explode(",", $attributes); $variable_tmp_values = explode(",", $values); $id_attributes_for_combinations = array(); $flag_just_exist_color = 1; $flag_just_exist_size = 1; for ($i = 0; $i < sizeof($variable_tmp_attributes); $i++) { $code = ""; if ($variable_tmp_values[$i] != "") { $id_attribute_group = $this->createAttributeGroups($variable_tmp_attributes[$i], $language); $id_attributes_for_not_repeat = $this->getAttributeColorAndSize($variable_tmp_values[$i], $language); if (strtolower($variable_tmp_attributes[$i]) === "colore" || strtolower($variable_tmp_attributes[$i]) === "colori") { $product_attribute_for_not_reply = $product->getAttributeCombinations($language); if (empty($product_attribute_for_not_reply)) { if ($id_attributes_for_not_repeat === '-1') { $code = $this->getCodeColor(strtolower($variable_tmp_values[$i])); if ($code != "") { $code = "#" . $code; $attribute_for_product = new Attribute(); $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language); $attribute_for_product->color = $code; $attribute_for_product->id_attribute_group = $id_attribute_group; $attribute_for_product->add(); array_push($id_attributes_for_combinations, $attribute_for_product->id); } } else { array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat); } } else { $flag = 1; foreach ($product_attribute_for_not_reply as $more_attributes) { if ($more_attributes['attribute_name'] === $variable_tmp_values[$i]) { array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']); $flag = 0; $flag_just_exist_color = 0; break; } } if ($flag) { if ($id_attributes_for_not_repeat === '-1') { $code = $this->getCodeColor(strtolower($variable_tmp_values[$i])); if ($code != "") { $code = "#" . $code; $attribute_for_product = new Attribute(); $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language); $attribute_for_product->color = $code; $attribute_for_product->id_attribute_group = $id_attribute_group; $attribute_for_product->add(); array_push($id_attributes_for_combinations, $attribute_for_product->id); } } else { array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat); } } } } if (strtolower($variable_tmp_attributes[$i]) === "taglia" || strtolower($variable_tmp_attributes[$i]) === "taglie") { $product_attribute_for_not_reply = $product->getAttributeCombinations($language); if (empty($product_attribute_for_not_reply)) { if ($id_attributes_for_not_repeat === '-1') { $attribute_for_product = new Attribute(); $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language); $attribute_for_product->id_attribute_group = $id_attribute_group; $attribute_for_product->add(); array_push($id_attributes_for_combinations, $attribute_for_product->id); } else { array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat); } } else { $flag = 1; foreach ($product_attribute_for_not_reply as $more_attributes) { $vrbls_1 = trim($more_attributes['attribute_name']); $vrbls_2 = trim($variable_tmp_values[$i]); if (gettype($vrbls_1) === "string" && gettype($vrbls_2) === "string") { if ($vrbls_1 === $vrbls_2) { array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']); $flag = 0; $flag_just_exist_size = 0; break; } } if (gettype($vrbls_1) === "integer" && gettype($vrbls_2) === "integer") { if ($vrbls_1 == $vrbls_2) { array_push($id_attributes_for_combinations, (int) $more_attributes['id_attribute']); $flag = 0; $flag_just_exist_size = 0; break; } } } if ($flag) { if ($id_attributes_for_not_repeat === '-1') { $attribute_for_product = new Attribute(); $attribute_for_product->name = $this->setArrayElementForLinkRewrite($variable_tmp_values[$i], true, $language); $attribute_for_product->id_attribute_group = $id_attribute_group; $attribute_for_product->add(); array_push($id_attributes_for_combinations, $attribute_for_product->id); } else { array_push($id_attributes_for_combinations, (int) $id_attributes_for_not_repeat); } } } } } else { if (strtolower($variable_tmp_attributes[$i]) === "colore" || strtolower($variable_tmp_attributes[$i]) === "colori") { $flag_just_exist_color = 0; } if (strtolower($variable_tmp_attributes[$i]) === "taglia" || strtolower($variable_tmp_attributes[$i]) === "taglie") { $flag_just_exist_size = 0; } } } if ($flag_just_exist_color || $flag_just_exist_size) { $id_images = array(); $tmp_photo = explode(".jpg,", $image); for ($i = 0; $i < sizeof($tmp_photo); $i++) { if (!empty($tmp_photo[$i])) { $image_for_prestashop = new PrestashopImageHandler(); $id_image = $image_for_prestashop->getIdImageByName(trim($tmp_photo[$i])); if (!empty($id_image)) { array_push($id_images, $id_image); } else { $id_image = $image_for_prestashop->insertImageInPrestashop($id_product, trim($url_photo), trim($tmp_photo[$i])); array_push($id_images, $id_image); array_push($id_new_images, $id_image . ";" . trim($tmp_photo[$i]) . '.jpg'); } } } $id_product_attributes = $product->addProductAttribute($price, 0, 0, 0, $quantity, "", $reference, $id_supplier, 0, 1); $combinations = new CombinationCore((int) $id_product_attributes); $combinations->setAttributes($id_attributes_for_combinations); $combinations->setImages($id_images); } else { $id_images = array(); $tmp_photo = explode(".jpg,", $image); for ($i = 0; $i < sizeof($tmp_photo); $i++) { if (!empty($tmp_photo[$i])) { $image_for_prestashop = new PrestashopImageHandler(); $id_image = $image_for_prestashop->getIdImageByName(trim($tmp_photo[$i])); if (empty($id_image)) { $id_image = $image_for_prestashop->insertImageInPrestashop($id_product, trim($url_photo), trim($tmp_photo[$i])); array_push($id_images, $id_image); array_push($id_new_images, $id_image . ";" . trim($tmp_photo[$i]) . ".jpg"); } } } if (!empty($id_images)) { $array_product_attribute_combinations_get = $product->getCombinationImages($language); for ($i = 0; $i < sizeof($id_images); $i++) { $id_della_immagine = $id_images[$i]; foreach ($array_product_attribute_combinations_get as $single_array_of_attribute_product) { foreach ($single_array_of_attribute_product as $array_di_combinazioni) { $id_arr_comb = $array_di_combinazioni['id_product_attribute']; $combinazioni_attributi = new CombinationCore((int) $id_arr_comb); $attribute_combinations = $combinazioni_attributi->getAttributesName($language); $attr_comb1 = $attribute_combinations[0]; $attr_comb2 = $attribute_combinations[1]; $id_attr_comb1 = $attr_comb1['id_attribute']; $id_attr_comb2 = $attr_comb2['id_attribute']; $id_attributi_input = $id_attributes_for_combinations; $id_color_or_size = $id_attributi_input[0]; $id_size_or_color = $id_attributi_input[1]; if ($id_color_or_size == $id_attr_comb1 || $id_color_or_size == $id_attr_comb2) { if ($id_size_or_color == $id_attr_comb1 || $id_size_or_color == $id_attr_comb2) { $new_image = array(); $new_array_image = $combinazioni_attributi->getWsImages(); for ($j = 0; $j < sizeof($new_array_image); $j++) { $tmp_image_ = $new_array_image[$j]; array_push($new_image, (int) $tmp_image_['id']); } array_push($new_image, $id_della_immagine); $fff = $product->updateProductAttribute($id_arr_comb, 0, $price, 0, 0, 0, $new_image, $reference, $id_supplier, 0, 1, null, null, 1, '0000-00-00'); } } } } } } } } return $id_new_images; }
/** * Return if Combination feature is active or not * @return bool */ public function combinationIsActive() { return \CombinationCore::isFeatureActive(); }
/** * Get specific prices list for a product * * @param object $product * @param object $defaultCurrency * @param array $shops Available shops * @param array $currencies Available currencies * @param array $countries Available countries * @param array $groups Available users groups * * @return array */ public function getSpecificPricesList($product, $defaultCurrency, $shops, $currencies, $countries, $groups) { $content = []; $specific_prices = \SpecificPriceCore::getByProductId((int) $product->id); $tmp = array(); foreach ($shops as $shop) { $tmp[$shop['id_shop']] = $shop; } $shops = $tmp; $tmp = array(); foreach ($currencies as $currency) { $tmp[$currency['id_currency']] = $currency; } $currencies = $tmp; $tmp = array(); foreach ($countries as $country) { $tmp[$country['id_country']] = $country; } $countries = $tmp; $tmp = array(); foreach ($groups as $group) { $tmp[$group['id_group']] = $group; } $groups = $tmp; if (is_array($specific_prices) && count($specific_prices)) { foreach ($specific_prices as $specific_price) { $id_currency = $specific_price['id_currency'] ? $specific_price['id_currency'] : $defaultCurrency->id; if (!isset($currencies[$id_currency])) { continue; } $current_specific_currency = $currencies[$id_currency]; if ($specific_price['reduction_type'] == 'percentage') { $impact = '- ' . $specific_price['reduction'] * 100 . ' %'; } elseif ($specific_price['reduction'] > 0) { $impact = '- ' . \ToolsCore::displayPrice(\Tools::ps_round($specific_price['reduction'], 2), $current_specific_currency) . ' '; if ($specific_price['reduction_tax']) { $impact .= '(' . $this->translator->trans('Tax incl.', [], 'Admin.Global') . ')'; } else { $impact .= '(' . $this->translator->trans('Tax excl.', [], 'Admin.Global') . ')'; } } else { $impact = '--'; } if ($specific_price['from'] == '0000-00-00 00:00:00' && $specific_price['to'] == '0000-00-00 00:00:00') { $period = $this->translator->trans('Unlimited', [], 'Admin.Global'); } else { $period = $this->translator->trans('From', [], 'Admin.Global') . ' ' . ($specific_price['from'] != '0000-00-00 00:00:00' ? $specific_price['from'] : '0000-00-00 00:00:00') . '<br />' . $this->translator->trans('to', [], 'Admin.Global') . ' ' . ($specific_price['to'] != '0000-00-00 00:00:00' ? $specific_price['to'] : '0000-00-00 00:00:00'); } if ($specific_price['id_product_attribute']) { $combination = new \CombinationCore((int) $specific_price['id_product_attribute']); $attributes = $combination->getAttributesName(1); $attributes_name = ''; foreach ($attributes as $attribute) { $attributes_name .= $attribute['name'] . ' - '; } $attributes_name = rtrim($attributes_name, ' - '); } else { $attributes_name = $this->translator->trans('All combinations', [], 'Admin.Catalog.Feature'); } $rule = new \SpecificPriceRuleCore((int) $specific_price['id_specific_price_rule']); $rule_name = $rule->id ? $rule->name : '--'; if ($specific_price['id_customer']) { $customer = new \CustomerCore((int) $specific_price['id_customer']); if (\ValidateCore::isLoadedObject($customer)) { $customer_full_name = $customer->firstname . ' ' . $customer->lastname; } unset($customer); } if (!$specific_price['id_shop'] || in_array($specific_price['id_shop'], \ShopCore::getContextListShopID())) { $can_delete_specific_prices = true; if (\ShopCore::isFeatureActive()) { $can_delete_specific_prices = count($this->legacyContext->employee->getAssociatedShops()) > 1 && !$specific_price['id_shop'] || $specific_price['id_shop']; } $price = \ToolsCore::ps_round($specific_price['price'], 2); $fixed_price = $price == \ToolsCore::ps_round($product->price, 2) || $specific_price['price'] == -1 ? '--' : \ToolsCore::displayPrice($price, $current_specific_currency); $content[] = ['id_specific_price' => $specific_price['id_specific_price'], 'id_product' => $product->id, 'rule_name' => $rule_name, 'attributes_name' => $attributes_name, 'shop' => $specific_price['id_shop'] ? $shops[$specific_price['id_shop']]['name'] : $this->translator->trans('All shops', [], 'Admin.Global'), 'currency' => $specific_price['id_currency'] ? $currencies[$specific_price['id_currency']]['name'] : $this->translator->trans('All currencies', [], 'Admin.Global'), 'country' => $specific_price['id_country'] ? $countries[$specific_price['id_country']]['name'] : $this->translator->trans('All countries', [], 'Admin.Global'), 'group' => $specific_price['id_group'] ? $groups[$specific_price['id_group']]['name'] : $this->translator->trans('All groups', [], 'Admin.Global'), 'customer' => isset($customer_full_name) ? $customer_full_name : $this->translator->trans('All customers', [], 'Admin.Global'), 'fixed_price' => $fixed_price, 'impact' => $impact, 'period' => $period, 'from_quantity' => $specific_price['from_quantity'], 'can_delete' => !$rule->id && $can_delete_specific_prices ? true : false]; unset($customer_full_name); } } } return $content; }