Hiding Products Programmatically in Magento 2 Checkout with One Step Checkout Module
As a senior developer working extensively with Magento 2 e-commerce platforms, I often encounter clients who need to hide specific products from the checkout process. This is particularly challenging when using third-party modules like the One Step Checkout module by Magestore.
Understanding the Issue: Hiding Products in Checkout and Cart
The client in this scenario wants to programmatically hide certain pre-added products (like packing materials) from appearing in both the cart and the one-step checkout process. CSS styling isn’t an effective solution for this requirement, as it doesn’t remove the products entirely.
Navigating the One Step Checkout Module’s KnockoutJS Implementation
The One Step Checkout module uses KnockoutJS to manage the product display on the frontend. The cart-items.js file controls how items are rendered in the checkout and cart:
define(
[
'jquery',
'ko',
//... other dependencies
],
function ($, ko, totals, Component, stepNavigator, quote) {
'use strict';
return Component.extend({
initialize: function () {
this._super();
var self = this;
totals.isLoading.subscribe(function () {
if (totals.isLoading() == true) {
self.showOverlay();
} else {
self.hideOverlay();
}
});
},
//... other code
});
}
);
Hiding Products Programmatically
To achieve the desired outcome, you’ll need to modify this JavaScript file. The getItems function returns an array of quote items currently in the cart:
getItems: totals.getItems(),
We can filter out the specific products here using the product’s SKU or ID.
Implementing the Filter
You need to add a new computed observable to filter out the unwanted items. Update your code as follows:
define(
[
'jquery',
'ko',
//... other dependencies
],
function ($, ko, totals, Component, stepNavigator, quote) {
'use strict';
return Component.extend({
initialize: function () {
this._super();
var self = this;
totals.isLoading.subscribe(function () {
if (totals.isLoading() == true) {
self.showOverlay();
} else {
self.hideOverlay();
}
});
},
defaults: {
template: 'Magestore_OneStepCheckout/summary/cart-items'
},
totals: totals.totals(),
getItems: function () { // Filter out the unwanted items
return this._.filter(totals.getItems(), function(item) {
var unwantedSkus = ['XXXXXXX', 'YYYYYYY']; // Add your SKUs here
if (unwantedSkus.indexOf(item.sku) > -1) {
return false;
}
return true;
});
},
getItemsQty: function() { // Recalculate the total items quantity after filtering
return parseFloat(this.totals.items_qty);
},
//... other code
});
}
);
In this example, we assume you want to hide products with SKUs ‘XXXXXXX’ and ‘YYYYYYY’. You can replace these placeholders with your actual product SKUs.
Remember to replace the placeholders with the actual values of the unwanted products’ SKUs or IDs. If you have many items to exclude, consider using a dedicated configuration file to manage this list.
Updating the Template
After updating the JavaScript file, ensure the template (cart-items.html) is adjusted accordingly:
<div class="block-content">
<div data-bind="foreach: getItems()">
<!-- Your existing HTML code for each item -->
</div>
</div>
Here’s a quick tutorial on updating KnockoutJS bindings in Magento 2 if you need more guidance.
Conclusion
Hiding specific products programmatically is a complex task when using third-party modules like the One Step Checkout module. By understanding the KnockoutJS implementation and applying a filter to the getItems function, we can successfully exclude unwanted items from the checkout process.
Remember to test your changes thoroughly in a development environment before deploying them to your live site.
If you’re facing any further challenges with Magento 2 customizations or need expert guidance on e-commerce solutions, feel free to contact us for assistance.