Page 1 of 1

Change theme based on domain?

Posted: Mon Oct 25, 2010 9:56 am
by relish27
Hi.

We would like to be able to have the following: 2 domains, 2 themes, 1 database.

I know there is the domain add-on, which allows two domains to be associated with one store -- but has anyone done the custom coding required to change the theme based on the domain? This is what is required in order for this to work.

Thanks.

Re: Change theme based on domain?

Posted: Tue Oct 26, 2010 9:01 am
by mazhar
Read following discussion, it may provide you some information about this
viewtopic.php?f=42&t=7519

Re: Change theme based on domain?

Posted: Fri Oct 29, 2010 8:49 am
by relish27
Thanks. That information was related, but didn't get into how to actually do it.

I did a quick search and this part of the code looks promising:

Code: Select all

    public static void BindCmsTheme(Page page, CatalogableBase catalogObject)
    {
        string theme = catalogObject.ActiveTheme;
        if (!string.IsNullOrEmpty(theme))
        {
            if (CommerceBuilder.UI.Styles.Theme.Exists(theme))
            {
                page.Theme = catalogObject.ActiveTheme;
            }
            else
            {
                //INVALID THEME SPECIFIED, LOG WARNING
                Logger.Warn("Invalid theme '" + theme + "' specified for catalog item " + catalogObject.Name + "; theme not applied.");
            }
        }
    }
Seems like you could set the theme there based on the current URL. Have you or anyone else ever done this?

The domain add-on product description (http://www.ablecommerce.com/Domain-Add- ... 68C51.aspx) says "With customization, it is possible to create different websites that access the same AbleCommerce database. This would typically be the reason to use a dual license key." So it sounds possible.

Re: Change theme based on domain?

Posted: Fri Oct 29, 2010 9:22 am
by mazhar
Give it a try, create a file and name it ThemeHelper.cs. Then copy past following contents into it and upload this file to your Website/App_Code folder

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CommerceBuilder.Common;
using CommerceBuilder.Utility;

/// <summary>
/// Summary description for ThemeHelper
/// </summary>
public class ThemeHelper
{
    public static void BindTheme(Page page, string url)
    {
        string theme = (url.Contains("YourFirstDomain.xyz")) ? "FirstTheme" : "SecondTheme";
        if (!string.IsNullOrEmpty(theme))
        {
            if (CommerceBuilder.UI.Styles.Theme.Exists(theme))
            {
                page.Theme = theme;
            }
            else
            {
                //INVALID THEME SPECIFIED, LOG WARNING
                Logger.Warn("Invalid theme '" + theme + "; theme not applied.");
            }
        }
    }
}
Now update YourFirstDomain.xyz text with the actual name of your default domain. Finally specify default theme name in place of FirstTheme and alternative theme name for "SecondTheme".
Now you need to make all your store pages to make use of this method. You need to add ThemeHelper usage statement in Page_PreInit method in all retail pages. For example edit ContactUs.aspx file on root and make its content look like

Code: Select all

<%@ Page Language="C#" MasterPageFile="~/Layouts/Scriptlet.master" Inherits="CommerceBuilder.Web.UI.AbleCommercePage" Title="Contact Us" %>
<%@ Register Assembly="CommerceBuilder.Web" Namespace="CommerceBuilder.Web.UI.WebControls.WebParts" TagPrefix="cb" %>

<script runat="server">
protected void Page_PreInit(object sender, EventArgs e)
    {
        ThemeHelper.BindTheme(this, Request.RawUrl);
    }
</script>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="PageContent">
    <cb:ScriptletPart ID="ContactUs" runat="server" Layout="Left Sidebar" Header="Standard Header" LeftSidebar="Our Departments" Content="Contact Us" Footer="Standard Footer" Title="Contact Us" AllowClose="False" AllowMinimize="false" />
</asp:Content>

Re: Change theme based on domain?

Posted: Wed Nov 03, 2010 7:24 am
by relish27
Thanks, Mazhar. I didn't get an email notification so I just saw this now. I will give it a shot.

Re: Change theme based on domain?

Posted: Wed Nov 03, 2010 3:43 pm
by relish27
This worked!!!! Mazhar, thanks so much for your guidance.

The only modification I made was to use Request.Url.AbsoluteUri instead of Request.RawUrl:

Code: Select all

<script runat="server">
    protected void Page_PreInit(object sender, EventArgs e)
    {        
        ThemeHelper.BindTheme(this, Request.Url.AbsoluteUri);
    }
</script>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="PageContent">
    <cb:ScriptletPart ID="ContactUs" runat="server" Layout="Left Sidebar" Header="Standard Header" LeftSidebar="Our Departments" Content="Contact Us" Footer="Standard Footer" Title="Contact Us" AllowClose="False" AllowMinimize="false" />
</asp:Content>
I have only tested out the Contact page that you mentioned, but it seems proof that this is possible.

Thank you!

Re: Change theme based on domain?

Posted: Wed Nov 10, 2010 5:01 am
by MerlinMags
We need to manage themse based on domain name too.

I see you were writing your own C# there....does that mean you had to purchase and modify the AbleCommerce source? Or was this possible by just adding a bit of code to one or two folders?

Re: Change theme based on domain?

Posted: Wed Nov 10, 2010 5:16 am
by jmestep
mazar's themehelper.cs wouldn't need the source code. You just create a file called themehelper.cs in the App_Code folder and pick up the code in your display pages (category.aspx, product.aspx, etc) from that by replacing one line of code:
PageHelper.BindCmsTheme(this, _Product);