草庐IT

php - 在 OpenCart 中为最近添加的产品添加标签

coder 2024-05-02 原文

我需要在最近添加的产品上贴上"new"标签。我在 extension/module/latest.tpl 上完成了此操作,但我也希望此标签也能在其他页面上使用,例如 product/product.tpl产品/类别等等...

如何在不购买任何扩展程序的情况下执行此操作?

  • 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_added在当前时间,您需要为类别修改两个文件: 第一个文件: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']; ?></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 中为最近添加的产品添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41064684/

有关php - 在 OpenCart 中为最近添加的产品添加标签的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 在院子里用@param 标签警告 - 2

    我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  6. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  7. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  8. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  9. css - 用 watir 检查标签类? - 2

    我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes

  10. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

随机推荐