Saturday, January 29, 2011

Magento-Shopping Cart instant preview

One more Magento blog. Recently we got a requirement of instant preview of top 3 shopping cart items in Magento on mouse over of My cart link at top. See the image below.


To do this you have to edit this file /app/code/core/Mage/Checkout/Block/Links.php. There is a function in it public function addCartLink() . This is the function which generates the HTML of My Cart Link. So here you have to get all the cart items and arrange them in proper layout like above. Following is the code for it.


$cart = Mage::helper('checkout/cart')->getCart()->getItems();
//It gives you recent items in cart
$_items = Mage::helper('checkout/cart')->getCart()->getItems();
$obj = Mage::getModel('catalog/product');

Following line generates the necessary HTML layout for the cart items.

$miniShoppingCart ='
left:-87px;margin-top:20px;background-color:white;width:150px;-moz-box-shadow:0 0 8px #666666;-webkit-box-shadow: #666 0px 0px 8px;border:white;" onmouseover="show_product();" onmouseout="hide_product();" >';

Generating HTML layout for each item in the shopping cart.


foreach($_items as $_item)
{
$_product = $obj->load($_item->getProductId());
if($_product->getTypeId() !="simple")
{
$item_array[$i].='<div >';
$item_array[$i] .='<p style="width:80px;height:50px;text-align:right;margin-bottom:0px;"><a href='.$_product->getProductUrl().' style="text-decoration:none" >'.$_product->getName().'</a>';
$item_array[$i] .='<br/><a style="text-decoration:none" href='.$_product->getProductUrl().' > <span>£</span>'. round($_product->getPrice(),2).'<br />‾ ‾ ‾ ‾ ‾ </a></p>';
$item_array[$i] .='<a style="text-decoration:none" href='.$_product->getProductUrl().'><img height="50" width="50" style="float:right;margin-top:-45px;margin-right:8px; -webkit-box-shadow: #666 0px 0px 8px; -moz-box-shadow:0 0 8px #666666" src='.$_product->getSmallImageUrl().' /></a>';
$item_array[$i].='</div><br/>';
$i++;
$subtotal+=$_item->getPrice()*$_item->getQty();
}
}
$total_items =count($item_array);

Now requirements was to show only recemt three items so following is the code to add only three items.

if($total_items>0)
{
for($j=$total_items;$j>=$total_items-3;$j--)
{
$miniShoppingCart.=$item_array[$j];
}
}
else
{
$miniShoppingCart.="Your Shopping Bag is Empty";
}
$miniShoppingCart .='</div>';

if( $count == 1 ) {
$text = $this->__('Shopping Bag (%s item) £%s', $count,$subtotal);
} elseif( $count > 0 ) {
$text = $this->__('Shopping Bag (%s items) £%s', $count,$subtotal);
} else {
$text = $this->__('Shopping Bag');
}
$parentBlock->addLink($text.$miniShoppingCart, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');

So that was the code for generating HTML layout. Now add following code in Links.phtml file in app/design/frontend/base/default/template/page/template to show and hide the preview.



That's it and you will have instant preview of Shopping cart with recently added three items.




Saturday, January 22, 2011

Side by Side execution of .Net DLL

In my last post I mentioned that I will publish blog on Side by side execution. So here it is.

"Side by side execution is the ability to run multiple versions of same component on the same computer"

Above is the definition of the side by side execution. So how is that possible. I will mention it with code and simple example. But before that let's go through two important terms related to this.

1) Strong named Assembly

Strong name gives unique identity to the assembly.It consists of text name, version information and a public key. You can assign strong name to any assembly using windows SDK or visual studio.Strong name guarantees the name uniqueness for assembly. One assembly is signed with strong name , that will have name that is globally unique. To sign assembly with strong name you need to generate the uniques key from Visual studio command prompt and assign that key to assembly.

2) Global Assembly Cache (GAC)

You can call it as centralized repository of assembly on a Machine. Each computer where a runtime is installed has a GAC. You can share assembly by installing then into GAC. Once you install assembly in GAC, it becomes global. All the application on the same machine can reference that assembly. When you add reference of any assembly from GAC into your application and when you build your application the local copy of DLL will not created in bin/debug or bin/release folder of application along with executable file. It will reference it from GAC. You can install assembly to GAC in two ways.

1) With the development tool called Gacutil.exe
2) Using installer which works with GAC.

Now we will see how to do this using a simple example. Open a visual studio and create a class library project. Add following function to the class and build it.

public string MyFunction() {
return "This is from old version";
}

Now select all files options in solution explorer and go to properties folder. Here you will find AssemblyInfo.cs file open it and see the following lines in the code.

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

This gives you assembly version by default it is 1.0.0.0 version. Now let's assign strong name to assembly. Right click on project select project properties. Go to last tab Signing. Here you will find one checkbox sign the assembly. check it and it will enable choose a key drop down. You can generate new key or you can browse to existing key if you have already generated it using command line. Using command line you can generate it using the command sn -k.

After signing build the project again. Now you can put this DLL into global assembly cache. Open a visual studio command prompt and type the following command

gacutil.exe -i /path of dll file

This will intall the assembly in GAC. Now copy paste the project folder in windows explorer and it will generate the duplicate project for it. Open it in visual studio as we are going to make few changes in it.

Add following function to the class.

public string MyFunction() {
return "This is from new version";
}

Open the AssemblyInfo.cs file and change the following two lines and build the project.

[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]

So this is the new version of the same DLL. Add this also to glaobal assembly cache using command mention above in the post. Now you have two versions of the same DLL in global assembly cache and you can specify version information in line of code in application. Create new windows application project in Visual studio and add reference of first DLL to it. We have to generate config file for this windows application. By default it is not available in the application. You can use Assembly Linker tool or you can add it manually through add new item. Once you generate it through Assembly Linker tool you will see following section in the file.


<dependentAssembly>
<assemblyIdentity name="classLibrary1" publicKeyToken="c0302c47590ba429" /> 
                 <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.0.1"/>  
</dependentAssembly>
Public key token value can be different in your case. Now call the function of the class in DLL.

Class1 obj = new Class1();
MessageBox.Show(obj.MyFunction());
This should show you message box with text "This is from old version". Now make following changes to config file.


<dependentAssembly>
<assemblyIdentity name="classLibrary1" publicKeyToken="c0302c47590ba429" /> 
                 <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.0.1"/>  
</dependentAssembly>

This should show you message box with text "This is from new version". 
So this was the example of Side by side execution.

Monday, January 17, 2011

.Net Assembly

Finally this is my first blog on my first love .Net.

Recently I interviewed some candidates. I asked few basic questions on .Net framework. I was not satisfied with their answer particularly for the answer of question what is .Net Assembly? So this is the blog about .Net Assembly.

One line definition of .Net Assembly is "It is the smallest deployable unit of .Net application" and here is the definition given on MSDN site

"Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly."

So let's first understand why Microsoft has introduced the concept of Assembly with .Net Framework. What was wrong with previous approach?

Before .Net, applications were using standard DLLs and COM components. There might be the case that one DLL was shared between two or more applications. If one application updates the base DLL, other applications crashed because they can not understand the newer version. There was no facility to have more than one version of same DLL so applications using DLL can dynamically call the respective version they need. This situation coined a term called "DLL HELL".

So to resolve this issue Microsoft has introduced the the concept of .Net Assembly. As mentioned in the definition above it is the fundamental block of version control, now we have facility to specify the version information in line of code. Basically Assembly is nothing but your .Net application. If you are creating .Net class library, the DLL is your Assembly. If you are creating console application or windows application, the EXE file is your assembly. But it differs from the other executable files. It contains MSIL code and meta data which useful for CLR. Each assembly has one entry point like for DLL its DLLMain and for windows application its WinMain. In short your main function.

Each assembly has assembly manifest. It contains verious information about assembly like assembly version, culture etc. An executbale file without assembly manifest is called portable executable file.

Each assembly has version number associated with it to define its identity. Two assembly with different version number are treated as completely different assemblies by CLR. Version number are defined in four part as follow.

major-minor-buid-revision

For example 1.0.0.1

Major benefit of using assembly is we can achieve side by side execution. Side by side execution is the ability to use more than one version of same application on one computer. Version information can be stored in line of code. How to do it? I will publish another blog for it.

I hope this helps you.




Saturday, January 8, 2011

Turn ExtJs combo box to Auto Suggest Textbox

First of all this is the first blog of 2011 so wish you a very "Happy New Year"

Recently I was working on ExtJs project. Where I got a requirement to create auto suggest text box. So I was planning to extend ExtJs text field class and add my own methods and properties to make it auto suggest.

Before I start development, I was going through document of ExtJs combo box and suddenly I show a following property description in docs.

hideTrigger : Boolean
true to hide the trigger element and display only the base text field (defaults to false)

It hides the extreme right arrow in combo box and displays only base text box. When user enters some characters in text box it displays relevant combo box values. See the image below.


So that's it by setting just one property in combo box you got your auto suggest text box.

Following is the code .

var autoSuggestTextBox = new Ext.form.ComboBox{
store: mystore,
displayField:'display_field',
id : 'mycombo',
name : 'mycombo',
valueField : 'value_field',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText:'Select......',
selectOnFocus:true,
hideTrigger: true,
width: 250,
listeners:{
select:{
fn:function(combo,record,index) {
}
}
}
}

This saved my complete day else I would have written a code to create auto suggest text box from scratch. So I thought of sharing this so it can save your time also.