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

如何在BigCommerce中以编程方式获取商品变体的purchasable属性?

如何以编程方式判断BigCommerce商品变体是否可购买

BigCommerce并没有直接提供名为purchasable的属性来直接判断商品变体是否可购买,但你可以通过组合变体的几个核心字段来实现这个判断逻辑,不管是用Stencil主题上下文还是GraphQL Storefront API都能做到:

判断核心条件

  • 库存规则:如果变体开启了库存跟踪(inventory_tracking设为variant),需同时满足「库存数量inventory_quantity > 0」或者「允许缺货购买allow_backorders为true」;如果未开启库存跟踪,则不受库存数量限制。
  • 状态有效性:变体本身必须是启用状态(is_active/isActive为true),且所属商品也处于启用状态。
  • 价格有效性:变体必须有有效的定价(price值大于0,特价sale_price同理)。
  • 额外限制:如果店铺设置了会员专属、年龄验证等购买限制,还需结合对应规则补充判断。

不同场景的实现示例

1. Stencil主题模板中判断

在模板里可以直接通过Handlebars语法组合字段判断:

{{#each product.variants}}
  {{#if (and is_active 
            (or (not inventory_tracking) 
                (or (gt inventory_quantity 0) allow_backorders))
            (gt price.value 0))}}
    <span>该变体可购买</span>
  {{else}}
    <span>该变体不可购买</span>
  {{/if}}
{{/each}}

2. GraphQL Storefront API查询后判断

先请求变体的关键字段:

query GetProductVariants($productId: Int!) {
  site {
    product(entityId: $productId) {
      variants {
        edges {
          node {
            entityId
            isActive
            inventoryQuantity
            inventoryTracking
            allowBackorders
            price {
              value
            }
          }
        }
      }
    }
  }
}

然后在你的代码里(比如JS)组合判断:

function isVariantPurchasable(variant) {
  const hasValidStock = variant.inventoryTracking !== 'VARIANT' || 
                       (variant.inventoryQuantity > 0 || variant.allowBackorders);
  const hasValidPrice = variant.price.value > 0;
  const isActive = variant.isActive;

  return hasValidStock && hasValidPrice && isActive;
}

关于你的疑问

你提到的前端选择不可购买商品时的弹出提示,本质是BigCommerce后台基于上述字段的组合逻辑生成的交互,并没有单独的purchasable属性对外暴露,所以需要自行通过上述条件来判断变体是否可购买。

内容的提问来源于stack exchange,提问作者jesus g_force Harris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:31:05