Archive for the ‘Development’ Category

How to bring in Related Product Data into View.phtml with Downloadable Sample Links

Monday, July 26th, 2010

Morning Guys and Girls,

Today we will learn how to bring in related product data into the View.phtml or product colum or middle column.

To see a demo of this have a look at this page:

3AM Psytrance Timecode Records – Multistate – Collaborating with machines

Ready? Let’s do it!


<?php foreach ($_product->getRelatedProducts() as $related_product) {
 if (sizeof($_product->getRelatedProducts())>0):
 ?>
 <?php

$ids[] = $related_product->getId();

echo $relatedproduct->getName(); ?>

That will get you related products. However, I actually extended it quite a bit more, in the sense that my related products are downloadable products, and I needed to get the sample link from these products. The complete code that I used therefor looks like this:


<?php $i = 1; ?>
 <?php foreach ($_product->getRelatedProducts() as $related_product) {
 if (sizeof($_product->getRelatedProducts())>0):
 ?>
 <?php
 $ids[] = $related_product->getId();

 //Download product samples code :)  Ninja Go!!
 $_myprodsamples = Mage::getModel('downloadable/sample');
 $_mySampleCollection = $_myprodsamples->getCollection()->addProductToFilter($related_product->getId());

 $relatedproduct = Mage::getModel('catalog/product');
 $relatedproduct->load($related_product->getId());

 if (sizeof($_mySampleCollection)>0):
 ?>

 <?php
 foreach ($_mySampleCollection as $_sample) {
 //$_samplelink = $this->getUrl('downloadable/download/sample/sample_id/'.$_sample->getId());
 $_samplelink = Mage::getModel('downloadable/sample')->load($_sample->getId())->getUrl();
 ?>
 <span><?php echo $i++; ?><?php echo $this->__('. ') ?><a href="javascript:void(0)" onclick="player1.sendEvent('STOP'); player1.sendEvent('LOAD', {'image':'<?php echo $_product->getImageUrl(); ?>', 'file':'<?php echo $_samplelink ?>', 'provider':'sound', 'buffer':0, 'autostart':true});" title="Click to play <?php echo $relatedproduct->getName(); ?>"><?php echo $relatedproduct->getName(); ?></a></span>
 <?php } ?>
 <?php /*endforeach;*/ ?>

 <?php endif; ?>

 <?php //Mage::Helper('debug')->log($_sample) ?>
 <?php endif; ?>
 <?php } ?>
 <?php /*endforeach;*/ ?>

Beer time!
There you have it, if you don’t come right, use the contact form to contact me, and I will set this up for you.

Have a wonderful day!

Winston

Magento Contact Us – Unable to submit your request. Please, try again later error

Saturday, May 8th, 2010

I had an issue with Magento CE Version 1.4.0.1 where an email would not be sent when a visitor used the contact us page, I found the fix using Google, on ScreencastWorld

In WEBROOT/app/design/frontend/base/default/template/contacts/form.phtml
Look for

<div class="buttons-set">

It should look like this, the important part is the input file name=”hideit”

<div class="buttons-set">
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
</div>

Good luck !

Add All products in Cart to Wishlist

Tuesday, April 6th, 2010

Want to add all the products in your cart to your Wishlist, in one click?
I’ll show you how to do it here ;) This procedure will take you 5 Minutes

We will begin to override the Wishlist Controller.
We do this by creating a folder called Mycompany in your webroot /app/code/local/ and inside it, we’ll add the folder: Addcwl

Inside this folder we will create two extra folders:

etc
controllers

Now, we are ready to begin:

In WEBROOT/app/code/local/Mycompany/Addcwl/etc/ create the file config.xml
Inside this file put the following,

<?xml version="1.0" encoding="utf-8" ?>
<config>
   <!-- snip -->
   <frontend>
      <routers>
         <wishlist>
            <args>
               <modules>
                  <Mycompany_Addcwl before="Mage_Wishlist">
                            Mycompany_Addcwl
                 </Mycompany_Addcwl>
               </modules>
            </args>
         </wishlist>
      </routers>
   </frontend>
</config>

The above code allows you to override the Wishlist controller :)

Now, in WEBROOT/app/code/local/Mycompany/Addcwl/controllers/ create the file

IndexController.php

Inside this file put the following:

<?php

 * @category   BUYX
 * @package
 * @copyright  Copyright (c)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Wishlist front controller
 *
 * @category   BUYX
 * @package
 * @author     Winston nolan <winston@buyxonline.com>
 */

include_once("Mage/Wishlist/controllers/IndexController.php");

class Mycompany_Addcwl_IndexController extends Mage_Wishlist_IndexController
{
	//we test our controller override with this
	/*
	public function indexAction()
	    {
	        # Just to make sure
	        error_log('Yes, I did it!');
	        parent::indexAction();
	    }
	*/

    /**
     * Adding new item
     */

    public function addcartAction()
    {
        $session = Mage::getSingleton('customer/session');
        $wishlist = $this->_getWishlist();
        if (!$wishlist) {
            $this->_redirect('*/');
            return;
        }
        // We store the id's of our products in the cart, in our core session
        $ids = Mage::getSingleton('core/session')->getWishListIds();
        // Here we explode our id's
        $ids = explode('-', $ids);
                        //do a foreach on them
        		foreach ($ids as $productId) {

        $product = Mage::getModel('catalog/product')->load($productId);

        try {

            $wishlist->addNewItem($productId);
            Mage::dispatchEvent('wishlist_addcart_product', array('wishlist'=>$wishlist, 'product'=>$product));

            //unset wishlist id's in session
	    Mage::getSingleton('core/session')->unsWishListIds();

            if ($referer = $session->getBeforeWishlistUrl()) {
                $session->setBeforeWishlistUrl(null);
            }
            else {
                $referer = $this->_getRefererUrl();
            }
            $message = $this->__('%1$s was successfully added to your wishlist. Click <a href="%2$s">here</a> to continue shopping', $product->getName(), $referer);
            $session->addSuccess($message);
        	}
        	catch (Mage_Core_Exception $e) {
            $session->addError($this->__('There was an error while adding item to wishlist: %s', $e->getMessage()));
        	}
        	catch (Exception $e) {
            //$session->addError($this->__('There was an error while adding item to wishlist.'));
        	}
        $this->_redirect('*');
    	}

 	}
}

Now that have the controller wrapped up, we need to modify one file called sidebar.phtml
The file is located here here:

WEBROOT/app/design/frontend/YOURPACKAGE/YOURTHEME/checkout/sidebar/sidebar.phtml

In this file we’ll make the following change around line 56

<?php //first we'll make our id's null ?>
<?php $ids = null; ?>
    <?php foreach($_items as $_item): ?>
        <?php echo $this->getItemHtml($_item) ?>
         <?php now we'll concatenate our Id's into a string
        <?php $ids .= '-' . $_item['product_id']; ?>
    <?php endforeach; ?>
    <?php
        //finally, we'll store the id's into our core session
    	Mage::getSingleton('core/session')->setWishListIds($ids);

         //setup some debug logging to see what we're doing :)
    	//$session = Mage::getSingleton('core/session');
    	//Mage::Helper('debug')->log($session);
     ?>

And we’ll add the Add All to Wishlist button/link here:

<!-- add all items to wishlist -->
        <button style="margin-right:5px;" class="form-button" type="button" onclick="setLocation('<?php echo Mage::getBaseUrl(); ?>wishlist/index/addcart/product/')">
            <span><?php echo $this->__('Add to my Wishlist') ?></span>
        </button>

And now for the Grand Finale!
We’ll activate our module by creating a file in

WEBROOT/app/etc/modules/Mycompany_Addcwl.xml
Inside this file we’ll put the following

<?xml version="1.0"?>
<config>
 <modules>
 <!-- Activate Add All in Cart to Wishlist Module -->
   <Mycompany_Addcwl>
     <active>true</active>
     <codePool>local</codePool>
   </Mycompany_Addcwl>
 </modules>
</config>

Congratulations, have a beer! You can now add all the products in your cart to your wishlist :)

If you know how to Debug, you’ll know how to code

Monday, April 5th, 2010

A very good friend of mine, a JAVA coder, once told me.

“When I started coding, all I focused on, was learning how to DEBUG properly – I wanted to KNOW what was happening there.”

This is probably the best advice I can offer someone new to Development. Learn how to debug your code from day one, so that you’ll have a clear idea of what your code is doing.

Now, working with Magento you’ll need to know how to debug your code.
Of course you can use the old faithful

print_r($variable);
var_dump($variable);

However, Magento objects are often times very large, and the output will be painful to understand and read.

However, there is a better way.

Install FireFox, and add the extensions

Then install the excellent Magento Debug Module by netresearch
Log into the Magento Admin Backend, and navigate to System->Configuration->Developers Options

Enable the Debug module.
Now in your Magento Code, when you want to debug a variable, add the following

Mage::Helper('debug')->log($variable);

Open the FireBUG console and reload the page, and you’ll see a collapsible output of your object

Open Source at Buyx Online

Thursday, October 29th, 2009

As one of my first posts on our shiny new website, I thought I’d highlight some of the technologies we use. Many of them, if not most, are licensed under some or another Open License, such as the GNU Public License.

To give you an example, Linux, is Open Source. The meaning Open Source, literally means that the application’s Source Code is available to anyone. This is an important fact. Another term to describe Open Source, is “Free Software”.

“Open Source” and “Free Software” in this context mean’s, that you are “Free” to change the code of the application. It does not refer to the price of the software. That said, many Open Source software are absolutely Free, it does not cost anything, and you are Free to change the source code.

Now, many people are not familiar with the Development Process that Software follows, and Open Source are no different. Code gets written, checked into a “Version Control” server, where some other developers “Check-out” the code, and then make changes to that code, it could be improvements, or bug fixes. That code is then again “Checked into” a “Version Control” server where the process get’s repeated.

What the above means is that Open Source code is generally very stable, and secure, and has been worked on by many people, until finally, you, the end user, can download and install the program. Examples of such programs are,

Firefox, Open Office, Java, Gimp Image Editor, Ubuntu Linux and many more ..

We at Buyx Online use Open Source applications. In fact, Open Source plays a pivotal role at Buyx Online. All our servers run Ubuntu Linux, our Database Servers run MySQL, and our Web Servers run Apache.

Our eCommerce websites are build on Magento eCommerce Platform, running on the Zend Framework. Absolutely the latest and gratest.

Our Monitoring solution, runs on an encrypted laptop, it’s only accessible to our Systems Administrators, the application runs on Nagios Network Monitor and Cacti RRD Graphs.

Over the next couple of weeks we’ll be posting more articles, and take a look at each solution in depth