Adding variables to SC Shopping Cart template in Service Portal?

YenGar
Mega Sage

Hi community, 

I'm working on the Service Portal for my organization and they would like to see some variable fields in in the SC Shopping Cart widget. The widget is using the large_shopping_cart template and by default, it shows the product, price, quantity and subtotal. We would like to have the Requested For (variable in the form) value be added to it as well so it would look like the below to reflect who the item is requested for

 

find_real_file.png

 

I am not sure how to get the variable field to be displayed in the layout. Would like to know if any of you has done something similar and would appreciate any advice/suggestions!!

This is the template that I am using - I've already made some modifications to it but would like to know how i can get variables in it:

 

<div>
  <div ng-show="c.data.cartItems.length > 0" class="panel panel-primary b">
    <table id="cart" class="table table-hover table-condensed">
      <thead>
        <tr>
          <th style="width: 52%;">${Product}</th>
          <th style="width: 10%;">${Requested for}</th>
          <th style="width: 10%;">${Price}</th>
          <th style="width: 8%;">${Quantity}</th>
          <th style="width: 10%;">${Subtotal}</th>
          <th style="width: 10%;"></th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="item in c.data.cartItems track by item.sys_id | orderBy: 'order'">
          <td data-th="Product">
            <div class="row">
              <div class="col-sm-2 hidden-xs"><img ng-show="item.picture" ng-src="{{item.picture}}" alt="..." class="img-responsive item-image"/></div>
              <div class="col-sm-10">
                <h4 class="nomargin"><a ng-href="?id=sc_cat_item&sys_id={{item.item_id}}">{{item.name}}</a></h4>
                <p class="hidden-xs">{{item.short_description}}</p>
              </div>
            </div>
          </td>
          <td>
            <strong>{{item.display_price}}</strong>
            <div ng-show="item.recurring_price > 0">+ {{item.display_recurring_price}} {{item.recurring_frequency_display}}</div>
          </td>
          <td data-th="Quantity">
            <input type="number"
                   ng-if="item.show_quantity"
                   class="form-control text-center"
                   ng-model="item.quantity"
                   min="1"
                   max="99"
                   ng-model-options="{ updateOn: 'blur' }"
                   ng-change="c.updateQuantity(item)">
            <span ng-if="!item.show_quantity">-</span>
          </td>
          <td>
            <strong>{{item.subtotal_price}}</strong>
            <div ng-show="item.recurring_subtotal > 0">+ {{item.recurring_subtotal_price}} {{item.recurring_frequency_display}}</div>
          </td>
          <td class="actions" data-th="">
            <button class="btn btn-info btn-sm" ng-show="item.has_options" ng-click="c.editItem(item.sys_id)"><i class="fa fa-pencil"></i></button>
            <button class="btn btn-danger btn-sm" ng-click="c.removeItem($event, item)"><i class="fa fa-trash-o"></i></button>
          </td>
        </tr>
      </tbody>
    </table>

    <div class="panel-footer clearfix">
      <div class="pull-right">
        <h4 class="text-bold">${Total price}: {{c.data.cart.display_subtotal}}</h4>
        <h5 class="text-bold" ng-repeat="(key, value) in c.data.cart.recurring_subtotals">+ {{value}} {{key}}</h5>
      </div>
    </div>
  </div>

 <!-- <div class="panel b" ng-show="c.data.cartItems.length > 0">
    <div class="order-details-container form-group">
      <div class="order-details requested-for-details">
        <div>
          <label>${Requested For}</label>
          <sn-record-picker field="c.requestedFor" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
        </div>
        <div>
          <label>${Deliver To}</label>
          <textarea class="form-control" ng-model="c.deliverTo"></textarea>
        </div>
      </div>
      <div class="order-details special-instructions-details">
        <label>${Special Instructions}</label>
        <textarea class="form-control" ng-model="c.additionalDetails"></textarea>
      </div>
    </div>-->
    <div class="panel-footer">
      <a href="?id=sc_home" name="submit" ng-click="triggerOnSubmit()" class="btn btn-default m-r-xs">${Continue Shopping}</a>
      <button ng-disabled="c.checkoutInProgress" name="submit" ng-click="c.triggerCheckout($event)" class="btn btn-primary m-l-xs pull-right">
        <span ng-show="!c.checkoutInProgress">${Checkout}</span>
        <span ng-show="c.checkoutInProgress">${Ordering...}</span>
      </button>
      <!--<a class="btn btn-default"
         ng-click="c.saveBundle()">${Save as Bundle}</a>-->
    </div>
  </div>

  <div ng-show="c.data.cartItems.length == 0" class="panel panel-default b">
    <div class="panel-body">
      <div class="empty-state-content">
        <span class="fa fa-shopping-cart"></span>
        <h3>${Your shopping cart is empty}</h3>
        <p>${Once you have added items to your shopping cart, you can check out from here.}</p>
        <a class="btn btn-primary" href="?id=sc_home">${View the Catalog}</a>
      </div>
    </div>
  </div>
</div>

Thank you, 

Yeny

25 REPLIES 25

Hi, Please check the response in one of my replies. I have added step by step on what i have done in addition and also attached the xml files if you want to download. See this response below in this thread. If you still have difficulty, then let me know.

RiteshAlla_0-1690923378904.png

 

Hi Ritesh,

I have been trying to Add variables to SC Shopping Cart template in Service Portal but the requested for variable is coming empty in the widget and not sure what code needs to be adjusted in the "large_shopping_cart_v2.html" angular ng-template.

It would be of great help if you help me to fix this issue .

I will paste the screenshot and html template code for correction.

 

 

 

<div ng-if="c.data.cartItems.length > 0">
  <div class="panel panel-primary b {{::c.data.isMEE ? 'cart-mobile' : ''}}">
    <div class="cart-header">
	    <h2 id="main-cart-header" class="main-cart-header h3 m-t-sm" ng-class="{'padder-md' : !c.data.isMEE, 'pull-left padder-l-none' : c.data.isMEE}" tabindex="-1">${Your Cart}</h2>
      <button ng-if="::c.data.isMEE" class="btn btn-default" ng-click="c.clearCart()">${Empty Cart}</button>
    </div>
    <table ng-if="::!c.data.isMEE" id="cart" class="table table-hover b-t table-condensed cart-table" aria-label="${Your Cart}" aria-describedby="id-caption-cart">
      <caption id="id-caption-cart"><span class="sr-only">${Your Cart}</span></caption>
      <thead>
        <tr>
          <th id="id-header-item" scope="col" class="col-sm-6 wrapper padder-md">${Item}</th>
          <th id="id-header-price" scope="col" class="col-sm-2 wrapper hidden-xs" ng-if="c.data.cart.cart_items.show_subtotal_price === 'true'">${Price}</th>
          <th id="id-header-quantity" scope="col" class="col-sm-1 wrapper hidden-xs">${Quantity}</th>
          <th id="id-header-subtotal" scope="col" class="col-sm-2 wrapper hidden-xs" ng-if="c.data.cart.cart_items.show_subtotal_price === 'true'">${Subtotal}</th>
          <th id="id-header-item-controls" scope="col" class="col-sm-1"><span style="visibility: hidden;">${Item Controls}</span></th>
        </tr>
      </thead>
      <tbody>
        <tr class="item-row b-b" ng-repeat="item in c.data.cartItems | orderBy: 'created_on' track by item.sys_id">
          <td headers="id-header-item" data-th="Product" class="col-sm-7 col-xs-11 wrapper-md break-word">
            <div class="row">
              <a ng-href="{{::item.item_url}}" aria-labelledby="sc_cat_item_{{::item.sys_id}}" aria-describedby="sc_cat_item_short_desc_{{::item.sys_id}}" ng-class="::{'a-disabled': !item.can_order}" class="block">
                <div>
              		<div class="sn-flexbox">
                    		<div class="col-sm-3 col-xs-4">
                      			<img ng-if="item.picture" ng-src="{{::item.picture}}" class="img-responsive item-image" alt=""/>
                    		</div>
                    		<div class="col-sm-9 col-xs-8">
                    			<div class="nomargin h4 catalog-text-wrap"><div ng-href="{{::item.item_url}}" ng-class="::{'a-disabled': !item.can_order}" id="sc_cat_item_{{::item.sys_id}}">{{::item.name}}</div></div>
                			<p class="catalog-text-wrap sc-cart-text" id="sc_cat_item_short_desc_{{::item.sys_id}}">{{::item.short_description}}</p>
                             <td>
                                  <strong>{{item.variables.requested_for}}</strong>
                             </td>
   
                      <div class="visible-xs sc-cart-text" ng-if="c.data.cart.cart_items.show_subtotal_price === 'true'">
                        <span class="sr-only" ng-if="::(c.showPrice(item))">${Subtotal Price:} {{item.subtotal_price_display}}</span>
            		        <span ng-if="::(c.showPrice(item) && c.showRecurringPrice(item))" aria-hidden="true">&nbsp;&nbsp;&nbsp;{{item.subtotal_price_display}}</span>
            		        <span ng-if="::(c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden="true">{{item.subtotal_price_display}}</span>
                        <span class="sr-only" ng-if="::c.showRecurringPrice(item)">${Recurring subtotal price} {{item.recurring_subtotal_price_display + ' ' + item.recurring_frequency}}</span>
                        <div ng-if="::c.showRecurringPrice(item)" aria-hidden="true">{{::c.showPrice(item) ? '+ ' : ' '}} {{item.recurring_subtotal_price_display + ' ' + item.recurring_frequency}}</div>
                        <span class="sr-only" ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))">${blank}</span>
                        <div ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden=true>
                          ---
                        </div>
                      </div>
                    		</div>
              		</div>
                </div>
             </a>
            </div>

                <div class="col-xs-6 visible-xs">
                  <select ng-if="c.showQuantity(item) && !item.read_only_quantity"
                    id="{{item.sys_id + 'xs'}}"
                    title="{{c.getQuantityTitle(item)}}"
                    class="m-t scBasicSelect"
                    ng-disabled="(item.order_guide && !item.show_quantity_guide_item)  || !item.can_order"
                    ng-model="item.quantity"
                    ng-change="c.updateQuantity(item)">
                     <option ng-repeat="num in data.choiceListQuantity" value={{::num.value}}>{{::num.label}}</option>
                  </select>
                </div>
          </td>
          <td headers="id-header-price" class="break-word hidden-xs wrapper" ng-if="c.data.cart.cart_items.show_subtotal_price === 'true'">
            <span ng-if="::(c.showPrice(item) && c.showRecurringPrice(item))">&nbsp;&nbsp;&nbsp;{{item.price_display}}</span>
            <span ng-if="::(c.showPrice(item) && !c.showRecurringPrice(item))">{{item.price_display}}</span>
            <div ng-if="::c.showRecurringPrice(item)">
            	<div aria-hidden="true">{{::c.showPrice(item) ? '+ ' : ' '}} {{item.recurring_price_display + ' ' + item.recurring_frequency}}</div>
              <span class="sr-only">${Recurring Price} {{item.recurring_price_display}} {{item.recurring_frequency}}</span>
            </div>
            <span class="sr-only" ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))">${blank}</span>
            <div ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden=true>
               ---
            </div>
          </td>
          <td headers="id-header-quantity" data-th="Quantity" class="break-word hidden-xs wrapper">
            <select ng-if="c.showQuantity(item) && !item.read_only_quantity"
                    id="{{item.sys_id}}"
                    title="{{c.getQuantityTitle(item)}}"
                    class="scBasicSelect"
                    ng-disabled="(item.order_guide && !item.show_quantity_guide_item) || !item.can_order"
                    ng-model="item.quantity"
                    ng-change="c.updateQuantity(item)">
                     <option ng-repeat="num in data.choiceListQuantity" value={{::num.value}}>{{::num.label}}</option>
                  </select>
            <span ng-if="::(!item.show_quantity || (item.read_only_quantity && item.sys_class_name != 'sc_cat_item_producer'))">{{::item.show_quantity ? item.quantity : '--'}}</span>
          </td>
          <td headers="id-header-subtotal" class="break-word hidden-xs wrapper" ng-if="c.data.cart.cart_items.show_subtotal_price === 'true'">
            <span ng-if="::(c.showPrice(item) && c.showRecurringPrice(item))" aria-hidden="true">&nbsp;&nbsp;&nbsp;{{item.subtotal_price_display}}</span>
            <span ng-if="::(c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden="true">{{item.subtotal_price_display}}</span>
            <span ng-if="::(c.showPrice(item))" class="sr-only">${Price} {{item.subtotal_price_display}}</span>
            <div ng-if="::c.showRecurringPrice(item)">
            	<div aria-hidden="true">{{::c.showPrice(item) ? '+ ' : ' '}} {{item.recurring_subtotal_price_display + ' ' + item.recurring_frequency}}</div>
              <span class="sr-only">${Recurring Price} {{item.recurring_subtotal_price_display}} {{item.recurring_frequency}}</span>
            </div>
            <span class="sr-only" ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))">${blank}</span>
            <div ng-if="::(!c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden=true>
               ---
             </div>
          </td>
          <td headers="id-header-item-controls" class="actions break-word col-xs-1" data-th="">
              <button class="btn btn-clear" ng-click="c.removeItem($event, item)" data-toggle="tooltip" data-placement="top"  data-container="body" title="${Remove Item} {{::item.name}}">
              <i class="fa fa-times-circle btn-remove" aria-hidden="true"></i>
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <div ng-if="::c.data.isMEE" id="cart">
      <div ng-repeat="item in c.data.cartItems | orderBy: 'created_on' track by item.sys_id" class="cart-item-mobile-info b-b">
        <div class="col-xs-3 no-padder">
            <a ng-href="{{::item.item_url}}" ng-if ="::item.picture" aria-label="{{::item.name}}">
            <img ng-show="item.picture" ng-src="{{::item.picture}}" class="img-responsive item-image" role="img" alt="{{::item.name}}"/>
          </a>
        </div>
        <div class="item-info col-xs-7">
          <div class="col-sm-9">
            <h2 class="nomargin h4 catalog-text-wrap"><a ng-href="{{::item.item_url}}" class="padder-v" ng-class="::{'a-disabled': !item.can_order}">{{::item.name}}</a></h2>
          </div>
          <div class="col-sm-9">
            <span ng-if="c.showQuantity(item)" role="text" aria-label="${quantity} {{::item.quantity}}">{{item.quantity}}{{c.showPrice(item) || c.showRecurringPrice(item) ? ' x ' : ' '}}</span>
            <span role="text">
              <span ng-if="::(c.data.cart.cart_items.show_subtotal_price === 'true' && c.showPrice(item))"><span class="sr-only">${Price }</span>{{item.price_display}}</span>
            	<span ng-if="::(c.data.cart.cart_items.show_subtotal_price === 'true' && c.showRecurringPrice(item))">{{::c.showPrice(item) ? '+ ' : ' '}} <span class="sr-only"> ${Recurring Price }</span>{{item.recurring_price_display + ' ' + item.recurring_frequency}}</span>
            </span>
            <div role="text">
              <span class="sr-only" ng-if="::(c.data.cart.cart_items.show_subtotal_price === 'true' && !c.showPrice(item) && !c.showRecurringPrice(item))">${Price blank}</span>
              <div ng-if="::(c.data.cart.cart_items.show_subtotal_price === 'true' && !c.showPrice(item) && !c.showRecurringPrice(item))" aria-hidden=true>
              ---
            	</div>
            </div>
            
          </div>
        </div>
        <button class="btn btn-clear" ng-click="c.removeItem($event,item)" aria-label="${Remove Item From Cart} {{::item.name}}" title="${Remove Item} {{::item.name}}">
          <i class="fa fa-times-circle btn-remove" aria-hidden="true"></i>
        </button>
      </div>
    </div>
    <div class="clearfix" ng-class="{'wrapper': !c.data.isMEE}">
      
      <div class="m-b-lg m-t-lg" ng-if="::c.data.isMEE && c.data.cart.cart_items.show_subtotal_price === 'true'" role="text">
        <div class="pull-left"><strong>${Total price}:</strong></div>
        <div class="m-r-sm">
          <span>&nbsp;{{c.data.cart.cart_items.subtotal_price}}</span>
          <div ng-repeat="(key, value) in c.data.cart.recurring_subtotals">+ <span class="sr-only">${recurring price}</span>{{value}} {{key}}</div>
        </div>
        
      </div>
      
      <div class="pull-right m-r-lg m-b-lg" ng-if="::!c.data.isMEE && c.data.cart.cart_items.show_subtotal_price === 'true'">
        <div class="pull-left"><strong>${Total price}: </strong></div>
        <div class="pull-left m-l-sm">
          <span>&nbsp;&nbsp;&nbsp;{{c.data.cart.cart_items.subtotal_price}}</span>
          <div ng-repeat="(key, value) in c.data.cart.recurring_subtotals">+ {{value}} {{key}}</div>
        </div>
      </div>
           <div>
          <label>${Requested For}</label>
          <sn-record-picker field="c.requestedFor" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
        </div>
      
      <div class="sc-btn-group">
        <a ng-if="::!c.data.isMEE" href="?id=sc_home" name="submit" class="btn sc-btn btn-default m-r-xs">${Continue Shopping}</a>
        <button ng-if="::!c.data.isMEE" class="btn sc-btn btn-default" ng-click="c.saveBundle()">${Save as Bundle}</button>
        <div ng-if="::!c.data.isMEE" class="sc-btn-group pull-right">
          <button class="btn sc-btn btn-default m-r-xs sc-remove-btn" ng-click="c.clearCart()" >${Clear Cart}</button>
          <button ng-disabled="c.checkoutInProgress" name="submit" ng-click="c.triggerCheckout()" class="btn sc-btn btn-primary" ng-class="{'rounded m-r-lg padder-xl': c.data.isMEE}">
            <span ng-show="!c.checkoutInProgress && c.data.sys_properties.twostep">${Proceed to Checkout}</span>
            <span ng-show="!c.checkoutInProgress && !c.data.sys_properties.twostep">${Checkout}</span>
            <span ng-show="c.checkoutInProgress">${Ordering...}</span>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>
<div ng-show="c.data.cartItems.length == 0" class="panel panel-default b">
	<div class="panel-body">
		<div class="empty-state-content" ng-show="!c.data.checkedOut">
			<span class="fa fa-shopping-cart" aria-hidden="true"></span>
			<h2 class="h3" id="empty-cart-heading" tabindex="-1">${Your shopping cart is empty}</h2>
			<p>${Once you have added items to your shopping cart, you can check out from here.}</p>
			<a class="btn btn-primary" href="?id=sc_home" ng-if="!c.data.isMEE">${View the Catalog}</a>
    	</div>
    	<div class="text-a-c" ng-show="c.data.checkedOut">
      		<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
      		<span class="sr-only">${Loading...}</span>
		</div>
  </div>
</div>

 

 

@Arne Soerensen 

Hi Arne,

Even i am trying to achieve the same requirement but unsuccessful.

I have written the script in the SPCart script includes and tried making changes in "large_shopping_cart_v2.html" ng-template but still no luck.

I guess somewhere i am making a mistake in the "large_shopping_cart_v2.html " ng-template which is hard to figure out .

Can you post me the script you have written in the ng-template that helped you accomplish this requirement.

Below is the script i wrote in the SPCart script includes :

var scItemOptionGR = new GlideRecord("sc_item_option");
scItemOptionGR.addQuery("cart_item","20756e989357711015a5718efaba1075");
scItemOptionGR.query();
var variables = {};
while(scItemOptionGR.next())
 {
var v = scItemOptionGR.getDisplayValue('value');
variables[scItemOptionGR.item_option_new.name] = (v == null || v =='' ? '' : v);

}	
var cartItem = GlideappCartItem.get(cartItemID);
    return {
		variables:variables.requested_for,
}

 Thanks in advance

Hello @diNesh_M 

 

Here is what our script looks like to display the requested_for variable among others.  I have also attached a screenshot of the cart with an item to better illustrate the connection between the script and actual cart.

 

I hope this helps.

 

Thanks

Arne SoerensenDC Install Shopping Cart.png

scItemOptionGR = new GlideRecord("sc_item_option");
	scItemOptionGR.addQuery("cart_item", cartItemID);
	scItemOptionGR.query();
	var variables = {};
	
	while (scItemOptionGR.next()){
		var v = scItemOptionGR.getDisplayValue('value');
		variables[scItemOptionGR.item_option_new.name] = (v == null || v =='' ? '' : v);
	
		var reqfor = '';
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', variables.requested_for);
		gr.query();
		if (gr.next()) {
			reqfor = gr.first_name.toString() + ' ' + gr.last_name.toString();
		}
		
		var prisupport = '';
		var grp = new GlideRecord('sys_user');
		grp.addQuery('sys_id', variables.primary_support_person);
		grp.query();
		if (grp.next()) {
			prisupport = grp.first_name.toString() + ' ' + grp.last_name.toString();
		}
	}

    var cartItem = GlideappCartItem.get(cartItemID);
    return {
		variables: variables,
		requestedfor: reqfor,
		primarysupport: prisupport,

@Arne Soerensen 

Hi Arne Soerensen,

Thank you so much for your quick response .

I have written the same script which you shared in the "SPCart" script include but i dont know  what changes i have to make in the Angular ng-template "large_shopping_cart_v2.html" .Could you pls put the screenshot of the script that was used  in  Angular ng-template "large_shopping_cart_v2.html" to achieve  your requirement.

And apart from reference field ,hope it is possible to cascade other variables  like Dropdown and Single line text right and if so can u guide me on that as well.

I will put the screenshot of the Angular ng-template for "large_shopping_cart_v2.html" which is not working  ,kindly correct me what went wrong .

 

Thanks in advance