Jump to content

Changing where a product in the cart links to


Recommended Posts

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
Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

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