PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-为OpenCart中最近添加的产品添加标签大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要在最近添加的产品上贴上“新”标签.我已经在extension / module / latest.tpl上完成了此操作,但是我也希望这个标签也可以在其他页面上使用,例如product / product.tpl,product / category等.

如何在不购买任何扩展的情况下做到这一点?

> OpenCart 2.3
>模板:

更新

/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)
            );
        }

这就是我在product / 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']) { ?>

解决方法:

您可以将产品的date_add与当前时间进行比较,您需要为category修改两个文件
Fisrt文件: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),
);

第二个文件:catalog / view / theme / default / template / product / category.tpl

将此代码添加到产品的foreach循环中,例如之前:< p><?PHP echo $product ['description']; \u0026< / p> :

<?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>sabeti05@gmail.com</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>

大佬总结

以上是大佬教程为你收集整理的php-为OpenCart中最近添加的产品添加标签全部内容,希望文章能够帮你解决php-为OpenCart中最近添加的产品添加标签所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: