Jump to content

Product.tpl modifier variable $groups


Recommended Posts

Bonjour,

 

Question très simple pour les développeurs aguerris Prestashop (que je ne suis pas) : comment modifier le contenu de la variable $groups dans product.tpl !

Je n'arrive pas à trouver dans Product.php une requête ou une variable similaire.

 

Le contexte : j'ai modifié la classe Attributes & la BDD (et bien d'autres choses) pour ajouter une constante "date" dans la table "ps_attributes" ex : 2017-10-18. Le soucis c'est que la variable $groups me sort le contenu de la table "ps_attribute_lang" ex : Mercredi 18 Octobre 2017 à 10h.

 

Hors, j'ai besoin d'avoir la date pour effectuer des requêtes dessus (cacher les dates antérieures dans le cas présent).

<div class="product_attributes clearfix">
 <pre>{$groups|var_dump}</pre>
 {if isset($groups)}
  <!-- attributes -->
  <div id="attributes">
  [...]

Par exemple le var_dump me sort ceci :

array(1) {
  [1]=>
  array(6) {
    ["group_name"]=>
    string(4) "Date"
    ["name"]=>
    string(4) "Date"
    ["group_type"]=>
    string(4) "date"
    ["default"]=>
    int(44)
    ["attributes"]=>
    array(6) {
      [44]=>
      string(28) "Jeudi 19 octobre 2017 à 10h"
      [70]=>
      string(29) "Jeudi 02 novembre 2017 à 10h"
      [88]=>
      string(29) "Lundi 13 novembre 2017 à 15h"
      [130]=>
      string(33) "Vendredi 08 décembre 2017 à 15h"
      [163]=>
      string(30) "Jeudi 28 décembre 2017 à 15h"
      [194]=>
      string(21) "Lundi 09 Octobre 2017"
    }
    ["attributes_quantity"]=>
    array(6) {
      [44]=>
      int(4)
      [70]=>
      int(4)
      [88]=>
      int(4)
      [130]=>
      int(4)
      [163]=>
      int(4)
      [194]=>
      int(0)
    }
  }
}

Hors, je voudrais comme je l'ai souligné plus haut, le contenu "date" de ma table "ps_attributes". 

Help !

Link to comment
Share on other sites

Je me répond à moi même. Encore. Décidément.

 

La variable $groups se situe dans le Controller, et non pas dans la Class.

De ce fait, une simple surchage m'a permis de gérer mes dates :

<?php
/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2016 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

class ProductController extends ProductControllerCore{
  /**
   * Assign template vars related to attribute groups and colors
   */
  protected function assignAttributesGroups()
  {
      $colors = array();
      $groups = array();

      // @todo (RM) should only get groups and not all declination ?
      $attributes_groups = $this->product->getAttributesGroups($this->context->language->id);
      if (is_array($attributes_groups) && $attributes_groups) {
          $combination_images = $this->product->getCombinationImages($this->context->language->id);
          $combination_prices_set = array();
          foreach ($attributes_groups as $k => $row) {
              // Color management
              if (isset($row['is_color_group']) && $row['is_color_group'] && (isset($row['attribute_color']) && $row['attribute_color']) || (file_exists(_PS_COL_IMG_DIR_.$row['id_attribute'].'.jpg'))) {
                  $colors[$row['id_attribute']]['value'] = $row['attribute_color'];
                  $colors[$row['id_attribute']]['name'] = $row['attribute_name'];
                  if (!isset($colors[$row['id_attribute']]['attributes_quantity'])) {
                      $colors[$row['id_attribute']]['attributes_quantity'] = 0;
                  }
                  $colors[$row['id_attribute']]['attributes_quantity'] += (int)$row['quantity'];
              }
              if (!isset($groups[$row['id_attribute_group']])) {
                  $groups[$row['id_attribute_group']] = array(
                      'group_name' => $row['group_name'],
                      'name' => $row['public_group_name'],
                      'group_type' => $row['group_type'],
                      'default' => -1,
                  );
              }

              /* edit date */
              $date = new DateTime();
              if($date->format('Y-m-d') < $row['date'])
                $groups[$row['id_attribute_group']]['attributes'][$row['id_attribute']] = $row['attribute_name'];
              $groups[$row['id_attribute_group']]['date'][$row['id_attribute']] = $row['date'];
              /* fin edit date */
              if ($row['default_on'] && $groups[$row['id_attribute_group']]['default'] == -1) {
                  $groups[$row['id_attribute_group']]['default'] = (int)$row['id_attribute'];
              }
              if (!isset($groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']])) {
                  $groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']] = 0;
              }
              $groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']] += (int)$row['quantity'];

              $combinations[$row['id_product_attribute']]['attributes_values'][$row['id_attribute_group']] = $row['attribute_name'];
              $combinations[$row['id_product_attribute']]['attributes'][] = (int)$row['id_attribute'];
              $combinations[$row['id_product_attribute']]['price'] = (float)Tools::convertPriceFull($row['price'], null, Context::getContext()->currency, false);

              // Call getPriceStatic in order to set $combination_specific_price
              if (!isset($combination_prices_set[(int)$row['id_product_attribute']])) {
                  Product::getPriceStatic((int)$this->product->id, false, $row['id_product_attribute'], 6, null, false, true, 1, false, null, null, null, $combination_specific_price);
                  $combination_prices_set[(int)$row['id_product_attribute']] = true;
                  $combinations[$row['id_product_attribute']]['specific_price'] = $combination_specific_price;
              }
              $combinations[$row['id_product_attribute']]['ecotax'] = (float)$row['ecotax'];
              $combinations[$row['id_product_attribute']]['weight'] = (float)$row['weight'];
              $combinations[$row['id_product_attribute']]['quantity'] = (int)$row['quantity'];
              $combinations[$row['id_product_attribute']]['reference'] = $row['reference'];
              $combinations[$row['id_product_attribute']]['unit_impact'] = Tools::convertPriceFull($row['unit_price_impact'], null, Context::getContext()->currency, false);
              $combinations[$row['id_product_attribute']]['minimal_quantity'] = $row['minimal_quantity'];
              if ($row['available_date'] != '0000-00-00' && Validate::isDate($row['available_date'])) {
                  $combinations[$row['id_product_attribute']]['available_date'] = $row['available_date'];
                  $combinations[$row['id_product_attribute']]['date_formatted'] = Tools::displayDate($row['available_date']);
              } else {
                  $combinations[$row['id_product_attribute']]['available_date'] = $combinations[$row['id_product_attribute']]['date_formatted'] = '';
              }

              if (!isset($combination_images[$row['id_product_attribute']][0]['id_image'])) {
                  $combinations[$row['id_product_attribute']]['id_image'] = -1;
              } else {
                  $combinations[$row['id_product_attribute']]['id_image'] = $id_image = (int)$combination_images[$row['id_product_attribute']][0]['id_image'];
                  if ($row['default_on']) {
                      if (isset($this->context->smarty->tpl_vars['cover']->value)) {
                          $current_cover = $this->context->smarty->tpl_vars['cover']->value;
                      }

                      if (is_array($combination_images[$row['id_product_attribute']])) {
                          foreach ($combination_images[$row['id_product_attribute']] as $tmp) {
                              if ($tmp['id_image'] == $current_cover['id_image']) {
                                  $combinations[$row['id_product_attribute']]['id_image'] = $id_image = (int)$tmp['id_image'];
                                  break;
                              }
                          }
                      }

                      if ($id_image > 0) {
                          if (isset($this->context->smarty->tpl_vars['images']->value)) {
                              $product_images = $this->context->smarty->tpl_vars['images']->value;
                          }
                          if (isset($product_images) && is_array($product_images) && isset($product_images[$id_image])) {
                              $product_images[$id_image]['cover'] = 1;
                              $this->context->smarty->assign('mainImage', $product_images[$id_image]);
                              if (count($product_images)) {
                                  $this->context->smarty->assign('images', $product_images);
                              }
                          }
                          if (isset($this->context->smarty->tpl_vars['cover']->value)) {
                              $cover = $this->context->smarty->tpl_vars['cover']->value;
                          }
                          if (isset($cover) && is_array($cover) && isset($product_images) && is_array($product_images)) {
                              $product_images[$cover['id_image']]['cover'] = 0;
                              if (isset($product_images[$id_image])) {
                                  $cover = $product_images[$id_image];
                              }
                              $cover['id_image'] = (Configuration::get('PS_LEGACY_IMAGES') ? ($this->product->id.'-'.$id_image) : (int)$id_image);
                              $cover['id_image_only'] = (int)$id_image;
                              $this->context->smarty->assign('cover', $cover);
                          }
                      }
                  }
              }
          }

          // wash attributes list (if some attributes are unavailables and if allowed to wash it)
          if (!Product::isAvailableWhenOutOfStock($this->product->out_of_stock) && Configuration::get('PS_DISP_UNAVAILABLE_ATTR') == 0) {
              foreach ($groups as &$group) {
                  foreach ($group['attributes_quantity'] as $key => &$quantity) {
                      if ($quantity <= 0) {
                          unset($group['attributes'][$key]);
                      }
                  }
              }

              foreach ($colors as $key => $color) {
                  if ($color['attributes_quantity'] <= 0) {
                      unset($colors[$key]);
                  }
              }
          }
          foreach ($combinations as $id_product_attribute => $comb) {
              $attribute_list = '';
              foreach ($comb['attributes'] as $id_attribute) {
                  $attribute_list .= '\''.(int)$id_attribute.'\',';
              }
              $attribute_list = rtrim($attribute_list, ',');
              $combinations[$id_product_attribute]['list'] = $attribute_list;
          }

          $this->context->smarty->assign(array(
              'groups' => $groups,
              'colors' => (count($colors)) ? $colors : false,
              'combinations' => $combinations,
              'combinationImages' => $combination_images
          ));
      }
  }
}

Edited by Shonen (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...