4

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.

2

its really much about experience with the system.

But, it is very helpful to get xdebug up and running (Here is a tutorial for mac: http://code.tutsplus.com/tutorials/xdebug-professional-php-debugging--net-34396)

So you can start a process, like placing and Order for example and see which classes are involved and when and with what parameters specific funtions are called.

For reading module configuration I think the di.xml is a good starting point for most cases. But you must rely on Magento naming the Classes and Parameters speaking enough that you get an Idea of whats happening there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.