dongqian0763 2014-03-13 14:05
浏览 36

Opencart将product属性添加到product_list.tpl

What i'm trying to do is edit the product_list.tpl file within Opencart (admin panel) to show the product attribute in a column.

Here is a before and after screenshot of what Im trying to achieve. (done badly in photoshop)

enter image description here

Now, im kinda ok with PHP. i don't need the exact code for laying out in a table.

Currently to make this list is the code below.

 <?php foreach ($products as $product) { ?>
            <tr>
              <td style="text-align: center;"><?php if ($product['selected']) { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" />
                <?php } ?></td>
              <td class="center"><img src="<?php echo $product['image']; ?>" alt="<?php echo $product['name']; ?>"   /></td>
              <td class="left"><?php echo $product['name']; ?></td>
              <td class="left"><?php echo $product['model']; ?></td>
              <td class="left"><?php if ($product['special']) { ?>
                <span style="text-decoration: line-through;"><?php echo $product['price']; ?></span><br/>
                <span style="color: #b00;"><?php echo $product['special']; ?></span>
                <?php } else { ?>
                 <?php echo substr($product['price'], 0, -2); ?> (ex. VAT)
                <?php } ?></td>
              <td class="right"><?php if ($product['quantity'] <= 0) { ?>
                <span style="color: #FF0000;"><?php echo $product['quantity']; ?></span>
                <?php } elseif ($product['quantity'] <= 5) { ?>
                <span style="color: #FFA500;"><?php echo $product['quantity']; ?></span>
                <?php } else { ?>
                <span style="color: #008000;"><?php echo $product['quantity']; ?></span>
                <?php } ?></td>
              <td class="left"><?php echo $product['status']; ?></td>
              <td class="right"><?php foreach ($product['action'] as $action) { ?>
                 <a class="editbutton" href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> 
                <?php } ?></td>
            </tr>
            <?php } ?>

This is the loop that it does foreach product in the list. now within editing a product, you can see the attributes, the handle to get the attributes is

<?php echo $product_attribute['name']; ?>

but if i put this within the Product_list.tpl file, I get php errors, im guessing i have to somehow modify the controller file that serves data to the product_list.tpl and add in a function to get the product attribute, but im unsure on how to do this?

Can anyone help?

  • 写回答

1条回答 默认 最新

  • douyu4822 2014-03-13 20:35
    关注

    Yeah you're right you need to pull the product attributes into the controller (/admin/controller/catalog/product.php) to make them available to the view.

    The slight complication is that a product can have multiple attributes so you'll need to pull them into an array and then iterate over the array in the view.

    The product model (admin/model/catalog/product.php) has a method getProductAttributes($product_id), if you call this from the controller you should be able to pull in the product attributes.

    NB. This controller pulls in all the products in the list so you need to be pulling in the product attributes within the loop that iterates over the list of all the products... in my version this loop starts around line 350 with:

    $results = $this->model_catalog_product->getProducts($data);
    
    foreach ($results as $result) {
    

    Inside this loop you should see the code that loads all the data for each product (around line 375):

    $this->data['products'][] = array(
                    'product_id' => $result['product_id'],
                    'name'       => $result['name'],
                    'model'      => $result['model'],
                    'price'      => $result['price'],
                    'special'    => $special,
                    'image'      => $image,
                    'quantity'   => $result['quantity'],
                    'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                    'selected'   => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
                    'action'     => $action
                );
    

    If you add the call to the model to get the attributes into this:

    $this->data['products'][] = array(
                    'product_id' => $result['product_id'],
                    'name'       => $result['name'],
                    'model'      => $result['model'],
                    'price'      => $result['price'],
                    'special'    => $special,
                    'image'      => $image,
                    'quantity'   => $result['quantity'],
                    'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                    'selected'   => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
                    'action'     => $action,
                    'attributes' => $this->model_catalog_product->getProductAttributes($result['product_id']);
    
                );
    

    You should then have the array $product['attributes'] available to iterate over in the view. Within the loop that iterates over each product:

    <?php 
        foreach ($products as $product) { 
            foreach ($product['attributes'] as $attribute) {
                echo "$attribute<br>";
            }
        }
    ?>
    

    NB. I haven't actaully tested this so copy and pasting might not work but it should get you on the right track.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测