【问题标题】:Magento: Display tier prices at category listing pageMagento:在类别列表页面显示等级价格
【发布时间】:2011-08-03 14:51:25
【问题描述】:

我正在尝试在类别列表页面 (catalog/product/list.phtml) 上显示所有等级价格,但只获取单个产品的基本价格。

print_r($_product->getTierPrice());

返回:

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

)

每个产品。在产品信息页面上,它没有问题。请指教。

版本。 1.5.0.1

更新:

以下是受以下答案启发的问题的解决方案:

$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);

【问题讨论】:

  • 对于 Magento 2 有什么需要做的?

标签: magento


【解决方案1】:

这是获取等级价格的另一种方式

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
    )

【讨论】:

  • 这对我也很有帮助 :) 谢谢!
【解决方案2】:

如果您正在寻找 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()

【讨论】:

  • 对于 Magento 2 有什么需要做的?
  • @AnkitShah open another question for Magento 2
【解决方案3】:

自从有人问/回答这个问题已经有一段时间了,但我只需要这样做并找到更好的方法,以免使用直接数据库访问。

如前所述,默认情况下,$_product 对象在产品列表页面上不包含 $_tierPrices ...但是如果您将 tier_price 值设置为 null,它会从数据库中检索实际值并填充对象.因此,无论您需要等级价格,请添加:

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

这应确保在对象中填充层价格,以便您可以在任何页面上使用它。

请记住,这仍然会导致与直接数据库访问相同的性能损失。

【讨论】:

  • 哇,这很奇怪。谢谢。我会试一试的。
【解决方案4】:

Magento 中的所有对象都可能在不同页面上以不同方式创建,并且其中包含不同的数据。起初它可能看起来不直观。碰巧将数据加载到 Item 页面上的 $_product 对象的 db 查询几乎包含“所有”数据。但出于优化目的,类别页面上使用的 $_product 仅包含一些数据 - 如果我没记错的话,它甚至可以从不同的数据库表中提取。例如,类别页面上的查询针对catalogindex* 表连接了一些通常会从常规eav 表中检索的数据。

我没有任何具体信息可提供给您,但您可以查看直接针对 catalog_product_entity_tier_price 表的查询,该表包含所有层级定价。至少那是我的magento版本中的表名,不是1.5。副作用是由于额外的查询/查询,类别页面将需要更长的时间来加载。

【讨论】:

    【解决方案5】:

    我已经尝试了上面的答案和其他一些在线答案,但没有一个有效,所以我将一些技巧合二为一,这就是你可以让它在任何地方工作的方法。我什至有一个自定义主页轮播,它像这样拉它。

    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,因为 Wholesale 默认为 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);
    }
    

    嗯,我就是这样做的。如果您需要任何帮助,请随时与我联系

    【讨论】:

      【解决方案6】:

      在 app/design/frontend/your_theme/default/template/catalog/product/list.phtml 内 将此添加到网格和/或列表的 foreach 循环中

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

      【讨论】:

      • 如果你使用这种方式会影响性能,因为你要为每个循环(产品)再次加载产品
      【解决方案7】:

      转到管理属性并编辑要显示的属性。

      也许产品没有出现在产品列表中,因为它们没有被配置为这样做,所以,在编辑属性页面中,检查它是否被激活:

      用于产品详情

      【讨论】:

      • 我在产品列表页面中显示产品没有问题,我只是无法显示等级价格。 tier_price 属性的“用于产品列表”设置为“是”。
      猜你喜欢
      • 2014-08-05
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      相关资源
      最近更新 更多