Accessing metafields on storefront in BigCommerce - bigcommerce

I'm currently working on a solution where I need to extend Product information with additional (custom) data. I chose to store it in metafields as they may be more complex than a simple custom field (limit to 250 chars) plus they need to be readonly for admin personell. I want to access these fields in the storefront template.
I see that there's a permission option for metafiels read_and_sf_access (setting storefront access for the metafield) but no info how that can be accessed.
On the other hand the product theme object does not have any attribute for metafields.
How can I access product metafields in a stencil template?

You can add the GraphQL query to fetch the metafields into the Front Matter at the top of the parent page template, for example product.html
And then from there, you can access it like any other handlebars property (under the gql top-level key) - so you may need to pass the result into your component for example.
Here's what that might look like in product.html:
---
product:
videos:
limit: {{theme_settings.productpage_videos_count}}
reviews:
limit: {{theme_settings.productpage_reviews_count}}
related_products:
limit: {{theme_settings.productpage_related_products_count}}
similar_by_views:
limit: {{theme_settings.productpage_similar_by_views_count}}
gql: "query productMetafieldsById($productId: Int!) {
site {
product(entityId: $productId) {
metafields(namespace: \"my-namespace\") {
edges {
node {
key
value
}
}
}
}
}
}
"
You would then access this via {{gql.data.site.product.metafields}}
An easy way to consume it would look like:
{{#each gql.data.site.product.metafields.edges}}
{{#with node}}
{{key}}
{{value}}
{{/with}}
{{/each}}
Metafields will only be available in the storefront if they have a permission_set containing storefront, otherwise they will be hidden from the storefront.
You can also invoke the GraphQL API directly from the Stencil frontend if you prefer, and you can test your queries using the "Storefront API Playground" under Advanced Settings in the control panel.

Related

How can we filter the options of an entity select component in the app config?

We have an app and we would like to configure it to send out emails. On app activation a mail template type and a default mail template are created. However in the configuration we would like to be able to select which mail template we should send out. Our config.xml looks contains the following:
<component name="sw-entity-single-select">
<name>customerMailTemplate</name>
<entity>mail_template</entity>
<label>Choose which mail template to be sent to customers on reservation</label>
<labelProperty>description</labelProperty>
<placeholder>Select mail template</placeholder>
</component>
Our question has 2 parts:
Given that not all mail templates have a description, is there a way how we can include Type + description as labels? similar to the mail templates overview in the admin settings.
We would like to filter this list to only show mail templates of a certain type. I see that this component takes a criteria object but we could only transfer strings from the xml file to the component. Would this be possible?
Having a plugin would allow us to create custom components and add them in the admin, but I don't see how we could do this from an app. If the above are not possible is there a way to create a custom vue component from an app?
Unfortunately neither of those things are possible with apps as of today.
The component has a slot to override the label but since you can't access the template directly with apps, you can only define a single property for the label.
While the component does take a criteria, the config.xml schema is not prepared to take and pass a criteria to the component.
If this is a must-have you could go the route of adding a custom module for you app. This is essentially just an iframe with a source to a page you're hosting. On that page you'd have to build a custom select dropdown. To feed the dropdown with data you request the admin api (with the credentials you received in the app registration process). That's also when you can make use of the criteria filters.
POST /api/search/mail-template
{
"associations": {
"mailTemplateType": []
},
"filters": [
{
"type": "equals",
"field": "mailTemplateType.technicalName",
"value": "order_confirmation_mail"
}
]
}
With the data you received from that endpoint you could then also freely set the labels as you like.
Once the user made a selection you can then either save the selection on your app server or send it back to the admin api, e.g. for storing it in the plugin config.
POST /api/_action/system-config
{
"MyApp.config.customerMailTemplate": "cc4996d68d22421081285fe957f85ec7"
}

How to add additional query parameters in cms page content api call

we want to append the cms page content api used out of box by spartacus for cart page
current: cms/pages?pageType=ContentPage&pageLabelOrId=/cart
required to add some additional query parameter
required: cms/pages?pageType=ContentPage&pageLabelOrId=/cart&staticstorecode=42
what code changes are required to achieve the above scenario?
The page request is made in the load method of the OccCmsPageAdapter. You would need to override this method to add your additional params. You could extend this class and create your own CustomOccCmsPageAdapter and subsequent classes like CustomPage model etc.

bigcommerce stencil display a brand's image

I am trying to display an image but it is not working as expected
{{#each shop_by_brand}}
{{image}} // returns nothing
<img src="{{getImage image "thumbnail"}}"> // src is '[object,object]'
{{/each}}
Surfacing brand images on the homepage would require making a storefront API request.
One way to do this would be to use Stencil utils to make a request to /brands/:
https://stencil.bigcommerce.com/docs/stencil-utils-api-reference#API-js
By default, the request returns HTML, but the function can take a presentation template as an argument; this could be a custom template that filters the page through a {{json brands}} helper.
Another way to access the full brands object in the header would be to use the v3 catalog API. You could use AWS Lambda/Amazon API Gateway as a lightweight solution for hosting a function that calls the API and returns brands.image_url data.
https://developer.bigcommerce.com/api/v3/catalog.html?json#getbrands

How to implement Quick Buy Functionality In BigCommerce Store?

We want to achieve Quick Buy Functionality in Big-commerce Store.  This functionality, we want to achieve on the results of quick search.
Please find attached screenshot for the reference. Customer can quickly add to cart the products with quantity while quick search and finally can checkout from the quick search window itself.
Please refer attached screenshot.
Can anyone help, what changes need to be done in template files to achieve this functionality?
Quick Buy Functionality
BigCommerce Stencil
You can add items to the cart from any page in your store via the AJAX API seen here.
// add item to cart
utils.api.cart.itemAdd(productId, quantity, options, (err, response) => {
let options = {
template: 'cart/preview',
params: {
suggest: response.data.cart_item.id
},
config: {
cart: {
suggestions: {
limit: 4
}
}
}
};
}
BigCommerce Blueprint
In blueprint, it's even easier. Just hit this URL with the products ID.
http://www.STOREURL.com/cart.php?action=add&product_id=PRODUCTID
Or for products with variations.
http://www.STOREURL.com/cart.php?action=add&product_id=PRODUCTID&variation_id=VARIATIONID
Not sure how you'd get the variation IDs in the quicksearch tab, but this is a good start. Also, checkout the quicksearch.js file in the js directory.

How can I use vue.js and wordpress rest api to template a specific page?

Does anyone know how to expand this theme ( https://github.com/gilbitron/wp-rest-theme ) in order to theme specific pages? For example, I would like to create a page called "Menu" which has a unique navigation to click through to child pages Breakfast, Lunch, Dinner, etc without reload. I'm used to creating individual .php files to theme specific pages to my liking via page-{slug}.php - Is there an equivalent workflow using vue.js and wp rest api?
Instead of using page-menu.php to customize the /menu page, I would imagine I'd need to create something like a menu-page.vue file and add a custom method to specifically call that page in order to template it.
I can't find any examples of this process. Any help would be greatly appreciated.
What I did was add a field using Advanced Custom Fields that determined which template the page should use. I wrapped the display in a component called PageContent that looks like this:
<div :is="page.template"
:page="page"
v-if="!$loadingRouteData"
transition="fade"
transition-mode="out-in">
</div>
router:
'/:slug': {
component: PageContent,
name: 'Page'
}
So when someone navigates to that route, I fetch the page based on the slug param. The page variable then has a template attribute through ACF, which determines the component to display and defaults to a generic page:
if(pages[0].acf.template){
return pages[0].acf.template;
}
return 'page'
You need the WP JSON API plugin as well as the ACF plugin that adds ACF data to the json
My site uses this setup, happy to share more code if you have more questions.