Chapter 4: Adding products to the Gift Registry

This tutorial will pick up where we left off on Chapter 4: Frontend Development, on that chapter started building our first Magento extension a Gift registry module; which supports the following functions:

We will continue to build our extension functionality by allowing customers to add products from the store catalog to their personal gift registries.

If you haven't done so yet, download the extension files from github https://github.com/amacgregor/mdg_giftreg/tree/chapter4

The Items Controller

The first thing we are going to need is a new controller for managing the items inside a gift registry. The controller needs to have the following Actions:

Our skeleton controller class will look like:

For now we will skip adding the logic to the controller actions and we will continue customizing the frontend for allowing logged in customer to add items to the giftregistry.

Since customers can have multiple gift registry we need to provide a small form that gives customers the option to select which giftregistry should the item be added. We need a new block where we can place the logic required for displaying the "add to registry form".

Add To Registry Block

Let's go ahead and create our block class:

This block needs a function that returns a collection of registries from the current customer. Let's call our function getCustomerRegistryCollection(), this function will referenced in the template file we will add next

This function looks for a logged in customer and if one exists it will retrieve a collection of all the registries associated with this customer.

Template and Layout

At this point we added the logic that we need for retrieving the associated registries with the current customer, next we will do the following:

It makes sense to render our block just below the add to cart button in the product view page, let's do it step by step:

Inside this template file we are generating a custom form based on the current customer collection and the current product. We also need to modify the layout files to add a new block to the product page <catalog_product_view> let's take a closer look at the layout file:

We are also checking to see if a customer is currently logged in, if there is not a current customer set then the add to registry for will not appear on the product page.

We also need to tell Magento where to load our custom block and how to loaded, for doing that we will need to modify our custom layout and the product/view.phtml template file

What we are doing is to tell Magento that it should instantiate a new block inside the product.info block in the<catalog_product_view> page, we also need to modify the product view.phtml template to directly call for the new block we just created.

We added our new form just before the add to cart product form is crated, on a default Magento installation the resulting page should look like this:

Product Page

Right now if we try to add a product to the registry we should see the only a blank page, this is because our controller doesn't have any logic yet, is just dumbly returning without rendering, redirecting or doing anything whatsoever. It's time to change that.

Back to the Controller

Our controller action will be receiving the form data using POST, to create a new registry item we only need two variables, the registryid and the productid.

Let's go ahead and modify the controller addAction()

If we now try to add a product to our registry we will the following message

Product Page

That's all what is needed to add products to our giftregistries on the next articles we will discuss how to to list them under they parent registry view, delete them and update them.