Prestashop 1.7模板不传递变量

时间:2018-05-17 16:24:42

标签: prestashop prestashop-1.7

我有一个我无法解决的问题。 在prestashop 1.7我有一个名为product.tpl的tpl文件,其中除了各种数据之外,我用以下代码调用一个文件:

<div class="product-actions">
          {block name='product_buy'}
            <form action="{$urls.pages.cart}" method="post" id="add-to-cart-or-refresh">
              <input type="hidden" name="token" value="{$static_token}">
              <input type="hidden" name="id_product" value="{$product.id}" id="product_page_product_id">
              <input type="hidden" name="id_customization" value="{$product.customization_id}" id="product_customization_id">

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

              {block name='product_pack'}
                {if $packItems}
                  <section class="product-pack">
                    <h3 class="h4">{l s='This pack contains' d='Shop.Theme.Catalog'}</h3>
                    {foreach from=$packItems item="product_pack"}
                      {block name='product_miniature'}
                        {include file='catalog/_partials/miniatures/pack-product.tpl' product=$product_pack}
                      {/block}
                    {/foreach}
                </section>
                {/if}
              {/block}

              {block name='product_discounts'}
                {include file='catalog/_partials/product-discounts.tpl'}
              {/block}

              {block name='product_add_to_cart'}
                {include file='catalog/_partials/product-add-to-cart.tpl'}
              {/block}

              {block name='product_additional_info'}
                {include file='catalog/_partials/product-additional-info.tpl'}
              {/block}

              {block name='product_refresh'}
                <input class="product-refresh ps-hidden-by-js" name="refresh" type="submit" value="{l s='Refresh' d='Shop.Theme.Actions'}">
              {/block}
            </form>
          {/block}

        </div>

一切正常。

我最感兴趣的是本块中提到的代码部分

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

代码是:

<div class="product-variants">
  {foreach from=$groups key=id_attribute_group item=group}
    <div class="clearfix product-variants-item">
      <span class="control-label">{$group.name}</span>
      {if $group.group_type == 'select'}
        <select
          class="form-control form-control-select"
          id="group_{$id_attribute_group}"
          data-product-attribute="{$id_attribute_group}"
          name="group[{$id_attribute_group}]">
          {foreach from=$group.attributes key=id_attribute item=group_attribute}
            <option value="{$id_attribute}" title="{$group_attribute.name}"{if $group_attribute.selected} selected="selected"{/if}>{$group_attribute.name}</option>
          {/foreach}
        </select>
      {elseif $group.group_type == 'color'}
        <ul id="group_{$id_attribute_group}">
          {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[{$id_attribute_group}]" 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>
      {elseif $group.group_type == 'radio'}
        <ul id="group_{$id_attribute_group}">
          {foreach from=$group.attributes key=id_attribute item=group_attribute}
            <li class="input-container float-xs-left">
              <label>
                <input class="input-radio" type="radio" data-product-attribute="{$id_attribute_group}" name="group[{$id_attribute_group}]" value="{$id_attribute}"{if $group_attribute.selected} checked="checked"{/if}>
                <span class="radio-label">{$group_attribute.name}</span>
              </label>
            </li>
          {/foreach}
        </ul>
      {/if}
    </div>
  {/foreach}
</div>

然后我修改了模块ps_featuredproducts的tpl文件,并在主文件“ps_featuredproducts.tpl”中,在foreach中我插入了一部分代码来显示变体和一些产品html设计。

所发生的是我完全可视化格式化,但变体的所有值都不让我看到它们,特别是这部分代码不起作用

<div class="product-variants">
  {foreach from=$groups key=id_attribute_group item=group}
    <div class="clearfix product-variants-item">
      <span class="control-label">{$group.name}</span>
      {if $group.group_type == 'select'}
        <select
          class="form-control form-control-select"
          id="group_{$id_attribute_group}"
          data-product-attribute="{$id_attribute_group}"
          name="group[{$id_attribute_group}]">
          {foreach from=$group.attributes key=id_attribute item=group_attribute}
            <option value="{$id_attribute}" title="{$group_attribute.name}"{if $group_attribute.selected} selected="selected"{/if}>{$group_attribute.name}</option>
          {/foreach}
        </select>
      {elseif $group.group_type == 'color'}
        <ul id="group_{$id_attribute_group}">
          {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[{$id_attribute_group}]" 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>
      {elseif $group.group_type == 'radio'}
        <ul id="group_{$id_attribute_group}">
          {foreach from=$group.attributes key=id_attribute item=group_attribute}
            <li class="input-container float-xs-left">
              <label>
                <input class="input-radio" type="radio" data-product-attribute="{$id_attribute_group}" name="group[{$id_attribute_group}]" value="{$id_attribute}"{if $group_attribute.selected} checked="checked"{/if}>
                <span class="radio-label">{$group_attribute.name}</span>
              </label>
            </li>
          {/foreach}
        </ul>
      {/if}
    </div>
  {/foreach}
</div>

显然,产品页面中的相同代码会回忆所有变量,而在我回忆起主页上内容的家中我无法恢复它们。我哪里错了?我几周前就把手放在prestashop上了,我觉得我的想法很混乱。 我希望我很清楚,感谢那些有耐心回答的人。

2 个答案:

答案 0 :(得分:0)

catalog/_partials/product-variants.tpl中使用的变量$ groups从PoductController.php传递,仅在产品信息页面上可用。您不能在主页或其他地方使用它。如果要获得相同的结果,则需要在模块中创建一些类似的变量并将其传递给您自己的模板。在方法controller/front/ProductController.php中的文件assignAttributesGroups中创建的变量,但它非常复杂,我认为您可以创建更简单的东西。

答案 1 :(得分:0)

ps_featuredproducts中不存在变量$groups。如果要在主页上使用它,则必须将其添加到模块中 为此,您必须像getWidgetVariables()一样修改ps_featuredproducts.php中的函数public function getWidgetVariables($hookName = null, array $configuration = []) { $products = $this->getProducts(); foreach ($products as &$product) { $product_object = new Product((int)$product['id_product']); $groups = array(); $attributes_groups = $product_object->getAttributesGroups($this->context->language->id); if (is_array($attributes_groups) && $attributes_groups) { foreach ($attributes_groups as $k => $row) { 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, ); } $groups[$row['id_attribute_group']]['attributes'][$row['id_attribute']] = array( 'name' => $row['attribute_name'], 'html_color_code' => $row['attribute_color'], 'texture' => (@filemtime(_PS_COL_IMG_DIR_.$row['id_attribute'].'.jpg')) ? _THEME_COL_DIR_.$row['id_attribute'].'.jpg' : '', 'selected' => (isset($product_for_template['attributes'][$row['id_attribute_group']]['id_attribute']) && $product_for_template['attributes'][$row['id_attribute_group']]['id_attribute'] == $row['id_attribute']) ? true : false, ); 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']; } // wash attributes list (if some attributes are unavailables and if allowed to wash it) if (!Product::isAvailableWhenOutOfStock($product_object->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]); } } } } } $product['groups'] = $groups; } if (!empty($products)) { return array( 'products' => $products, 'allProductsLink' => Context::getContext()->link->getCategoryLink($this->getConfigFieldsValues()['HOME_FEATURED_CAT']), ); } return false; }

ProductController::assignAttributesGroups()

我在这里使用函数$groups的部分代码来获取ps_featuredproducts.tpl变量。

最后,在 <div class="product-variants"> {foreach from=$groups key=id_attribute_group item=group} <div class="clearfix product-variants-item"> 中,您必须替换:

  <div class="product-variants">
      {foreach from=$product.groups key=id_attribute_group item=group}
        <div class="clearfix product-variants-item">

人:

@Entity
@Table(name = "tree")
public class Node implements Serializable {
@Id
private int id;
private String name;

@OneToMany(
        cascade = CascadeType.ALL
)
@JoinColumn(name = "parent_id", nullable = true)
@OrderColumn
@JsonManagedReference
private Set<Node> children = new HashSet<>();

@Basic(optional = true)
@ManyToOne(
        fetch = FetchType.LAZY,
        optional = true
)
@JoinColumn(name = "parent_id", nullable = true)
@JsonBackReference
private Node parent;

public void addNode(Node node){
    this.getParent().getChildren().add(node);
    node.setParent(this.getParent());
}

// Getter setter was removed for brevity
}

然后,所有变量都会显示在产品页面上。