且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

试图在opencart中以特殊价格添加jQuery倒数计时器

更新时间:2023-11-29 23:12:52

很好的问题.如您所述,您希望显示的数据已经是OpenCart的admin/后端的一部分,但在前端则不可用.我将向您展示如何添加它.

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.

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

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

    ...
}

太好了,现在有了一个功能getSpecialPriceDates(),您可以调用该函数来查找特殊产品何时结束.让我们将此数据提供给视图.为此,我们将不得不将其添加到Controller中.在ControllerProductProduct中查找设置特殊"变量的位置.

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

...

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

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.

目录/视图/主题/默认/模板/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 } ?>