Introducing the Chargebee CLI

How do I set up Chargebee checkout with a BigCommerce store?

Scope

How do I set up Chargebee checkout with a BigCommerce store?

Summary

You can extract the provided zip file and use the sample code below to set up Chargebee checkout with your BigCommerce store.

Solution

Set up basic Chargebee checkout in the BigCommerce theme

  • Download the custom theme package from the resources provided with your Chargebee integration (contact Chargebee Support if you do not have it).
  • Extract it and update the site name in templates/layout/base.html.
  • Replace data-cb-site="cb-ashik-test" with data-cb-site="example-test".
  • Zip the file and upload it to BigCommerce by navigating to Storefront > Theme > Upload theme. If you already have a theme, delete it and upload the new one.
  • Our theme does not update automatically because of caching. To get the latest update, edit any file and save it. Refer to the Cache clearing using Edit Theme Files video instructions.

Refer to this link for video instructions.

Cache clearing using Edit Theme Files

Go to Themes > Edit theme files, then leave a space and Save and Apply the file.

Refer to this link for video instructions.

What we did

Assumptions

For this example, we use the Cornerstone 5.1.0 theme (light). The same configuration can be applied to other themes by updating the equivalent files.

Configure the drop-in script

Get the drop-in script from the Chargebee Admin console and include it in templates/base.html in the theme file.

Drop-in checkout tutorial

The sample code will look like this:

<script src="https://js.chargebee.com/v2/chargebee.js" data-cb-site="bc-demo-test"></script>
<!-- replace bc-demo-test with your site name -->

Create custom fields in your product

Create custom fields in your BigCommerce product so you can map the associated Chargebee product ID.

In this example, we use chargebee_plan_id. You can use any field name, but make sure to update the code accordingly.

Product details page

Add the following code in templates/components/products/add-to-cart.html. It checks whether the chargebee_plan_id custom field exists for the product and, if it does, uses Chargebee checkout; otherwise it falls back to the BigCommerce cart.

The following details are sent from the product details page:

  • Plan ID
  • Plan quantity (reactive)
{{inject 'product' product}}

<script>
    var bcJsContext = JSON.parse({{ jsContext }});
</script>

<script>
    function cbCheckoutInit(event) {
        let cbInstance = Chargebee.getInstance();
        let cart = cbInstance.getCart();
        let [planIdCustomField] = bcJsContext.product.custom_fields.filter(field => field.name === "chargebee_plan_id");
        if (planIdCustomField) {
            document.getElementById("form-action-addToCart").value = 'Subscribe';
        }
        document.getElementById("form-action-addToCart").addEventListener("click", function (event) {
            if (planIdCustomField) {
                event.preventDefault();
                let productQty = document.querySelector('.form-input--incrementTotal').value;
                let product = cbInstance.initializeProduct(planIdCustomField.value);
                product.isItemsModel = true; // Set this flag to true only if the site is Latest Product Catalog
                product.setPlanQuantity(productQty);
                cart.replaceProduct(product);
                cart.proceedToCheckout();
            }
        });
    }

    let cbInitCheckerIntervalId;
    cbInitCheckerIntervalId = setInterval(() => {
        if (Chargebee && Chargebee.inited) {
            clearInterval(cbInitCheckerIntervalId);
            cbCheckoutInit();
        }
    }, 100);
</script>

Listing

card.html

Pass custom_fields property of product to the card from where ever it's called

Like this:

// top.html
  {{>components/products/card settings=../settings theme_settings=../theme_settings customer=../customer event="list" position=(add @index 1) custom_fields=this.custom_fields}}
// Do the same in featured.html, grid.html, related.html, carousel.html as well.

top.html

Refer to this link for video instructions

Similarly, do it for

  • components/products/featured.html

  • components/products/grid.html

  • components/products/related.html

  • components/products/carousel.html

Card.html

In card.html, replace the #if add_to_cart_url block with the following sample code.

Legacy Product Catalog

  {{#each custom_fields}}
    {{#if this.name '===' 'chargebee_plan_id'}}
        {{assignVar "chargebeePlanId" this.value}}
        <a href="javascript:void(0)" 
           data-cb-type="checkout" 
           data-cb-plan-id="{{this.value}}"  
           class="button button--small card-figcaption-button">
            Subscribe
        </a>
    {{else}}
        {{#if add_to_cart_url}}
            <a href="{{add_to_cart_url}}" 
               data-event-type="product-click" 
               data-button-type="add-cart"
               class="button button--small card-figcaption-button">
                {{lang 'products.add_to_cart'}}
            </a>
            <span class="product-status-message aria-description--hidden">
                {{lang 'category.add_cart_announcement'}}
            </span>
        {{/if}}
    {{/if}}
{{/each}}

{{#unless custom_fields}}
    {{#if add_to_cart_url}}
        <a href="{{add_to_cart_url}}" 
           data-event-type="product-click" 
           data-button-type="add-cart" 
           class="button button--small card-figcaption-button">
            {{lang 'products.add_to_cart'}}
        </a>
        <span class="product-status-message aria-description--hidden">
            {{lang 'category.add_cart_announcement'}}
        </span>
    {{/if}}
{{/unless}}

Latest Product Catalog

The only change here we're replacing data-cb-plan-id with data-cb-item-0

{{#each custom_fields}}
    {{#if this.name '===' 'chargebee_plan_id'}}
        {{assignVar "chargebeePlanId" this.value}}
        <a href="javascript:void(0)" 
           data-cb-type="checkout" 
           data-cb-item-0="{{this.value}}"  
           class="button button--small card-figcaption-button">
           Subscribe
        </a>
    {{else}}
        {{#if add_to_cart_url}}
            <a href="{{add_to_cart_url}}" 
               data-event-type="product-click" 
               data-button-type="add-cart"
               class="button button--small card-figcaption-button">
               {{lang 'products.add_to_cart'}}
            </a>
            <span class="product-status-message aria-description--hidden">
                {{lang 'category.add_cart_announcement'}}
            </span>
        {{/if}}
    {{/if}}
{{/each}}

{{#unless custom_fields}}
    {{#if add_to_cart_url}}
        <a href="{{add_to_cart_url}}" 
           data-event-type="product-click" 
           data-button-type="add-cart" 
           class="button button--small card-figcaption-button">
           {{lang 'products.add_to_cart'}}
        </a>
        <span class="product-status-message aria-description--hidden">
            {{lang 'category.add_cart_announcement'}}
        </span>
    {{/if}}
{{/unless}}

Refer to this link for video instructions

Applying theme

And make sure to apply the uploaded theme. Alternatively, if you're using Stencil based theme then you can use CLI to easily upload the theme and the development work locally

https://developer.bigcommerce.com/docs/storefront/stencil

Limitations

  • We do not support Multi subscription support yet hence the end user will be able to subscribe to only one product (subscription) at a time.

  • This customization is not straightforward and needs a developer to make these changes.

References

https://developer.bigcommerce.com/docs/storefront/stencil/themes/context/handlebars-reference#contains