You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Opencart Journal主题加购成功通知添加商品数量显示方案

OpenCart Journal主题购物车通知添加商品数量解决方案

问题概述

  • 环境:OpenCart 3.0.2.0 + Journal 3.1.8主题
  • 需求:商品加入购物车的成功通知需显示添加数量,该通知无对应Twig模板
  • 疑问:是否需要调用common/cart获取数据,以及如何将数量添加到通知消息中

解决方案

无需额外调用common/cart接口,触发通知时的$args参数已包含本次添加的商品数量,直接修改通知控制器的cart()方法即可实现需求:

步骤1:修改cart()方法逻辑

在原控制器的cart()方法中,新增数量提取与消息拼接逻辑:

public function cart($args) {
    if (!$this->journal3->settings->get('notificationStatus')) {
        return false;
    }

    $this->load->language('common/cart');

    if ($args['product_info']['image']) {
        $image = $this->model_journal3_image->resize($args['product_info']['image'], $this->journal3->settings->get('image_dimensions_notification.width'), $this->journal3->settings->get('image_dimensions_notification.height'), $this->journal3->settings->get('image_dimensions_notification.resize'));
        $image2x = $this->model_journal3_image->resize($args['product_info']['image'], $this->journal3->settings->get('image_dimensions_notification.width') * 2, $this->journal3->settings->get('image_dimensions_notification.height') * 2, $this->journal3->settings->get('image_dimensions_notification.resize'));
    } else {
        $image = false;
        $image2x = false;
    }

    // 提取本次添加的商品数量,默认值设为1防止参数缺失
    $quantity = Arr::get($args, 'quantity', 1);
    
    // 自定义消息格式,将数量拼接在原消息前
    $custom_message = "已添加 {$quantity} 件 " . $args['message'];

    return array(
        'className' => 'notification-cart',
        'position'  => $this->journal3->settings->get('cartNotificationStylePosition'),
        'title'     => Arr::get($args, 'product_info.name', 'product_info.option_name'),
        'image'     => $image,
        'image2x'   => $image2x,
        'message'   => $custom_message, // 使用修改后的消息
        'buttons'   => array(
            array(
                'className' => 'btn btn-cart notification-view-cart',
                'name'      => $this->language->get('button_continue_cart'),
                'href'      => $this->url->link('common/home', '', true),
            ),
            array(
                'className' => 'btn btn-success notification-checkout',
                'name'      => $this->language->get('text_checkout'),
                'href'      => $this->url->link('checkout/checkout', '', true),
            ),
        ),
    );
}

步骤2:可选 - 多语言支持优化

若站点需要多语言适配,可在对应语言文件中添加自定义语言项:

  1. 打开catalog/language/你的语言/common/cart.php
  2. 添加一行:
    $_['text_quantity_notice'] = '已添加 %d 件 ';
    
  3. 修改控制器中的消息拼接代码为:
    $custom_message = sprintf($this->language->get('text_quantity_notice'), $quantity) . $args['message'];
    

关键说明

  • Journal主题的通知由前端JS渲染,无需修改Twig模板,控制器返回的message字段直接决定通知显示内容
  • 若$args['quantity']未获取到值,可尝试$args['product_info']['quantity'],具体取决于主题传递参数的逻辑
  • 修改后需清除OpenCart缓存,确保代码生效

内容的提问来源于stack exchange,提问作者Ozgur Yetgin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 17:45:44