2016-12-09 80 views
3

我需要爲最近添加的產品標註「新」標籤。我有這個做了extension/module/latest.tpl,但希望我也有這個標籤上的另一個頁面太工作,像product/product.tplproduct/category等等...將標籤添加到OpenCart中最近添加的產品中

我怎麼能做到這一點無需購買任何擴展?

  • Opencart的2.3
  • 模板:默認

UPDATE

/controller/product/category.php

/*start added part*/ 

       if(strtotime($result['date_added']) > (time() - (60*60*24*10))){  
        //(note that 10 means ten days, to consider a product as a new product, you can change it based on your need.) 
        $is_new = true; 
        } else { 
        $is_new = false; 
       } 
      /*end added part*/ 


      $data['products'][] = array(
       'is_new'  => $is_new,    //added code 
       'product_id' => $result['product_id'], 
       'thumb'  => $image, 
       'name'  => $result['name'], 
       'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..', 
       'price'  => $price, 
       'special'  => $special, 
       'tax'   => $tax, 
       'minimum'  => $result['minimum'] > 0 ? $result['minimum'] : 1, 
       'rating'  => $result['rating'], 
       'href'  => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url) 
      ); 
     } 

這是我在做產品/category.tpl

<?php foreach ($products as $product) { ?> 
    <div class="product-layout product-list col-xs-12"> 
     <div class="product-thumb"> 
     <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></a></div> 
     <div> 
      <div class="caption"> 
      <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4> 

      <?php if($product['is_new']){ ?><span>NEW</span><?php } ?> 

      <p><?php echo $product['description']; ?></p> 
      <?php if ($product['price']) { ?> 

回答

5

您可以比較產品date_added與當前時間,你需要修改兩個文件類別: 最前一頁文件catalog/controller/product/category.php

發現:

$data['products'][] = array(

添加之前:

if(strtotime($result['date_added']) > (time() - (60*60*24*10))){ 
    $is_new = true; 
} else { 
    $is_new = false; 
} 

(注意,10意味着十幾天,要考慮產品作爲新產品,您可以根據您的需要改變它)

後添加:

'is_new'  => $is_new, 

現在,這部分必須是這樣的:

if(strtotime($result['date_added']) > (time() - (60*60*24*10))){ 
    $is_new = true; 
} else { 
    $is_new = false; 
} 

$data['products'][] = array(
    'is_new'  => $is_new, 
    'product_id' => $result['product_id'], 
    'thumb'  => $image, 
    'name'  => $result['name'], 
    'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..', 
    'price'  => $price, 
    'special'  => $special, 
    'tax'   => $tax, 
    'minimum'  => $result['minimum'] > 0 ? $result['minimum'] : 1, 
    'rating'  => $result['rating'], 
    'href'  => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url), 
); 

第二個文件<p><?php echo $product['description']; ?></p>catalog/view/theme/default/template/product/category.tpl

之前在代碼里加入foreach循環的產品,例如

<?php if($product['is_new']){ ?><span class="new-product">NEW</span><?php } ?> 

我創建了vqmod一個XML腳本:

<?xml version="1.0" encoding="UTF-8"?> 
<modification> 
    <id>Products New Label</id> 
    <version>2.x</version> 
    <vqmver required="true">2.6.0</vqmver> 
    <author>[email protected]</author> 

    <file name="catalog/controller/product/category.php"> 
     <operation error="skip"> 
      <search position="replace"><![CDATA[$data['products'][] = array(]]></search> 
      <add><![CDATA[ 
       if(strtotime($result['date_added']) > (time() - (60*60*24*10))){ 
        $is_new = true; 
       } else { 
        $is_new = false; 
       } 
       $data['products'][] = array(
        'is_new'  => $is_new, 
      ]]></add> 
     </operation> 
    </file> 

    <file name="catalog/view/theme/*/template/product/category.tpl"> 
     <operation error="skip"> 
      <search position="before"><![CDATA[<p><?php echo $product['description']; ?></p>]]></search> 
      <add><![CDATA[ 
       <?php if($product['is_new']){ ?><span class="new-product">NEW</span><?php } ?> 
      ]]></add> 
     </operation> 
    </file> 

</modification> 
+0

打招呼@Mojtaba Sabeti,感謝您的回覆 我嘗試了第一個解決方案(不使用vqmod),但沒有得到ny的結果,它只是不顯示任何東西(我使用'clear'opencart2.3沒有安裝任何其他擴展)...我把代碼放在我的問題更新(抱歉,如果我做錯了,我是新的stackoverflow: - )),也許我做錯了什麼? 請幫助.... –

+0

我也嘗試vqmod,但仍然沒有結果...我也嘗試使用最新的模塊,它顯示latly增加產品... –

+0

你好,我上面寫的那一天,我測試了我的工作,它工作正常,請耐心等待,我會盡快重新檢查。 – DigitCart

相關問題