Jump to content
DennisMidjord

Changing where a product in the cart links to

Recommended Posts

Posted (edited)

Hi,

We're trying to change where products in the sidebar in the cart-menu links to. I've tried this so far without luck:

<?php
use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) {
	$secondarySidebar->getChild('Categories')->getChild('Webhosting')->setUri('webhosting.php');
});

Any idea if it's possible at all?

I can remove the category-section just fine using 'removeChild('Categories')'.

Edit: I obviously made a typo. That has been fixed now - but now I see an error on every other page like " Error: Call to a member function getChild() on null in "

Edit2: I might have fixed it by doing this:

add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) {
	if (!is_null($secondarySidebar->getChild('Categories'))) {
        $secondarySidebar->getChild('Categories')->getChild('Webhosting')->setUri('webhosting.php');
    }
});

Is that the correct way to do it?

Edited by DennisMidjord

Share this post


Link to post
Share on other sites

Hi Dennis,

2 hours ago, DennisMidjord said:

Edit: I obviously made a typo. That has been fixed now - but now I see an error on every other page like " Error: Call to a member function getChild() on null in "

the golden rule when modifying a sidebar/navbar child is to first check that it exists, and only if it does should you attempt to modify it... so most of the cart pages, that hook will work because what you're trying to modify exists... go to a page outside the cart, and you're then trying to modify something that doesn't exist and WHMCS will throw an error.

2 hours ago, DennisMidjord said:

Edit2: I might have fixed it by doing this - Is that the correct way to do it?

it's closer and should work - unless you were to do something daft to break it like renaming that product group, or running another hook that ran before this which removed that child you're modifying...

the cleanest way might be a variation of the hook I posted in the thread below...

<?php

# Cart Redirect Category URLs Hook
# Written by brian!

use WHMCS\View\Menu\Item as MenuItem;
add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar)
{
	$CategoriesSidebar = $secondarySidebar->getChild("Categories");
	if (empty($CategoriesSidebar)) {
		return;
	}
	$CategoriesSidebarChildren = $CategoriesSidebar->getChildren();
	$ChildrenToRemove = array('Webhosting'=>'webhosting.php');
	foreach($CategoriesSidebarChildren as $CategoryKey => $CategoryChild) {
		foreach($ChildrenToRemove as $label => $child) {
			if ($CategoryKey == $label) {
				$CategoriesSidebar->getChild($CategoryKey)
					->setUri($child);
			}
		}
	}
});

it's a little long-winded for just redirecting one group, but it was originally written to assign icons to multiple product groups, and so can be expanded to redirect multiple groups, just by expanding the $ChildrenToRemove array.

the trick with this hook is that you're looping through the array of children in that sidebar, so that immediately removes the need to check whether they exist - we already know that they do... renaming product groups in WHMCS settings shouldn't break this hook, it would simply cause the group to be linked as normal in the cart and not redirected.

Share this post


Link to post
Share on other sites

Thanks for the great answer, @brian! 🙂

I'm gonna stick with the "simple" solution for now (as it seems to work just fine, and I don't plan to mess with the name of the 'Categories' item :P), but I'll definitely save your post for later!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated