Change theme based on domain?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
relish27
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Mon Jun 02, 2008 2:50 pm

Change theme based on domain?

Post by relish27 » Mon Oct 25, 2010 9:56 am

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.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Change theme based on domain?

Post by mazhar » Tue Oct 26, 2010 9:01 am

Read following discussion, it may provide you some information about this
viewtopic.php?f=42&t=7519

relish27
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Mon Jun 02, 2008 2:50 pm

Re: Change theme based on domain?

Post by relish27 » Fri Oct 29, 2010 8:49 am

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.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Change theme based on domain?

Post by mazhar » Fri Oct 29, 2010 9:22 am

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>

relish27
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Mon Jun 02, 2008 2:50 pm

Re: Change theme based on domain?

Post by relish27 » Wed Nov 03, 2010 7:24 am

Thanks, Mazhar. I didn't get an email notification so I just saw this now. I will give it a shot.

relish27
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Mon Jun 02, 2008 2:50 pm

Re: Change theme based on domain?

Post by relish27 » Wed Nov 03, 2010 3:43 pm

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!

MerlinMags
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Wed Nov 10, 2010 4:23 am

Re: Change theme based on domain?

Post by MerlinMags » Wed Nov 10, 2010 5:01 am

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?

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Change theme based on domain?

Post by jmestep » Wed Nov 10, 2010 5:16 am

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);
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

Post Reply