What am I doing wrong here? I am just simply tryin...
# suitecommerce
k
What am I doing wrong here? I am just simply trying to set an item field value on the PDP
showoutofstockmessage
to false or true or really any item value and am not having any success.
s
Firstly, if you’re going to add to the PDP view context object, you should use the method attached to PDP, not layout. That could be a problem for you. Secondly, what exactly is not working? Are you logging to the console successfully? Are they the right values? Is a property being added to the context object? Is it the wrong value? You need to be more specific about your issue.
Oh and your dependencies at the top of the page look weird. You’ve added jQuery, a custom view and underscore but don’t use any of them. You’ve also incorrectly enumerated/named them — you have two dependencies but named three, which are not aligned to the names correctly
k
I am able to view and change the correct values in the console, but they are not "taking effect" on the frontend of the site.
s
… I’m not sure that answers my question
You mean you can type this code into the console and it works?
k
It is outputting to the console correctly.
But it is not removing the "Out Of Stock Message" on the PDP when I run it locally.
I get
Copy code
Auto Build? true
shopping_ext.js:249 Buildable Quantity 1
In the console correctly.
s
OK and this code:
return iteminfo[0].isinstock = true
Did you mean to try and set the
isinstock
value or did you miss a second equals sign (ie for comparison)?
k
I want to set the isinstock value to
true
this
return iteminfo[0].isinstock == true;
also doesnt work
s
OK, that won’t work in this function
k
Ok
s
By using getSelectedMatrixChilds() you are just pulling variables out — using a getter
The parent addToViewContextDefinition is about setting a value on the context object of the view, name and type you provide
The function you are using is the function that derives that value
So the whole point of this function should be setting the autoBuild property
And nothing else
k
Ok, that makes sense, so how would I set the value correctly then?
Or rather, 3 or 4 item values on the PDP
After getting these values in this function.
s
Well it’s a little tricky because you need to first tell the context object how to return the correct value, so that means sorting out this function first. Then, depending on how you implement displaying it on the view, you may need to tell the layout view to ‘refresh’ it
The PDP.afterOptionSelect event could be useful for that second part
But you should figure out the first part first obviously, eg:
Copy code
PDP.addToViewContextDefinition('<http://Product.Stock.Info|Product.Stock.Info>', 'autobuild', 'string', function (context)
{
  var iteminfo = PDP.getSelectedMatrixChilds();
  return iteminfo[0].custitem_auto_build)
});
It’s been a while since I wrote extension code, so that might be too naive
k
Hmm not sure I follow
I cleaned up the overall file a bit.
Copy code
define('FWC.OutOfStock.OutOfStock', [], function () {
		'use strict';
		return {
			mountToApp: function mountToApp(container) {
				var Layout = container.getComponent('Layout');
				var PDP = container.getComponent('PDP');

				if (PDP) {
					PDP.addToViewContextDefinition('<http://Product.Stock.Info|Product.Stock.Info>', 'autobuild', 'string', function (context) {
							var ctx, iteminfo;

							if (PDP) {
								iteminfo = PDP.getSelectedMatrixChilds();
								console.log("Item Info: context", iteminfo[0]);
								var autoBuild = iteminfo[0].custitem_auto_build;
								console.log("Auto Build?", autoBuild);
								var buildableQuantity = iteminfo[0].custitem_buildable_quantity;
								console.log("Buildable Quantity", buildableQuantity);
								return iteminfo[0].isinstock == true;
							}
							return {};
						});
				}
			}
		}
	});