How To Get SKUs From Orders In Magento

This Magento tutorial will show you how to get all SKUs from orders of the user who is currently logged in in Magento.

For Example:

A user has placed 3 orders.

  • 1st order contains SKUs: 0001, 0002 and 0003
  • 2nd order contains SKUs: 0002 and 0004
  • 3rd order contains SKUs: 0001, 0002 and 0005

How can we get the returned list of SKUs including 0001, 0002, 0003, 0004, and 0005?

The aim of this is to create a list of all the products that the user has ordered on one page along with the product thumbnail, name, and a link to the product page.

3 Steps To Get SKUs From Orders In Magento

Step 1: Add a new tab to My Account

<layout version="0.1.0">
...
<customer_account>
	<reference name="customer_account_navigation">
            <action method="addLink" translate="label" module="yourmodule">
                <name>name</name>
                <path>controller_of_custommodule</path>
                <label>Your Label</label>
            </action>
        </reference>
</customer_account>
<yourmodule_default>
	<update handle="page_two_columns_left" />
	<reference name="left_first">
          <block type="customer/account_navigation" name="customer_account_navigation" before="-" template="customer/account/navigation.phtml">
            <action method="addLink" translate="label" module="customer"><name>account</name><path>customer/account/</path><label>Account Dashboard</label></action>
            <action method="addLink" translate="label" module="customer"><name>account_edit</name><path>customer/account/edit/</path><label>Account Information</label></action>
            <action method="addLink" translate="label" module="customer"><name>address_book</name><path>customer/address/</path><label>Address Book</label></action>
            <action method="addLink" translate="label" module="sales"><name>orders</name><path>sales/order/history/</path><label>My Orders</label></action>
            <action method="addLink" translate="label" module="wishlist" ifconfig="wishlist/general/active"><name>wishlist</name><path>wishlist/</path><label>My Wishlist</label></action>
            <action method="addLink" translate="label" module="yourmodule">
              <name>name</name>
              <path>controller_of_custommodule</path>
              <label>Your Label</label>
            </action>
          </block>
        </reference>
</yourmodule_default>
<yourmodule_index_index>
	<update handle="yourmodule_default" />
	<reference name="content">
            <block type="yourmodule/nameblock" name="yourmodule.nameblock" template="yourmodule/yourtemplate.phtml"></block>
        </reference>
</yourmodule_index_index>
...
</layout>

Step 2: Create a Block in your module

<layout version="0.1.0">
...
<customer_account>
	<reference name="customer_account_navigation">
            <action method="addLink" translate="label" module="yourmodule">
                <name>name</name>
                <path>controller_of_custommodule</path>
                <label>Your Label</label>
            </action>
        </reference>
</customer_account>
<yourmodule_default>
	<update handle="page_two_columns_left" />
	<reference name="left_first">
          <block type="customer/account_navigation" name="customer_account_navigation" before="-" template="customer/account/navigation.phtml">
            <action method="addLink" translate="label" module="customer"><name>account</name><path>customer/account/</path><label>Account Dashboard</label></action>
            <action method="addLink" translate="label" module="customer"><name>account_edit</name><path>customer/account/edit/</path><label>Account Information</label></action>
            <action method="addLink" translate="label" module="customer"><name>address_book</name><path>customer/address/</path><label>Address Book</label></action>
            <action method="addLink" translate="label" module="sales"><name>orders</name><path>sales/order/history/</path><label>My Orders</label></action>
            <action method="addLink" translate="label" module="wishlist" ifconfig="wishlist/general/active"><name>wishlist</name><path>wishlist/</path><label>My Wishlist</label></action>
            <action method="addLink" translate="label" module="yourmodule">
              <name>name</name>
              <path>controller_of_custommodule</path>
              <label>Your Label</label>
            </action>
          </block>
        </reference>
</yourmodule_default>
<yourmodule_index_index>
	<update handle="yourmodule_default" />
	<reference name="content">
            <block type="yourmodule/nameblock" name="yourmodule.nameblock" template="yourmodule/yourtemplate.phtml"></block>
        </reference>
</yourmodule_index_index>
...
</layout>

Step 3: Create a template and get data from your Block

Wrapping Up

By following our tutorial, you probably got SKUs from orders on your Magento website successfully.

Tigren’s blog provides a lot of similar tutorials, for example, How To Get Attribute ID, Name, Value From Attribute Code In Magento 2, How To Get A Product By REST API and GraphQL In Magento 2, How To Get Product List (Catalog) Through Magento 2 API, and so on. Therefore, you should subscribe to our blog now if you don’t want to miss any of these.

April LeeHow To Get SKUs From Orders In Magento

Related Posts