News:

Looking for documentation? Take a look on our wiki

Main Menu

Set layout product details page programmatically

Started by Kuubs, April 22, 2025, 08:56:51 AM

Previous topic - Next topic

Kuubs

Hello,

Does anyone know how to set the layout of the product details page programmatically.. I try to make a simple A/B testing plugin to test 2 different layouts but i'm stuck how I can set the layout of the product..

Jumbo!

Where do you want to change the product details page layout? You must clearly explain your requirements to get the correct answer.

Kuubs

Quote from: Jumbo! on April 29, 2025, 05:48:01 AMWhere do you want to change the product details page layout? You must clearly explain your requirements to get the correct answer.

Like I said I want to use a plugin to change the product details page layout to A/B test my product details. It's not really relevant where of when I want to change it, I just want to change it. Probably beforeRender or something

Jumbo!

Quote from: Kuubs on May 16, 2025, 08:32:40 AMIt's not really relevant where of when I want to change it, I just want to change it. Probably beforeRender or something

It is crucial because once the layout is initialised, you cannot change it, even in the onBeforeRender event. You can try it in the onAfterRoute event. But, ensure that you do not override the default layout in the menu item.

Example:
    public function onBeforeRoute()
    {
        // Register VirtueMart config to Joomla autoloader
        \JLoader::register('VmConfig', JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');

        // If VirtueMart is not installed, return
        if (!class_exists('VmConfig')) {
            return;
        }

        // Load VirtueMart config
        \VmConfig::loadConfig(false, false, true, false);

        // Set the default product layout
        \VmConfig::set('productlayout', 'default');
    }

Milbo

Very great Jumbo.

Yes, that is one way. Jumbo shows here, how you can load the config and change the vm config param for the productdetails layout. It works as long you do the changes BEFORE the view.html.php is called. So you can also use it in a normal vm plugin of the family vmextended, or vmuserfields, they are triggered directly after the loadConfig.

Btw the second param should be also false, because the language is not know yet. It works, because it uses the prior language, but it wont reflect a language switch.

But you can also change the layout if you have the view object. If you are in a layout, the view object is just "$this", for example $this->setLayout('notify')

But we use in the view.html.php of productdetails this function
VmTemplate::setVmTemplate($this, $this->category->category_template, 0, $this->category->category_product_layout, $layout);

so maybe the last line is your real answer, because it should also reflect the overrides bs- stuff and so on.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/