X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   extending / overriding product search (https://forum.x-cart.com/showthread.php?t=73301)

codegrunt 12-18-2015 01:20 PM

extending / overriding product search
 
Howdy. I am currently looking at extending / replacing the customer product search function on the front end. I believe this starts here:

classes/XLite/Controller/Customer/Search.php

Code:

protected function doActionSearch()
    {
        [...]

        \XLite\Core\Session::getInstance()->{$this->getAdvancedPanelCellName()} = array_intersect(array_keys($productsSearch), array_values($advancedParams));
        \XLite\Core\Session::getInstance()->{$sessionCell} = $productsSearch;

        $this->setReturnURL($this->buildURL('search', '', array('mode' => 'search')));
    }


with the bulk of the control logic actually happening in the view:

classes/XLite/View/ItemsList/Product/Customer\Search.php

Code:

protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false)
    {
        return \XLite\Core\Database::getRepo('\XLite\Model\Product')->search(
            $this->prepareCnd($cnd),
            $countOnly
        );
    }


The template that gets loaded appears to be:

skins/default/en/product/search/body.tpl

Which has a direct call to the class that does the actual search:

Code:

<widget class="\XLite\View\ItemsList\Product\Customer\Search" />

Based on that, is the only way to change the core search functionality to override the above template? I may be doing it incorrectly but so far I have not been able to get any obvious behavioural changes by extending "classes/XLite/View/ItemsList/Product/Customer/Search.php" in my testing module.

codegrunt 12-21-2015 12:54 PM

Re: extending / overriding product search
 
Hoping a bump spikes someone's interest. . .

=)

I am feeling pretty stumped here as I can't seem to sort out how to get my module's search view to load.

If I want to override this view:

Code:

classes/XLite/View/ItemsList/Product/Customer/Search.php

and have a dev called "MyDev" and a module called "MyModule", where should the overridden class go?

I have tried placing it here:

Code:

classes/XLite/Module/MyDev/MyModule/View/ItemsList/Product/Customer/Search.php

But it does not get loaded.

Any help appreciated. I've looked through the docs but did not see any examples specific to search results that seemed to apply.

Do you have to create a "A[object name].php" stub at every branch of the tree for lower level files to be loaded?

codegrunt 12-22-2015 10:12 AM

Re: extending / overriding product search
 
In case this helps anyone else, if you want to see what files are actually being loaded during execution (so as to find what file you need to replicate or change), you can do this by temporarily adding some code to the autoloader class (starts at line 101):

includes/autoloader.php

Code:

public static function __lc_autoload($class)
{
    $class = ltrim($class, '\\');
    list($prefix) = explode('\\', $class, 2);

    // Workaround for Doctrine 2 proxies
    if (
        ($prefix === LC_NAMESPACE || $prefix === LC_NAMESPACE . 'Abstract')
        && false === strpos($class, \Doctrine\Common\Persistence\Proxy::MARKER)
    ) {
        // start temp logging of file loading
        if($fp=fopen(__DIR__.'/../debug.log','a'))
        {
            fputs($fp,'['.date('YmdHis').']'.static::$lcAutoloadDir . str_replace('\\', LC_DS, $class) . '.php'."\n");
            fclose($fp);
        }
        // end logging
        include_once (static::$lcAutoloadDir . str_replace('\\', LC_DS, $class) . '.php');
    }
}


qualiteam 12-23-2015 01:47 AM

Re: extending / overriding product search
 
It depends on the desired changes.

\XLite\Controller\Customer\Search is the class that implements the logic to change search filters and pass the data to the widget class.

\XLite\View\ItemsList\Product\Customer\Search is the widget class that contains/calls all the logic to retrieve the list of products to be shown on the page.

Quote:

Originally Posted by codegrunt
is the only way to change the core search functionality to override the above template?


No, most likely you don't have to edit the template.

Quote:

Originally Posted by codegrunt
I may be doing it incorrectly but so far I have not been able to get any obvious behavioural changes by extending "classes/XLite/View/ItemsList/Product/Customer/Search.php" in my testing module.


How did you extend it? Did you use the "implements \XLite\Base\IDecorator" construction?

codegrunt 12-23-2015 10:39 AM

Re: extending / overriding product search
 
Thanks for the response Alex.

I did use the Decorator interface but I initially was extending the abstract class "ACustomer.php" instead of "Search.php". If I extend the Search.php view my class seems to take precedence. However, if I extend the abstract class ACustomer.php view, my class does not appear to take precedence.

Code:

namespace XLite\Module\MyDev\MyModule\View\ItemsList\Product\Customer;

class Search extends \XLite\View\ItemsList\Product\Customer\ACustomer implements \XLite\Base\IDecorator
{
    protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false)
    {
        return false;
    }
}


What I expect from the above is that all searches will return no results due to me overriding the getData() call. The behaviour is that it has no effect (searches work normally). If I change the extends directive to instead use Search.php, it does what I expect and returns no data.

Why does the ACustomer version get ignored?


All times are GMT -8. The time now is 12:41 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.