---
title: "折扣优惠"

doc_type: static
doc_category: static
doc_version: v20270301
source_url:
  html: 'https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-features/pricing-and-payment/discounts'
  md: 'https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-features/pricing-and-payment/discounts.md'
---
# 折扣优惠
折扣是商家为促进销售而设定的一种营销手段，它可以应用在多个维度上，如订单、订单商品行、购物车和结账页。你可以在 SHOPLINE 商家后台设置以下类型的折扣优惠：

+ [自动折扣活动](https://help.shopline.com/hc/zh-cn/articles/14181273236761-%E6%8A%98%E6%89%A3%E6%B4%BB%E5%8A%A8)
+ [手动折扣码活动](https://help.shopline.com/hc/zh-cn/articles/14181273236761-%E6%8A%98%E6%89%A3%E6%B4%BB%E5%8A%A8)

---

## 展示折扣信息需要用到的 object
### [discount_application](https://developer.shopline.com/zh-hans-cn/docs/sline/object/discount-application.md)
在 cart 类型的 [模板](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-structure/templates.md) 中，你可以通过 [cart](https://developer.shopline.com/zh-hans-cn/docs/sline/object/cart.md) object 访问以下 `discount_application` 折扣优惠信息：

+ cart.discount_applications
+ cart.cart_level_discount_applications

### [discount_allocation](https://developer.shopline.com/zh-hans-cn/docs/sline/object/discount-allocation.md)
`discount_allocation` 与一个订单商品行关联，因此可以通过 [line_item.line_level_discount_allocations](https://developer.shopline.com/zh-hans-cn/docs/sline/object/line-item.md) 访问直接适用于具体商品行的折扣优惠。

### [line_item](https://developer.shopline.com/zh-hans-cn/docs/sline/object/line-item.md)
代表一个订单商品行，可以通过以下 object 访问：

+ [cart.items](https://developer.shopline.com/zh-hans-cn/docs/sline/object/cart.md)
+ [order.line_items](https://developer.shopline.com/zh-hans-cn/docs/sline/object/order.md)

要展示折扣前和折扣后的价格，你可以使用 [line_item](https://developer.shopline.com/zh-hans-cn/docs/sline/object/line-item.md) 的以下属性：

+ original_price
+ original_line_price
+ final_price
+ final_line_price
+ line_level_total_discount

---

## 在主题中展示折扣信息
以下以购物车为示例，实现在订单商品行和订单小计部分的折扣信息展示。同理，订单详情页也适用以下示例代码。

### 订单商品行展示折扣信息
当你对一个订单商品使用了折扣，就需要展示这个订单商品行上生效的折扣优惠和最终折扣金额。这时，你就需要用到 [line_item.line_level_discount_allocations](https://developer.shopline.com/zh-hans-cn/docs/sline/object/line-item.md) 和 [line_item.final_line_price](https://developer.shopline.com/zh-hans-cn/docs/sline/object/line-item.md)。

#### 输出一个订单商品行的折扣优惠信息
代码示例如下：

```html
{{#for item in cart.items }}
  {{#if item.line_level_discount_allocations | size() }}
    <ul>
      {{#for discount in item.line_level_discount_allocations }}
        <li>
          {{#component "icons/bookmark" /}}
          <span>
            {{ discount.discount_application.title }}
            (-{{{ discount.amount | money_with_currency() }}})
          </span>
        </li>
      {{/for}}
    </ul>
  {{/if}}
{{/for}}
```

#### 输出一个订单商品行的最终折扣金额
代码示例如下：

```html
{{#for item in cart.items }}
  <b>{{{item.final_line_price | money_with_currency()}}}</b>
{{/for}}
```

#### 最终效果
以下是在一个订单商品行展示折扣优惠的效果图：
![1.png](https://cdn.myshopline.com/sl/slfs/sl-new/178062799479203/1.png)


### 购物车页面展示小计折扣
当你对整个购物车使用了折扣，则可以通过 [cart](https://developer.shopline.com/zh-hans-cn/docs/sline/object/cart.md) object 中的 `cart_level_discount_applications` object 去获取整个订单的总折扣优惠金额。代码示例如下所示：

```html
{{#for level_discount_application in cart.cart_level_discount_applications}}
<li>
  <div>
    {{#component "icons/bookmark" /}}
    <em>{{level_discount_application.title}}</em>
  </div>
  <span>-{{{level_discount_application.value | money_with_currency()}}}</span>
</li>
{{/for}}
```

效果图如下：
![2.png](https://cdn.myshopline.com/sl/slfs/sl-new/178062801557959/2.png)

