Jump to content

Separate the variants of product-variants.tpl


giulym

Recommended Posts

I'm trying to separate the variants of the product-variants.tpl I need this split (and I have to add them on product-special.tpl:

TAB 1

colors: all the colors

TAB 2

colors 2: all the second colors

TAB 3

option 1: A B

TAB 4

option 2: C D

I'm calling the group by ID, but I still see the others groups too.

{elseif $group.group_type == 'color'}
        <ul id="group_1">
          {foreach from=$group.attributes key=id_attribute item=group_attribute}
            <li class="float-xs-left input-container">
              <label>
                <input class="input-color" type="radio" data-product-attribute="{$id_attribute_group}" name="group[1]" value="{$id_attribute}"{if $group_attribute.selected} checked="checked"{/if}>
                <span
                  {if $group_attribute.html_color_code}class="color" style="background-color: {$group_attribute.html_color_code}" {/if}
                  {if $group_attribute.texture}class="color texture" style="background-image: url({$group_attribute.texture})" {/if}
                ><span class="sr-only">{$group_attribute.name}</span></span>
              </label>
            </li>
          {/foreach}
        </ul>

 

Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

Hello there,

I want to have the product variants in the front list of the product so I edited templates/catalog/_partials/miniatures/product.tpl and inserted 

{block name='product_variants'}
{include file='catalog/_partials/product-variants.tpl'}
{/block}

in the according place but I get nothing. 
I troubleshooted it a while and it seems that 

{$groups|@var_dump}

inside templates/catalog/_partials/product-variants.tpl returns NULL while inside the product it returns all the array.

Any hint for what should be done to appear the variants in the front list of the product ?

I am with PS 1.7.5.2 for your reference.

Thank you

Edited by swtiris (see edit history)
Link to comment
Share on other sites

  • 7 months later...
  • 1 month later...

I do not add my solutions, because the rules of the forum are not clear to me. But in this case I guess I can:
 

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class CompsoulMainVariants extends Module {
  public function __construct() {
    $this->name = 'compsoulmainvariants';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'compsoul.pl';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = [
      'min' => '1.7.6',
      'max' => _PS_VERSION_,
    ];
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Compsoul Main Variants');
    $this->description = $this->l('Description of my module.');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

    if (!Configuration::get('COMPSOUL_MAIN_VARIANTS')) {
      $this->warning = $this->l('No name provided');
    }
  }

  public function install() {
    if (Shop::isFeatureActive()) {
      Shop::setContext(Shop::CONTEXT_ALL);
    }

    return (
      parent::install()
      && $this->registerHook('displayCompsoulMainVariants')
      && Configuration::updateValue('COMPSOUL_MAIN_VARIANTS', 'my friend')
    );
  }

  public function uninstall() {
    return (
      parent::uninstall()
      && Configuration::deleteByName('COMPSOUL_MAIN_VARIANTS')
    );
  }

  public function hookDisplayCompsoulMainVariants($params) {
    $product = $this->getProductById($params['product']->id_product);
    $attributes = $product->getAttributesInformationsByProduct($params['product']->id_product);
    $attributesList = $this->getAttributesIdList($attributes, $params['product']->id_product);
    $atributesGroup = array();

    if(!$attributesList) return false;

    foreach ($attributesList as $key => $value) {
      $atributesGroup[$key] = $this->getAttributesGroupById($params['product']->id_product, $value);
      foreach ($atributesGroup[$key] as $index => $result) {
        $atributesGroup[$key][$index]['url'] = $this->getProductAttributeLink($params['product']->id_product, $result['id_product_attribute']);
      }
    }

    $this->context->smarty->assign([
      'compsoul_main_variants_name' => Configuration::get('COMPSOUL_MAIN_VARIANTS'),
      'compsoul_main_variants_link' => $this->context->link->getModuleLink('compsoulmainvariants', 'display'),
      'compsoul_main_variants_atributes' => $atributesGroup,
      'compsoul_main_variants_listing' => $params['listing']
    ]);

    return $this->display(__FILE__, 'compsoulmainvariants.tpl');

  }

  public function getAttributesIdList($attributes = null, $idProduct = null ) {
    if(!$attributes || !$idProduct) return false;

    $attributesIdList = array();
    foreach ($attributes as $key => $value) {
      $attributesIdList[$key] = $value['id_attribute_group'];
    }

    return array_unique($attributesIdList);
  }

  public function getAttributesGroupById($idProduct = null, $idAttributeGroup = null)  {
    if(!$idProduct || !$idAttributeGroup) return false;

    $idLang = $this->context->language->id;
    $checkStock = !Configuration::get('PS_DISP_UNAVAILABLE_ATTR');

    if (!$res = Db::getInstance()->executeS(
        '
        SELECT pa.`id_product`, pac.`id_product_attribute`, ' . ($checkStock ? 'SUM(IF(stock.`quantity` > 0, 1, 0))' : '0') . ' qty, a.`id_attribute`, al.`name`, color, IF(color = "", a.id_attribute, color) group_by
        FROM `' . _DB_PREFIX_ . 'product_attribute` pa
        ' . Shop::addSqlAssociation('product_attribute', 'pa') .
        ($checkStock ? Product::sqlStock('pa', 'pa') : '') . '
        JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON (pac.`id_product_attribute` = product_attribute_shop.`id_product_attribute`)
        JOIN `' . _DB_PREFIX_ . 'attribute` a ON (a.`id_attribute` = pac.`id_attribute`)
        JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = ' . (int) $idLang . ')
        JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON (a.id_attribute_group = ag.`id_attribute_group`)
        WHERE pa.`id_product` IN (' . $idProduct . ') AND ag.`id_attribute_group` = '. $idAttributeGroup .'
        GROUP BY pa.`id_product`, a.`id_attribute`, `group_by`
        ' . ($checkStock ? 'HAVING qty > 0' : '') . '
        ORDER BY a.`position` ASC;'
        )
    ) {
        return false;
    }

    return $res;
  }

  public function getProductById($id = null) {
    if($id) $id = new Product($id, $this->context->language->id);
    return $id;
  }

  public function getProductAttributeLink($idProduct = null, $idProductAttribute = null) {
    $link = new Link();
    $url = null;

    if($idProduct && $idProductAttribute) {
      $url = $link->getProductLink(
        $idProduct,
        null,
        null,
        null,
        $this->context->language->id,
        null,
        $idProductAttribute,
        false,
        false,
        true
      );
    }
    return $url;
  }

}

 

  • 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...