Overriding Javascript in Non-Core Module

Hello all!

Recently, I’ve been trying to modify the behaviour of the “All Products” page in the “Webshop” plugin. It displays a list of all the products, and has the capacity to accept filters - i.e. only displaying Website Items where “field” equals “value”. These are client-defined filters - they appear as a set of fields on the left of the page for the user to select as they will.

My intent is to add a filter that is always filled out - I have added a custom field “custom_user” to the “Website Item” Doctype, to hopefully only display those items that match the currently logged in user.

The function that defines the set of filters is in “webshop > public > js > product_ui > views.js”:

webshop.ProductView =  class {
	/* Options:
		- View Type
		- Products Section Wrapper,
		- Item Group: If its an Item Group page
	*/
	
	// Monkey Patching: get_query_filters
	
	constructor(options) {
		Object.assign(this, options);
		this.preference = this.view_type;
		this.make();
	}

	...

	get_query_filters() {
		const filters = frappe.utils.get_query_params();
		let {field_filters, attribute_filters} = filters;

		field_filters = field_filters ? JSON.parse(field_filters) : {};
		attribute_filters = attribute_filters ? JSON.parse(attribute_filters) : {};

		return {
			field_filters: field_filters,
			attribute_filters: attribute_filters,
			item_group: this.item_group,
			start: filters.start || null,
			from_filters: this.from_filters || false
		};
	}

	...

};

where “field_filters” in “get_query_filters” is a dictionary of each field and allowable values: {“name”:[“opt1”, “opt2”]}.

I intend to use “hooks.py” in my own custom module to override this function into:

get_query_filters() {
	const filters = frappe.utils.get_query_params();
	let {field_filters, attribute_filters} = filters;

	field_filters = field_filters ? JSON.parse(field_filters) : {};
	attribute_filters = attribute_filters ? JSON.parse(attribute_filters) : {};
		
	// edit start
	field_filters.custom_user = [frappe.session.user];
	// edit end

	return {
		field_filters: field_filters,
		attribute_filters: attribute_filters,
		item_group: this.item_group,
		start: filters.start || null,
		from_filters: this.from_filters || false
	};
}

This code adds a new filter value, making sure the “custom_user” field matches the current logged in user.

I am having difficulty adding an entry into “hooks.py”, however - it seems that “hooks.py” is made primarily for overriding the core modules.

How would I go about overriding this function? I’d prefer not to use monkey patching if it can be helped, but if that’s the only way then guidance on that would be appreciated.

Thanks in advance!