1
votes

First let me explain myself. I'm new to Prestashop Development, I've a background using Wordpress. The thing is, I'm trying to hook 'blockcart.php' to my custom hook 'HOOK_SHOPPINGBAG'. I have tried a lot and after a couple of hours I have decided to just ask the question here.

First I have created the Hook inside ps_hook inside the Prestashop Database. This hook is functioning and Prestashop reads it. After that I have added my new hook to FrontController.php, the code is shown below:

public function initContent()
{
    $this->process();
    if (!isset($this->context->cart))
        $this->context->cart = new Cart();
    if (!$this->useMobileTheme())
    {
        // These hooks aren't used for the mobile theme.
        // Needed hooks are called in the tpl files.
        $this->context->smarty->assign(array(
            'HOOK_HEADER' => Hook::exec('displayHeader'),
            'HOOK_TOP' => Hook::exec('displayTop'),
            'HOOK_SHOPPINGBAG' => Hook::exec('displayShoppingBag'),
            'HOOK_LEFT_COLUMN' => ($this->display_column_left ? Hook::exec('displayLeftColumn') : ''),
            'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),
        ));
    }
    else
        $this->context->smarty->assign('HOOK_MOBILE_HEADER', Hook::exec('displayMobileHeader'));
}

I also have added the Hook to the second bunch of lines inside FrontController.php:

// Call hook before assign of css_files and js_files in order to include correctly all css and javascript files
    $this->context->smarty->assign(array(
        'HOOK_HEADER' => $hook_header,
        'HOOK_TOP' => Hook::exec('displayTop'),
        'HOOK_SHOPPINGBAG' => Hook::exec('displayShoppingBag'),
        'HOOK_LEFT_COLUMN' => ($this->display_column_left ? Hook::exec('displayLeftColumn') : ''),
        'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),
        'HOOK_FOOTER' => Hook::exec('displayFooter')
    ));

I want to show the Cart inside header.tpl so I have added the Hook to this file:

<div class="shoppingbag">
    {$HOOK_SHOPPINGBAG}
</div>

To make sure Blockcart.php can be hooked to my new hook I have added the following line to blockcart.php:

public function hookShoppingBag($params) {
    return $this->hookheader($params, 'displayShoppingBag');
}

I've already tried so many things, but when I try to Hook the module to the new Hook it keeps telling me "This modules can't transplant to this hook!"

Maybe I have just made some stupid mistakes, but anyway I hope you guys can help me. Thanks!

1
I'm not exactly sure what you're trying to hook on what here, but I think you may be missing $this->registerHook('displayShoppingBag') in your module install() method. This actually tells perstashop that when a hook is executed, call this module's hook function. Otherwise the function is just laying around and no one is calling it. - gskema

1 Answers

0
votes

I assume you need a custom hook for blockcart module to be placed somewhere.

This is how to achieve it through Presta's custom hook way.

|| $this->registerHook('shoppingBag') == false

Add this to the module's install method.

public function install()
{
    if (
        parent::install() == false
        || $this->registerHook('top') == false
        || $this->registerHook('header') == false
        || $this->registerHook('actionCartListOverride') == false
        || Configuration::updateValue('PS_BLOCK_CART_AJAX', 1) == false
        || Configuration::updateValue('PS_BLOCK_CART_XSELL_LIMIT', 12) == false)
        || $this->registerHook('shoppingBag') == false
        return false;
    return true;
}

Create a public method hookShoppingBag like this.

public function hookShoppingBag($params)
{
    return $this->hookProductActions($params);
}

To hook it to any tpl use this.

<div class="shoppingbag">
{hook h='presetWishlist'}
</div>