在Prestashop中循环产品属性

时间:2015-04-17 13:29:16

标签: php loops attributes prestashop product

我目前正在寻找Prestashop 1.6.0.14的购物车主题代码,并在构建方面苦苦挣扎。看到这段代码:

<td class="cart_description">
     <div class="narrow-screen">{l s='Description'}</div>
        <div class="floatL">
        {capture name=sep} : {/capture}
        {capture}{l s=' : '}{/capture}
            <p class="product-name"><a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|escape:'html':'UTF-8'}">{$product.name|escape:'html':'UTF-8'}</a></p>
            {if $product.reference}<small class="cart_ref">{l s='SKU'}{$smarty.capture.default}{$product.reference|escape:'html':'UTF-8'}</small>{/if}
            {if isset($product.attributes) && $product.attributes}<small><a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|escape:'html':'UTF-8'}">{$product.attributes|escape:'html':'UTF-8'}</a></small>{/if}
        <div>
    </td>

代码目前以相当庞大的难以理解的方式显示产品属性。 Please observe this image

我想要的是获取每组属性并逐行显示。

  • 颜色:红色
  • 版本:高级
  • 宽度:1米
  • 身高:1米

但是我似乎无法通过我对Prestashop类的有限知识逐一遍历属性。

感谢您的时间和帮助

1 个答案:

答案 0 :(得分:-1)

您需要在cacheSomeAttributesLists中替换( or override )方法classes/Cart.php 我添加了attributes_array以及attributesattributes_small

public static function cacheSomeAttributesLists($ipa_list, $id_lang)
{
    if (!Combination::isFeatureActive())
        return;

    $pa_implode = array();

    foreach ($ipa_list as $id_product_attribute)
        if ((int)$id_product_attribute && !array_key_exists($id_product_attribute.'-'.$id_lang, self::$_attributesLists))
        {
            $pa_implode[] = (int)$id_product_attribute;
            self::$_attributesLists[(int)$id_product_attribute.'-'.$id_lang] = array('attributes' => '', 'attributes_small' => '', 'attributes_array' => '');
        }

    if (!count($pa_implode))
        return;

    $result = Db::getInstance()->executeS('
        SELECT pac.`id_product_attribute`, agl.`public_name` AS public_group_name, al.`name` AS attribute_name
        FROM `'._DB_PREFIX_.'product_attribute_combination` pac
        LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
        LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
        LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (
            a.`id_attribute` = al.`id_attribute`
            AND al.`id_lang` = '.(int)$id_lang.'
        )
        LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (
            ag.`id_attribute_group` = agl.`id_attribute_group`
            AND agl.`id_lang` = '.(int)$id_lang.'
        )
        WHERE pac.`id_product_attribute` IN ('.implode(',', $pa_implode).')
        ORDER BY ag.`position` ASC, a.`position` ASC'
    );

    foreach ($result as $row)
    {
        self::$_attributesLists[$row['id_product_attribute'].'-'.$id_lang]['attributes'] .= $row['public_group_name'].' : '.$row['attribute_name'].', ';
        self::$_attributesLists[$row['id_product_attribute'].'-'.$id_lang]['attributes_small'] .= $row['attribute_name'].', ';
        self::$_attributesLists[$row['id_product_attribute'].'-'.$id_lang]['attributes_array'][] = $row ;
    }

    foreach ($pa_implode as $id_product_attribute)
    {
        self::$_attributesLists[$id_product_attribute.'-'.$id_lang]['attributes'] = rtrim(
            self::$_attributesLists[$id_product_attribute.'-'.$id_lang]['attributes'],
            ', '
        );

        self::$_attributesLists[$id_product_attribute.'-'.$id_lang]['attributes_small'] = rtrim(
            self::$_attributesLists[$id_product_attribute.'-'.$id_lang]['attributes_small'],
            ', '
        );
    }
}

然后,在tpl中的这一行之后

{if isset($product.attributes) && $product.attributes}<small><a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|escape:'html':'UTF-8'}">{$product.attributes|@replace: $smarty.capture.sep:$smarty.capture.default|escape:'html':'UTF-8'}</a></small>{/if}

添加此代码

{if isset($product.attributes_array) && count($product.attributes_array)}
    <small>
        <a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|escape:'html':'UTF-8'}">
            {foreach from=$product.attributes_array item=attribute}
                {$attribute.public_group_name}: {$attribute.attribute_name}<br>
            {/foreach}
        </a>
    </small>
{/if}

如果这对您有用,请附上改进后的显示屏截图,我想将此编辑提交给PrestaShop来源