2016-11-10 186 views
0

嗨有誰知道是否有辦法選擇哪些特色產品首先展示?我如何在特色模塊中分類產品?像最新到最早的那樣.. 這是模塊的代碼。我如何設置產品的排序順序?如何爲opencart中的特色產品添加排序順序?

$data['heading_title'] = $this->language->get('heading_title'); 

    $data['text_tax'] = $this->language->get('text_tax'); 

    $data['button_cart'] = $this->language->get('button_cart'); 
    $data['button_wishlist'] = $this->language->get('button_wishlist'); 
    $data['button_compare'] = $this->language->get('button_compare'); 

    $this->load->model('catalog/product'); 

    $this->load->model('tool/image'); 

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

    if (!$setting['limit']) { 
     $setting['limit'] = 4; 
    } 

    if (!empty($setting['product'])) { 
     $products = array_slice($setting['product'], 0, (int)$setting['limit']); 

     foreach ($products as $product_id) { 
      $product_info = $this->model_catalog_product->getProduct($product_id); 

      if ($product_info) { 
       if ($product_info['image']) { 
        $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']); 
       } else { 
        $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']); 
       } 

       if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) { 
        $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))); 
       } else { 
        $price = false; 
       } 

       if ((float)$product_info['special']) { 
        $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); 
       } else { 
        $special = false; 
       } 

       if ($this->config->get('config_tax')) { 
        $tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']); 
       } else { 
        $tax = false; 
       } 

       if ($this->config->get('config_review_status')) { 
        $rating = $product_info['rating']; 
       } else { 
        $rating = false; 
       } 

       $data['products'][] = array(
        'product_id' => $product_info['product_id'], 
        'thumb'  => $image, 
        'name'  => $product_info['name'], 
        'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..', 
        'price'  => $price, 
        'special'  => $special, 
        'tax'   => $tax, 
        'rating'  => $rating, 
        'href'  => $this->url->link('product/product', 'product_id=' . $product_info['product_id']) 
       ); 
      } 
     } 
    } 

    if ($data['products']) { 
     if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) { 
      return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data); 
     } else { 
      return $this->load->view('default/template/module/featured.tpl', $data); 
     } 
    } 
} 

}

+2

你有任何的代碼,你有什麼嘗試,你今天吃午餐,月球有多遠?可能需要更多的信息伴侶。 – Blinkydamo

+0

我在互聯網上發現了一些代碼,但我不知道哪個文件和哪一行我應該添加它。我發現這個「ORDER BY p.date_added DESC」,但我不知道如何或是否正確。 – Neconeco

+0

搭檔也沒有幫助,您需要閱讀並更好地理解您下載的代碼。如果我們無法看到我們無法幫助您的代碼。 – Blinkydamo

回答

1

在Opencart的每一個產品都 「數據」 選項卡上的 「排序順序」 域,您可以填寫它,當您添加或編輯一個產品:

Opencart Sort order field for product

轉至此文件:

catalog/controller/module/featured.php 

將這些行添加到產品陣列

'sort_order' => $product_info['sort_order'], 
'date_added' => $product_info['date_added'] 

,並使用array_multisort波紋管:

$temp_array = array(); 
foreach ($data['products'] as $key => $row){ 
    $temp_array[$key] = $row['sort_order']; 
} 
array_multisort($temp_array, SORT_ASC, $data['products']); 

插入上面的代碼只是if ($data['products']) {

這裏之前是全碼:

<?php 
class ControllerModuleFeatured extends Controller { 
    public function index($setting) { 
     $this->load->language('module/featured'); 

     $data['heading_title'] = $this->language->get('heading_title'); 

     $data['text_tax'] = $this->language->get('text_tax'); 

     $data['button_cart'] = $this->language->get('button_cart'); 
     $data['button_wishlist'] = $this->language->get('button_wishlist'); 
     $data['button_compare'] = $this->language->get('button_compare'); 

     $this->load->model('catalog/product'); 

     $this->load->model('tool/image'); 

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

     if (!$setting['limit']) { 
      $setting['limit'] = 4; 
     } 

     if (!empty($setting['product'])) { 
      $products = array_slice($setting['product'], 0, (int)$setting['limit']); 

      foreach ($products as $product_id) { 
       $product_info = $this->model_catalog_product->getProduct($product_id); 

       if ($product_info) { 
        if ($product_info['image']) { 
         $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']); 
        } else { 
         $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']); 
        } 

        if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) { 
         $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'))); 
        } else { 
         $price = false; 
        } 

        if ((float)$product_info['special']) { 
         $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'))); 
        } else { 
         $special = false; 
        } 

        if ($this->config->get('config_tax')) { 
         $tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']); 
        } else { 
         $tax = false; 
        } 

        if ($this->config->get('config_review_status')) { 
         $rating = $product_info['rating']; 
        } else { 
         $rating = false; 
        } 

        $data['products'][] = array(
         'product_id' => $product_info['product_id'], 
         'thumb'  => $image, 
         'name'  => $product_info['name'], 
         'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..', 
         'price'  => $price, 
         'special'  => $special, 
         'tax'   => $tax, 
         'rating'  => $rating, 
         'href'  => $this->url->link('product/product', 'product_id=' . $product_info['product_id']), 
         // Add 'sort_order' and 'date_added' to products array 
         'sort_order' => $product_info['sort_order'], 
         'date_added' => $product_info['date_added'] 
        ); 
       } 
      } 
     } 

     // Create a temporary array 
     $temp_array = array(); 
     foreach ($data['products'] as $key => $row){ 
      // You can use 'date_added' or 'sort_order' or 'price' or ... 
      $temp_array[$key] = $row['date_added']; 
     } 
     // You can use SORT_ASC or SORT_DESC 
     array_multisort($temp_array, SORT_ASC, $data['products']); 


     if ($data['products']) { 
      if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/featured.tpl')) { 
       return $this->load->view($this->config->get('config_template') . '/template/module/featured.tpl', $data); 
      } else { 
       return $this->load->view('default/template/module/featured.tpl', $data); 
      } 
     } 
    } 
} 

我的Opencart的2.1測試這.0.1, 希望這對你有所幫助。

+0

Still產品已在舊版本的基礎上增加了舊版本,並在底部新增了新版本。我複製完整的代碼仍然相同。也許它在管理文件夾中具有相同名稱的文件 – Neconeco

+0

現在可以工作。我將SORT_ASC更改爲desc,瞧!非常感謝你。 – Neconeco

+0

很高興聽到它的工作,不客氣。 – DigitCart