Control modules on VirtueMart pages

E-mail
(8 votes, average 4.75 out of 5)

Use these recipes to display different modules on different VirtueMart pages.

  • display a module only on the “Front Page” of VirtueMart
  • display a certain module when a certain product category is being listed
  • display a module only on the 1st page of a “shop.browse” list
  • display a module only to members in a particular VM shopper group
  • display a certain module when a certain product is being displayed in detail
  • show certain modules on various other VirtueMart pages, e.g. shopping cart, signup, any of the checkout pages, profile pages, search pages, etc etc.

Display module only on the “Front Page” of VirtueMart:

Using this simple recipe you can restrict modules to just the front page of VirtueMart. The “Front Page” is the one linked to from a menu item.

Note that if you have several menu items pointing to several VirtueMart pages, this starts to get very confusing. Ask about it on the MetaMod Forums if you need a solution for that!

if ($option == "com_virtuemart" and JRequest::getVar("page") == null) return 101;
/* (replace 101 with the module number you want
 * to display only on the front page of VirtueMart)
 */ 

Need to reverse that, and remove the modules from the Front Page of VirtueMart, and show them on any other VirtueMart pages? Try this one instead:

if ($option == "com_virtuemart" and JRequest::getVar("page") != null) return 101;
/* (replace 101 with the module number you want
 * to display on all VirtueMart pages except the Front Page)
 */

Simple recipe to display a certain module only on browse pages, with a different module for each category:

if ($option == "com_virtuemart") {
 $category_id = JRequest::getVar("category_id");
 $category = JRequest::getVar("category", null);
 $page = JRequest::getVar("page");
 if ($page == "shop.browse") {
 
  /* when viewing ALL categories */
  if ($category === '') return 100; /* module 100 for "ALL" categories */
 
  /* when browsing a specific category on browse page (e.g. category 2) */
  if ($category_id == "2") return 101; /* module 101 for category 2 */
  if ($category_id == "3") return 102; /* module 102 for category 3 */
  if ($category_id == "4") return 103; /* module 103 for category 4 */
 
  /* when browsing any other category on browse page */
  return 98;
 }
}

Show a module only on the first (paginated) page of a “shop.browse” list:

if ($option == 'com_virtuemart' and JRequest::getVar('page') == 'shop.browse' ) {
  global $vm_mainframe;
  $category_id = vmRequest::getInt('category_id');
  $limitstart = $vm_mainframe->getUserStateFromRequest(
    "view{$category_id}limitstart",
    'limitstart',
    0 );
  /* replace 101 with the module you want to show on
     1st page of browse results. If you want to change this
     to only show the module on pages 2, 3, 4 ... etc then
     also change the "==" to "!="
   */
  if ($limitstart == 0) return 101; 
}

Show modules only to shoppers in a particular VM shopper group:

if ($user->usertype == "Registered") {
 $id = (int)$user->id;
 $query = "SELECT  shopper_group_id FROM #__vm_shopper_vendor_xref " .
   " WHERE user_id = '$id'";
 $db->setQuery( $query, 0, 1 );
 $row = $db->loadObject();
 $groupid = $row->shopper_group_id;
 if ($groupid == 6) return 101; /* for VM shopper group 6, use module 101 */
 else return 102; /* for all other groups use module 102 */
}

Display a module on the detail page for certain products:

if ($option == "com_virtuemart") {
 $page = JRequest::getVar("page");
 $product_id = JRequest::getVar("product_id"); 
 
 /* PRODUCT DETAILS page */
 if ($page == "shop.product_details") {
  /* modules that should appear for specific products */
  if ($product_id == "4") return 99;
  if ($product_id == "44") return 100;
 }
}

The all-in-one version that can detect just about any page within VirtueMart:

Don’t forget to customise the “return” numbers to return the module numbers that you actually want to appear.

if ($option == "com_virtuemart") {
 $page = JRequest::getVar("page");
 $category_id = JRequest::getVar("category_id"); 
 $product_id = JRequest::getVar("product_id"); 
 $search_category = JRequest::getVar("search_category"); 
 if ($search_category != "") { 
  if ($search_category == 1) return 95; /* searched for a specific category */
  return 96; /*searched on any other category */
 }
 
 /* BROWSE page */
 if ($page == "shop.browse") {
  /* when browsing a specific category on browse page (e.g. category 2) */
  if ($category_id == "2") return 97;
 
  /* when browsing any other category on browse page */
  return 98; 
 }
 
 /* PRODUCT DETAILS page */
 if ($page == "shop.product_details") {
  /* modules that should appear for specific products */
  if ($product_id == "4") return 99;
  if ($product_id == "44") return 100;
 
  /* when viewing a product in a specific category (2 in this case).
   * Doesn't always show up unfortunately: */
  if ($category_id == "2") return 101; 
  return 102; /* when viewing any other product not mentioned above */
 }
 
 /* "ask a question about this product" page */
 if ($page == "shop.ask") return 103;
 
 /* viewing shopping cart page */
 if ($page == "shop.cart") return 104;
 
 /* advanced search page */
 if ($page == "shop.search") return 1041;
 
 /* customer account page */
 if ($page == "account.index") return 1042;
 
 /* cutomer account shipping address page */
 if ($page == "account.shipping") return 1043;
 
 /* customer account add shipping address page */
 if ($page == "account.shipto") return 1044;
 
 /* customer account billing info page */
 if ($page == "account.billing") return 1045;
 
 /* page about saved carts */
 if ($page == "shop.savedcart") return 1046;
 
 /* order details page */
 if ($page == "account.order_details") return 1047;
 
 $last_step = JRequest::getVar("checkout_last_step");
 /* $last_step is:
  * "" on the first checkout page (as long as page is "checkout.index")
  * 1 on the page for shipping method,
  * 2 on the page for taking the credit card number
  * 3 on the page for reviewing the order
  * 4 on the page for confirming the order
  */
 
// now some more possible rules:
 /* checkout, before or after login: */
 if ($page == "checkout.index" && $last_step == "" ) return 105;
 
 /* for selecting shipping method */
 if ($last_step == 1) return 106;
 
 /* page for taking the credit card number */
 if ($last_step == 2) return 107;
 
 /* page for reviewing the order */
 if ($last_step == 3) return 108;
 
 /* page for confirming the order */
 if ($last_step == 4) return 109;
 
}
// END OF VIRTUEMART RULES