添加“;从“;如果产品在Opencart类别页面中有选项


Add "starting at" if products have options in Opencart category page

我需要在opencart 的类别页面中显示

如果该产品有选择,起价为xx.xx欧元。

类似这样的东西:

if (have options) {
    print starting at $price
} else {
    print $price }

category.tpl包含:

<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>

我需要插入另一个控制器,如果我们有选项显示"起价…",而不是价格。

谢谢大家,Davide

我在Opencart(v1.5.5.1)中需要一个类似的客户端。下面的代码检查是否有价格上涨的选项,如果有,则在类别页面上显示"From price":

将以下函数添加到''catalog''model''catalog''product.php

public function hasOptionPriceIncrease($product_id) {
  $option_data = $this->getProductOptions($product_id);
  if (is_array($option_data)) {
    foreach ($option_data as $option) {
      if (is_array($option['option_value'])) {
        foreach ($option['option_value'] as $value) {
          if ($value['price'] > 1) {
            return true;
          }
        }  
      }
    }
  }
  return false;
}

在opencart>2.0中使用product_option_value而不是option_value:

public function hasOptionPriceIncrease($product_id) {
  $option_data = $this->getProductOptions($product_id);
    if (is_array($product_option_value)) {
      foreach ($product_option_value as $option) {
      if (is_array($option['product_option_value'])) {
        foreach ($option['product_option_value'] as $value) {
          if ($value['price'] > 1) {
            return true;
          }
        }
      }
    }    
  }
  return false;
}

然后将以下行添加到类别控制器中的$this->data['products'][]阵列:''controller''product''category.php

      'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])

这是在我的250线左右,但你的可能有点不同,它在这里:

$this->data['products'][] = array(
  '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, 100) . '..',
  'price'       => $price,
  'special'     => $special,
  'tax'         => $tax,
  'rating'      => $result['rating'],
  'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
  'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])
);

别忘了在前一行的末尾加上逗号。

然后,您可以在视图中添加类似于以下内容的内容:''catalog''view''them''duvalay''template''product''category.tpl

<p class="price">
 <?php if ($product['has_option_price_increase']) { echo 'From'; } ?>    

jx12345发布的解决方案非常有用,然而,我花了下午的大部分时间试图弄清楚为什么它在OC v2.0.3.1上不起作用,并发现该解决方案只是更改原始数组检查变量的问题。他拥有的原始变量

$option_data 

需要更改为

$product_option_value

因此,添加到模型文件中的最终代码如下所示:

public function hasOptionPriceIncrease($product_id) {
  $product_option_value = $this->getProductOptions($product_id);
    if (is_array($product_option_value)) {
     foreach ($product_option_value as $option) {
      if (is_array($option['product_option_value'])) {
       foreach ($option['product_option_value'] as $value) {
         if ($value['price'] > 1) {
           return true;
         }
       }
     }
   }    
 }
return false;
}

至少这是让代码为我工作所必需的

感谢您的贡献jx12345!