I'm trying to add a custom value for the addtocart function in Hybris Commerce b2c. Is a boolean in with a checkbox.
I've add the bol custom value in core-items.xml but now I need to get that value when the checkbox is selected before press the addToCart Button and then show that value in the checkout page.
I thing it's not a short task, but I just want to understand the data flow for this kind of process, that's the complex part.
If someone have time to explain something, it will be grate! Thanks !
<form:form method="post" id="addToCartForm" class="add_to_cart_form" action="${addToCartUrl}">
<div>
<div class="custom-control custom-checkbox">
<input path="trasiego" onclick="ACC.productDetail.enableMinusBtn()" type="checkbox" class="custom-control-input" id="trasiego">
<form:checkbox path="trasiego" value="true"/>
<spring:theme code="product.pdp.pedidoTrasiego"/>
</div>
</div>
<input type="hidden" maxlength="3" size="1" id="qty" name="qty" class="qty js-qty-selector-input" value="1">
<input class = "js-simulate-validate-link" type="hidden" name="productCodePost" value="${fn:escapeXml(product.code)}"/>
<ycommerce:testId code="simulateButton">
<button id="simulateButton" type="button" data-error-simulate-url="${fn:escapeXml(simulatePopup)}" data-simulate-title="<spring:theme code="text.simulate"/>" class="btn btn-default btn-block js-add-to-cart" disabled=disabled">
<spring:theme code="basket.simulate"/>
</button>
</ycommerce:testId>
<ycommerce:testId code="addToCartButton">
<button id="addToCartButton" type="submit" class="btn btn-primary btn-block" disabled=disabled>
<spring:theme code="basket.buy"/>
</button>
<br>
</ycommerce:testId>
</form:form>
A couple of points below if you find it helpful,
I've add the bol custom value in core-items.xml......
A little confused when I read above statement and hence wanted to put that it is against the standard practices to touch OOB extensions code base. Instead, you should be generating custom extension and extend/update Item in your project $extension-items.xml
I need to get that value when the checkbox is selected before press the addToCart Button and then show that value in the checkout page.
You can achieve it with following steps :
Step 1 - In your project extension $extension-items.xml, add a new boolean attribute to AbstractOrder
<itemtype code="AbstractOrder" autocreate="false" generate="false" >
<attributes>
<attribute qualifier="code" type="java.lang.Boolean">
<persistence type="property" />
</attribute>
</attribute>
</itemtype>
Step 2 - Run ant clean all
Step 3 - In you project storefront extension, add a checkbox in the Jsp/Tag file
Step 4 - If you want to persist the selection of this value on select/deselect of this checkbox, you can write an onClick() event in corresponding JS file. In your case you can use /$storefornt-extension/web/webroot/WEB-INF/_ui-src/responsive/lib/ybase-0.1.0/js/acc.cart.js
Step 5 - Create corresponding handler in Controller (MVC). You may use AddToCartController.java or CartPageController as per your need
Step 6 - In corresponding facade --> service, save new field value to model
Step 7 - In AbstractOrderPopulator or CartPopulator, map your new field to fill it back into CartData from item model. And same can be used on next page for view.
With this your field value will be persisted & fetched to transfer between various hybris layers and you can use corresponding data object.
Hope it helps.