コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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;
 }