Without any background knowledge of Magento 1.x, I'm currently studying Magento 2 modules and after following quite some tutorials & docs, I now understand the basics. But when it comes to creating my own module that adds some behaviors to the existing system, it really gives me a hard time.
For example, I wanted to add a fee in proportion to the item quantity per order. I knew it has something to do with Magento_Checkout
, Magento_Sales
, Magento_Quote
, but I can hardly tell which class I should extend or which method in those classes actually do the trick.
Eventually I found an working example in this thread and modified a bit:
namespace My\Module\Model\Total;
class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
...
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
...
$fee = $quote->getItemsQty() * 10;
$total->setTotalAmount('fee', $fee);
$total->setBaseTotalAmount('fee', $fee);
$total->setFee($fee);
$total->setBaseFee($fee);
$total->setGrandTotal($total->getGrandTotal() + $fee);
$total->setBaseGrandTotal($total->getBaseGrandTotal() + $fee);
...
}
...
}
and of course some other parts like XMLs, layouts, templates... etc.
But the point is, I feel like I could never reach this solution on my own. The codebase is dauntingly huge that I couldn't find that the 'collect' and 'fetch' method in AbstractTotal is what I need if not for the answered thread.
I wonder if there is a recommended way of tracing the relations between modules. More specifically, when performing a task such as the above, in what order, depth-first or breadth-first, should I analyze the required modules? An example like "I start out reading the xml first then the layout systems..." would be nice.
Hope this topic isn't too vague to answer, thanks for any help.