Customize home text page

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 years ago
I have home text page,some thing like this
Welcome to our store..... that is for user not login
If some one login the text should be
Welcome {FirstName} to our store.Where:{FirstName} is FirstName of user login
Any idea for this?
Thanks
9 years ago
bmsoft wrote:
I have home text page,some thing like this
Welcome to our store..... that is for user not login
If some one login the text should be
Welcome {FirstName} to our store.Where:{FirstName} is FirstName of user login
Any idea for this?
Thanks


Hello,

The easiest way is to follow these steps:

1. Find this file: \Views\Home\Index.cshtml

2. In the beginning of the file add these rows:

@using Nop.Core
@using Nop.Core.Domain.Customers
@using Nop.Core.Infrastructure


3. Find this line:

@Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })


and replace it with these:

@{
  var workContext = EngineContext.Current.Resolve<IWorkContext>();

  if (workContext.CurrentCustomer.IsGuest())
  {
    @Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
  }
  else
  {
    @Html.Raw(string.Format("<h2>Hello, {0}</h2>", workContext.CurrentCustomer.Username ?? workContext.CurrentCustomer.Email))
  }
}


Now replace the text in bold (<h2>Hello, {0}</h2>) with your text (do not remove the {0}, because it represents the name or the email of the user).

I hope that helped you !
9 years ago
Thanks for replying my post but my question is just only add FirstName of user login if user alrerady login on home text page
I do not want to replace home text page(when user not login) by FirstName when user login!
Before login
Welcome to our store...blala
After login
Welcome Adam to our store.... blabla
Where Adam is the firstname of user login!
Thanks
9 years ago
bmsoft wrote:
Thanks for replying my post but my question is just only add FirstName of user login if user alrerady login on home text page
I do not want to replace home text page(when user not login) by FirstName when user login!
Before login
Welcome to our store...blala
After login
Welcome Adam to our store.... blabla
Where Adam is the firstname of user login!
Thanks


Hi,

There is not straightforward way for achieving this. You can not insert some kind of tokens inside a topic - as you can see here it is not supported - you may vote for it here.

You can just move the first sentence of your topic in the code below, check if the user is logged and add his name. just change the code this way:

@{
  var workContext = EngineContext.Current.Resolve<IWorkContext>();

  var user = string.Empty;
  
  if (!workContext.CurrentCustomer.IsGuest())
  {
    user = workContext.CurrentCustomer.Username ?? workContext.CurrentCustomer.Email;
    user = user + ", ";
  }
  @Html.Raw(string.Format("<h2>Welcome {0}to our store</h2>", user))
  
  @Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
}
9 years ago
Hi,

You could try a possible solution like following:

***NOTE: the underlined text is the new code***

1) Copy the "TopicBlock.cshtml" file to the "Themes/DefaultClean/Views/Topic" folder

2) Replace entire code of the cloned file with following:

@model TopicModel
@using Nop.Core
@using Nop.Web.Models.Topics
@using Nop.Core.Infrastructure
@using Nop.Core.Domain.Customers
@{
    var workContext = EngineContext.Current.Resolve<IWorkContext>();
    var currentCustomer = workContext.CurrentCustomer;
    var displayName = "guest";

    if (!currentCustomer.IsGuest())
    {
        displayName = currentCustomer.Username ?? currentCustomer.Email;
    }
}
@helper RenderTextWithUserToken(string rawText, string user) {
    @Html.Raw(rawText.Replace("{logged-user}", user))
}

@if (Model.IsPasswordProtected)
{
    <script type="text/javascript">
        $(function () {
            $('#[email protected]').click(function () {
                var topicId = $("#@Html.FieldIdFor(model => model.Id)").val();
                var password = $('#[email protected]').val();
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("TopicAuthenticate")',
                    data: { "id": topicId, "password": password },
                    dataType: 'json',
                    success: function (data) {
                        if (data.Authenticated) {
                            $('#[email protected] .topic-html-content-title h2.topic-html-content-header').html(data.Title);
                            if ($('#[email protected] .topic-html-content-title h2.topic-html-content-header').text().length == 0) {
                                $('#[email protected]').hide();
                            }
                            $('#[email protected] .topic-html-content-body').html(data.Body);
                            $('#[email protected]').hide();
                            $('#[email protected]').show();
                        }
                        else {
                            $('#[email protected]').text(data.Error);
                            $('#[email protected] #[email protected]').select().focus();
                        }
                    }
                });
                return false;
            });
        });

        $(document).ready(function () {
            $('#[email protected]').hide();
            $('#[email protected] #[email protected]').select().focus();
        });
    </script>
    <div class="topic-password" id="[email protected]">
        @using (Html.BeginRouteForm("TopicAuthenticate"))
        {
            @Html.HiddenFor(model => model.Id)
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { id = "password-" + @Model.Id })
                <input type="submit" id="[email protected]" value="@T("Topic.Button")" class="button-1 topic-password-button" />
            </div>
            <div class="password-error">
                <span id="[email protected]"></span>
            </div>
        }
    </div>
    <div class="topic-html-content" id="[email protected]">
        <div id="[email protected]">
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @RenderTextWithUserToken(Model.Title, displayName)
                </h2>
            </div>
        </div>
        <div class="topic-html-content-body">
            @RenderTextWithUserToken(Model.Body, displayName)
        </div>
    </div>
}
else
{
    <div class="topic-html-content">
        @if (!String.IsNullOrEmpty(Model.Title))
        {
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @RenderTextWithUserToken(Model.Title, displayName)
                </h2>
            </div>
        }
        <div class="topic-html-content-body">
            @RenderTextWithUserToken(Model.Body, displayName)
        </div>
    </div>
}

3) Go to Admin > Content Management > Topics (Pages), select the "HomePageText" topic
    + In the "Title" field, replace text with new one "Welcome {logged-user} to our store"
    + In the "Body" field, replace text with new one "Dear {logged-user}, Online shopping ..."

4) Deploy entire changes to your nopCommerce server folder, restart application to take affect.

Hope this help :)
9 years ago
Try this:

@using Nop.Services.Common;
@{
    
    string _firstName = String.Empty;
    var workContext = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Core.IWorkContext>();
    _firstName = workContext.CurrentCustomer.GetAttribute<string>(Nop.Core.Domain.Customers.SystemCustomerAttributeNames.FirstName);
  
}
9 years ago
ima9ines wrote:
Hi,

You could try a possible solution like following:

***NOTE: the underlined text is the new code***

1) Copy the "TopicBlock.cshtml" file to the "Themes/DefaultClean/Views/Topic" folder

2) Replace entire code of the cloned file with following:

@model TopicModel
@using Nop.Core
@using Nop.Web.Models.Topics
@using Nop.Core.Infrastructure
@using Nop.Core.Domain.Customers
@{
    var workContext = EngineContext.Current.Resolve<IWorkContext>();
    var currentCustomer = workContext.CurrentCustomer;
    var displayName = "guest";

    if (!currentCustomer.IsGuest())
    {
        displayName = currentCustomer.Username ?? currentCustomer.Email;
    }
}
@helper RenderTextWithUserToken(string rawText, string user) {
    @Html.Raw(rawText.Replace("{logged-user}", user))
}

@if (Model.IsPasswordProtected)
{
    <script type="text/javascript">
        $(function () {
            $('#[email protected]').click(function () {
                var topicId = $("#@Html.FieldIdFor(model => model.Id)").val();
                var password = $('#[email protected]').val();
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("TopicAuthenticate")',
                    data: { "id": topicId, "password": password },
                    dataType: 'json',
                    success: function (data) {
                        if (data.Authenticated) {
                            $('#[email protected] .topic-html-content-title h2.topic-html-content-header').html(data.Title);
                            if ($('#[email protected] .topic-html-content-title h2.topic-html-content-header').text().length == 0) {
                                $('#[email protected]').hide();
                            }
                            $('#[email protected] .topic-html-content-body').html(data.Body);
                            $('#[email protected]').hide();
                            $('#[email protected]').show();
                        }
                        else {
                            $('#[email protected]').text(data.Error);
                            $('#[email protected] #[email protected]').select().focus();
                        }
                    }
                });
                return false;
            });
        });

        $(document).ready(function () {
            $('#[email protected]').hide();
            $('#[email protected] #[email protected]').select().focus();
        });
    </script>
    <div class="topic-password" id="[email protected]">
        @using (Html.BeginRouteForm("TopicAuthenticate"))
        {
            @Html.HiddenFor(model => model.Id)
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { id = "password-" + @Model.Id })
                <input type="submit" id="[email protected]" value="@T("Topic.Button")" class="button-1 topic-password-button" />
            </div>
            <div class="password-error">
                <span id="[email protected]"></span>
            </div>
        }
    </div>
    <div class="topic-html-content" id="[email protected]">
        <div id="[email protected]">
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @RenderTextWithUserToken(Model.Title, displayName)
                </h2>
            </div>
        </div>
        <div class="topic-html-content-body">
            @RenderTextWithUserToken(Model.Body, displayName)
        </div>
    </div>
}
else
{
    <div class="topic-html-content">
        @if (!String.IsNullOrEmpty(Model.Title))
        {
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @RenderTextWithUserToken(Model.Title, displayName)
                </h2>
            </div>
        }
        <div class="topic-html-content-body">
            @RenderTextWithUserToken(Model.Body, displayName)
        </div>
    </div>
}

3) Go to Admin > Content Management > Topics (Pages), select the "HomePageText" topic
    + In the "Title" field, replace text with new one "Welcome {logged-user} to our store"
    + In the "Body" field, replace text with new one "Dear {logged-user}, Online shopping ..."

4) Deploy entire changes to your nopCommerce server folder, restart application to take affect.

Hope this help :)

What happen if i want to add more this
Not login
Welcome  to our store
When login
Welcome {logged-user} back to our store
The different is add more word "back"
Thanks
9 years ago
Okie, I have another possible solution for you.
Please clear everything in the previous change and then apply a new one :)

Following are steps:

1) Create a new partial view named "_TopicBlockWithCustomTokens.cshtml" inside the "Themes/DefaultClean/Views/Shared" folder

2) Place following code inside it:

@model IList<string>
@using Nop.Core
@using Nop.Core.Domain.Customers
@using Nop.Core.Infrastructure
@using Nop.Services.Localization
@{
    var engineContext = EngineContext.Current;
    
    var workContext = engineContext.Resolve<IWorkContext>();
    var localizationService = engineContext.Resolve<ILocalizationService>();
    
    var currentCustomer = workContext.CurrentCustomer;

    var textForGuest = Model[0];
    var textForLoggedUser = Model[1];

    var displayName = localizationService.GetResource("customer.guest");
    var isGuest = currentCustomer.IsGuest();

    if (!isGuest)
    {
        displayName = currentCustomer.Username ?? currentCustomer.Email;
    }
}
@helper RenderTextWithUserToken(MvcHtmlString htmlString, string user)
{
    @Html.Raw(htmlString.ToString().Replace("{logged-user}", user))
}
@RenderTextWithUserToken(Html.Action("TopicBlock", "Topic", new { systemName = isGuest ? textForGuest : textForLoggedUser }), displayName)

3) In the "Themes/DefaultClean/Views/Home/Index.cshtml" view, modify following code:

BEFORE

@Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })

AFTER

@Html.Partial("_TopicBlockWithCustomTokens", new List<string>() { "HomePageTextForGuest", "HomePageTextForLoggedUser" })

4) Create 2 new topics in Admin, one for guest and one for logged user.
For example:
- For guest:
  + System name: "HomePageTextForGuest"
  + Title: "Welcome {logged-user} to our store"
  + Body: "Dear {logged-user}, ..."

- For logged user:
  + System name: "HomePageTextForLoggedUser"
  + Title: "Welcome {logged-user} back to our store"
  + Body: "Dear {logged-user}, we are so happy to ..."

Hope this help :)
7 years ago
How to change the text color to GREEN when a product is IN STOCK and RED when a product is OUT OF STOCK?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.