Example GiftItem
This module is a basic example implementation of a gift item module. By default it simply adds a water bottle to the cart when the customer adds a product from the sample data Bags attribute set.
This module is not intended for production use, it is only example code for my presentations at MageTestFest.
Magento 2 Total Model Kata
Check out the tag beginning-of-kata
to start.
Then TDD your way through the creation of the free gift item total model.
collect()
behavior:
Target - Subtract gift item row totals sum from subtotal
- Subtract gift item base row totals sum from base subtotal
- Set
calculation_price
of every gift item to 0 - Set
base_calculation_price
of every gift item to 0 - Call
calcRowTotal
on each item
Suggested TDD Steps
- Create test class
\Example\GiftItem\Test\Unit\Model\Totals\GiftItemAddressTotalTest
- Rename namespace to
Example\GiftItem\Model\Totals
- Extend
\PHPUnit\Framework\TestCase
- Test 1: inherits abstract total model
- Test 2: returns zero if no items are passed
- Test 3: returns zero if non gift item is passed
- Test 4: returns gift item row total
- Test 5: returns sum of gift item row totals only
- Test 6: returns sum of gift item base row totals only
- Test 7: subtracts gift item total sums from subtotal
(for both base row total and row total) - Test 8: zeros gift item calculation_price and base_calculation_price
and calculates the row total and ignores non gift items
(c) 2017 Vinai Kopp
License: BSD-3-Clause