尝试在opencart中添加jquery倒计时器以获得特价

发布于 2024-12-10 08:46:31 字数 242 浏览 3 评论 0原文

我正在尝试在 opencart 中以特殊价格放置 jquery 倒计时器。

因为我们在开放购物车管理面板中有特价的开始日期和结束日期,所以

我想要 jquery 计数计时器来显示

剩余(days:Hours:Min:SEC)

该特价的

。我得到了 jquery 倒计时的代码并放入产品的模板文件中,但它不起作用,并且互联网上没有帮助或代码。

谢谢

i am trying to put jquery count down timer for special price in opencart.

as we have start date and end date for special price in open cart admin panel,

I want to have jquery count timer to show remaining

(days:Hours:Min:SEC)

for that special price.

i get the code for jquery countdown and put in template file of product but its not working and no help or code on internet.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-12-17 08:46:31

很好的问题。正如您所指出的,您希望显示的数据已经是 OpenCart 管理/后端的一部分,但在前端不可用。我将向您展示如何添加它。

由于 OpenCart 的 MVC 架构,您必须在 3 个地方进行更改。模型、视图和控制器。首先,您必须从数据库中获取数据。因为我们希望对前端进行更改,所以所有内容都将包含在 catalog 目录中。如果您查看代码,您会发现catalog/model/catalog/product.php。这是我们要进行第一个更改的地方。 ModelCatalogProduct 中提供特价,但没有特价结束日期。您可以修改现有的 getProduct() 方法,也可以创建自己的方法。我将向您展示后者,而前者则留给用户作为练习。

catalog/model/catalog/product.php

class ModelCatalogProduct extends Model {
    ...

    // Return an array containing special (price, date_start, date_end).
    // or false if no special price exists.
    public function getSpecialPriceDates($product_id) {
        if ($this->customer->isLogged()) {
            $customer_group_id = $this->customer->getCustomerGroupId();
        } else {
            $customer_group_id = $this->config->get('config_customer_group_id');
        }
        $query = $this->db->query("SELECT price, date_start, date_end FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY priority ASC, price ASC LIMIT 1");

        if ($query->num_rows) {
            return array(
                'special'    => $query->row['price'],
                'date_start' => $query->row['date_start'],
                'date_end'   => $query->row['date_end'],
            );
        } else {
            return false;
        }
    }

    ...
}

太好了,现在有一个函数 getSpecialPriceDates(),您可以调用该函数来了解产品特价何时结束。让我们将该数据提供给视图。为此,我们必须将其添加到控制器中。在 ControllerProductProduct 中查找“特殊”变量的设置位置。

catalog/controller/product/product.php

...

if ((float)$product_info['special']) {
    $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
    // +++ NEW CODE
    $special_info = $this->model_catalog_product->getSpecialPriceDates($product_id);
    if ($special_info) {
        $this->data['special_date_end'] = $special_info['date_end'];
    } else {
        $this->date['special_date_end'] = false;
    }
    // +++ END NEW CODE
} else {
    $this->data['special'] = false;
}

...

最后一个任务是在产品视图中实现计时器。这将位于catalog/view/theme/default/template/product/product.tpl之类的位置(如果您有自己的主题,请将default替换为{你的主题})。 jQuery 有很多不同的倒数计时器解决方案,选择您的最喜欢的

目录/视图/主题/默认/模板/产品/product.tpl

    <?php if (!$special) { ?>
    <?php echo $price; ?>
    <?php } else { ?>
    <span class="price-old"><?php echo $price; ?></span> <span class="price-new"><?php echo $special; ?></span>
      <?php if ($special_date_end): ?>
      <!-- TIMER CODE HERE -->
      <div class="timer"></div>
      <?php endif; ?>
    <?php } ?>

Excellent question. As you noted, the data you wish to display is already part of the admin/backend of OpenCart, but it is not available on the frontend. I'll show you how to add it.

Due to the MVC architecture of OpenCart, you'll have to make changes in 3 places. The Model, the View and the Controller. First things first, you will have to get the data from the database. Because we're looking to make changes to the frontend, everything will be contained in the catalog directory. If you look at the code, you'll find catalog/model/catalog/product.php. This is where we're going to make our first change. Special price is available in the ModelCatalogProduct, but the special price end date is not. You can either modify the existing getProduct() method, or you can create your own method. I am going to show you the latter, while the former is left as an exercise for the user.

catalog/model/catalog/product.php

class ModelCatalogProduct extends Model {
    ...

    // Return an array containing special (price, date_start, date_end).
    // or false if no special price exists.
    public function getSpecialPriceDates($product_id) {
        if ($this->customer->isLogged()) {
            $customer_group_id = $this->customer->getCustomerGroupId();
        } else {
            $customer_group_id = $this->config->get('config_customer_group_id');
        }
        $query = $this->db->query("SELECT price, date_start, date_end FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY priority ASC, price ASC LIMIT 1");

        if ($query->num_rows) {
            return array(
                'special'    => $query->row['price'],
                'date_start' => $query->row['date_start'],
                'date_end'   => $query->row['date_end'],
            );
        } else {
            return false;
        }
    }

    ...
}

Great, now there is a function getSpecialPriceDates() you can call to find out when a product special will end. Let's make this data available to the View. In order to to that, we're going to have to add it to the Controller. Look in the ControllerProductProduct for where the 'special' variable is set.

catalog/controller/product/product.php

...

if ((float)$product_info['special']) {
    $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
    // +++ NEW CODE
    $special_info = $this->model_catalog_product->getSpecialPriceDates($product_id);
    if ($special_info) {
        $this->data['special_date_end'] = $special_info['date_end'];
    } else {
        $this->date['special_date_end'] = false;
    }
    // +++ END NEW CODE
} else {
    $this->data['special'] = false;
}

...

The last task is to implement your timer in the product view. This will be located somewhere like catalog/view/theme/default/template/product/product.tpl (if you have your own theme, replace default with {your-theme}). There are a lot of different countdown timer solutions for jQuery, pick your favorite.

catalog/view/theme/default/template/product/product.tpl

    <?php if (!$special) { ?>
    <?php echo $price; ?>
    <?php } else { ?>
    <span class="price-old"><?php echo $price; ?></span> <span class="price-new"><?php echo $special; ?></span>
      <?php if ($special_date_end): ?>
      <!-- TIMER CODE HERE -->
      <div class="timer"></div>
      <?php endif; ?>
    <?php } ?>
忘东忘西忘不掉你 2024-12-17 08:46:31

查看我为您提供的内容

另请查看这个

更多选项 此处

Check what I got for you

Also check this

More Option over here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文