Is there any way I can access an item custom field...
# suitecommerce
d
Is there any way I can access an item custom field in Cart > cart_add_to_cart.tpl?
s
Sure. If it's not already exposed, you will need to use the
addToViewContextDefinition()
method to add the field you want to the view that uses that template
d
Thanks Steve. The problem I have is this works with a parent view, but not the one used by the template. Also, this template can be displayed anything up to 48 times in the page. Item custom fields are not available in the addToViewContextDefinition callback context. If I have to source using a back-end service, it'll be called up to 48 times on category pages, which is likely to negatively affect user experience and possibly server performance.
s
... why not just use PDP.getItemInfo().item.[yourcustomfield] and pull the value from the model?
An alternative would be to avoid editing the template and just inject a new view, and use a contextDataRequest
In fact, I prefer the second one. Generally speaking, you shouldn't need to edit a template to add new functionality
d
Thanks Steve. I'm actually modifying the add to cart button and whether it displays or not based on our own custom fields. I've now managed to get it working moving the button HTML into a child view and passing in the info from PDP.getItemInfo(), which works fine on product pages and quick views. Now just need to figure out the same on category pages.