2011-08-03 185 views
4

我試圖在類別列表頁面(catalog/product/list.phtml)上顯示所有等級的價格,但只獲得單個產品的基本價格。Magento:在類別列表頁面顯示等級價格

print_r($_product->getTierPrice()); 

回報:

Array 
(
    [0] => Array 
     (
      [price] => 0.5000 
      [website_price] => 0.5000 
      [price_qty] => 1 
      [cust_group] => 32000 
     ) 

) 

每個產品。在產品信息頁面上,它工作不帶任何問題。請指教。

ver。 1.5.0.1

UPDATE:

這裏是與下面的答案激發了問題的解決方案:

$resource = Mage::getSingleton('core/resource'); 
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product') . '_tier_price'; 
$_tier_prices = $resource->getConnection('core_read')->fetchAll($query); 
var_dump($_tier_prices); 
+0

對於Magento的2有什麼去做? –

回答

1

Magento中的所有對象都可能在不同的頁面上以不同的方式創建,並且在其中具有不同的數據。起初這似乎不直觀。發生這種情況的是,將數據加載到Item頁面上的$ _product對象中的數據庫查詢幾乎包含所有數據。但出於優化目的,類別頁面上使用的$ _product只包含一些數據 - 如果我沒有記錯,它甚至會從不同的數據庫表中抽取。例如,類別頁面上的查詢會加入catalogindex *表中,以查找通常從常規eav表中檢索的一些數據。

我沒有任何具體信息給您,但您可以直接查看具有所有層定價的catalog_product_entity_tier_price表。至少這是我的magento版本中的表名,不是1.5。副作用是由於額外的查詢/查詢,類別頁面需要更長的時間才能加載。

0

轉到管理屬性和編輯您要顯示的屬性。

也許產品不會出現在產品列表中,因爲它們沒有配置要做到這一點,所以,在編輯屬性頁,檢查是否該被激活:

使用的產品清單

+0

我在產品列表頁面顯示產品時沒有問題,我只能顯示分層價格。 tier_price屬性的「產品列表中使用」設置爲「是」。 –

3

已經有一段時間了,因爲這是被問到/回答,但我只是需要這樣做,並找到了一個更好的方法,以便不使用直接的數據庫訪問。

如前所述,默認情況下$ _product對象不包含產品列表頁面上的$ _tierPrices ...但如果您將tier_price值設置爲null,則會從數據庫中檢索實際值並填充目的。所以,無論你需要的一線價格,增加:

$_product->setData('tier_price',null); 
$_tierPrices = $this->getTierPrices($_product); 

這應該保證一線價格在對象填充,這樣你就可以在任何網頁上使用它。

請記住,這仍然會導致與直接db訪問相同的性能下降。

+0

哇,這很奇怪。謝謝。我會試一試。 –

+0

它像一個魅力:) –

1

我已經嘗試了上述和其他一些在線的答案,並沒有一個工作,所以我把幾個黑客放到1,這是如何讓它在任何地方工作。我甚至有一個自定義的主頁旋轉木馬是這樣拉它。

1。你開始通過建立連接附近的某處環外上方

$wh_resource = Mage::getSingleton('core/resource'); 
$wh_readConnection = $wh_resource->getConnection('core_read'); 

2.獲取客戶ID

$wh_customer = Mage::getSingleton('customer/session')->getCustomer(); 
$wh_id = $wh_customer->getGroupId(); 

3.接下來我們進入,你必須循環你的價格回聲

注意:下面的即時通訊使用硬編碼爲2,因爲批發默認爲2。你可以建立自己的if/else &查詢

if($wh_id == 2){ 
//get id 
$_eid = $_helper->productAttribute($_product, $_product->getId(), 'entity_id'); 
$wh_query = 'SELECT * FROM catalog_product_entity_group_price WHERE entity_id = '.$_eid.' LIMIT 1'; 
$wh_results = $wh_readConnection->fetchAll($wh_query); 
//var_dump($wh_results); 
/* var dump would look like this 
array(1) { [0]=> 
    array(6) { 
    ["value_id"]=> string(1) "1" 
    ["entity_id"]=> string(1) "3" 
    ["all_groups"]=> string(1) "0" 
    ["customer_group_id"]=> string(1) "2" 
    ["value"]=> string(6) "9.5000" 
    ["website_id"]=> string(1) "0" 
    } 
} 
*/ 
$wh_price = substr($wh_results[0]["value"], 0, -2); // this damn database gives us extra 00 
echo '<div class="price-box"><span class="regular-price"><span class="price">$'.$wh_price.'</span></span></div>'; 
} else { 
//not wholesale 
echo $this->getPriceHtml($_product, true); 
} 

好,這是我做到了。隨意如果您需要任何幫助

4

如果你正在尋找一個Magento的方式與我聯繫:

$attribute = $_product->getResource()->getAttribute('tier_price'); 

    if ($attribute) { 
     $attribute->getBackend()->afterLoad($_product); 
     $tierPrices = $_product->getTierPrice(); 
    } 

/app/code/core/Mage/Catalog/Model/Product/Type/Price.phpgetTierPrice()

+0

感謝您的幫助,它爲我工作 – Jalpesh

+0

對於Magento 2有什麼要做? –

+0

@AnkitShah爲Magento 2打開另一個問題 – WonderLand

3

這是另一種方式來獲得一線價格

Mage::getResourceModel('catalog/product_attribute_backend_tierprice') 
      ->loadPriceData($product_id,Mage::app()->getWebsite()->getId()); 

響應與價格排列

[0] => Array 
    (
     [price_id] => 7 
     [website_id] => 0 
     [all_groups] => 1 
     [cust_group] => 0 
     [price] => 32.0000 
     [price_qty] => 6.0000 
    ) 

[1] => Array 
    (
     [price_id] => 8 
     [website_id] => 0 
     [all_groups] => 1 
     [cust_group] => 0 
     [price] => 31.0000 
     [price_qty] => 12.0000 
    ) 
+0

謝謝,這是爲我工作的方法。 – Latheesan

1

內部應用程序/設計/前端/ your_theme /默認/模板/目錄/產品/ list.phtml 添加該在foreach循環中的電網和/或列表

<?php $this->setProduct(Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($_product->getId()))?> 
<?php echo $this->getTierPriceHtml() ?> 
+0

如果使用這種方式,它會達到性能,因爲您要爲每個循環(產品)再次加載產品, –

相關問題