🎓 Magento 2 Practical Exercises – Hands-on Magento Mastery
Ready to apply your Magento 2 knowledge and strengthen your skills? These practical exercises will help you implement real-world Magento concepts from products to categories, checkout, and more. Let’s dive into building hands-on Magento 2 solutions!
🛠️ Exercise 1: Creating a Simple Product
Start by creating a simple product manually through the Magento Admin Panel. Afterward, you’ll use a script to create this product programmatically.
// product-creator.php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $obj->create('Magento\Catalog\Model\Product');
$product->setSku('test-product')
->setName('Test Product')
->setPrice(99.99)
->setTypeId('simple')
->setAttributeSetId(4) // Default Attribute Set
->setStatus(1) // Enabled
->setVisibility(4) // Catalog, Search
->save();
echo "Product Created!";
🛠️ Exercise 2: Creating a Category
Next, let’s create a new category for your product. We’ll use the same script approach to build a category in Magento.
// category-creator.php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$category = $obj->create('Magento\Catalog\Model\Category');
$category->setName('Test Category')
->setIsActive(true)
->setParentId(2) // Default Root Category
->setLevel(2)
->setPath('1/2') // Path to Parent Category
->setPosition(10)
->setDescription('This is a test category')
->save();
echo "Category Created!";
🛠️ Exercise 3: Product Assignment to Category
Now that we have a product and a category, let’s assign the product to the category we just created.
// product-category-assignment.php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $obj->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', 'test-product');
$category = $obj->create('Magento\Catalog\Model\Category')->load(3); // Assuming category ID 3
$product->setCategoryIds([$category->getId()]);
$product->save();
echo "Product Assigned to Category!";
🛠️ Exercise 4: Custom Checkout Button
Let’s add a custom button to the checkout page. This exercise will teach you how to customize the checkout layout by adding a button.
// app/design/frontend/[Vendor]/[theme]/templates/checkout/button.phtml
🛠️ Exercise 5: Create a Customer Attribute
Let’s extend customer functionality by adding a custom attribute that stores a customer’s favorite product.
// customer-attribute-creator.php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$eavSetup = $obj->create('Magento\Eav\Setup\EavSetup');
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'favorite_product',
[
'type' => 'varchar',
'label' => 'Favorite Product',
'input' => 'text',
'required' => false,
'sort_order' => 100,
'visible' => true
]
);
echo "Customer Attribute Created!";
✅ Conclusion
By completing these practical exercises, you’ve gained hands-on experience with key Magento 2 concepts. These exercises cover product creation, category management, checkout customization, and customer attributes. Now you’re ready to take your Magento 2 skills to the next level!