Magento 2 Snippets Extension for Visual Studio Code
Overview
Welcome to the Magento 2 Snippets extension for Visual Studio Code! This extension is designed to streamline the development process in Magento 2 by providing a collection of code snippets that simplify common tasks. Whether you're a beginner or an experienced Magento 2 developer, these snippets will save you time and boost your productivity.
Installation
- Open Visual Studio Code.
- Access the Extensions view by clicking on the square icon in the sidebar or by pressing
Ctrl+Shift+X.
- Search for "Magento2 snippets."
- Click on "Install" to install the extension.
- Once installed, you can start using the snippets in your Magento 2 projects.
Usage
Using Magento 2 Snippets is straightforward. Follow these steps to start using the snippets in your code:
- Open a Magento 2 project in Visual Studio Code.
- Create or open a PHP, XML, or PHTML file.
- Type the prefix of a snippet and press
Tab to trigger the code snippet.
For example, to pick up a product by ID in Magento 2, type m2.get_product_by_id and press Tab. The extension will automatically generate the code structure for you.
Trigger: m2.get_product_by_id
Output:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = 1; // Replace with the desired product ID
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
echo $product->getName();
// note: Magento does not recommend using ObjectManager in production

Available Snippets
This extension now includes over 287 snippets covering all major Magento 2 development patterns and use cases:
ObjectManager Snippets (Most Used)
m2.om.get: ObjectManager get instance (singleton)
m2.om.create: ObjectManager create new instance
m2.om.create_args: ObjectManager create with arguments
m2.om.configure: ObjectManager configure preferences
Repository Pattern Snippets
m2.product_repository: Get product repository
m2.customer_repository: Get customer repository
m2.order_repository: Get order repository
m2.quote_repository: Get quote repository
m2.category_repository: Get category repository
m2.address_repository: Get address repository
Factory Pattern Snippets
m2.product_factory: Get product factory
m2.customer_factory: Get customer factory
m2.order_factory: Get order factory
m2.address_factory: Get address factory
m2.region_factory: Get region factory
Collection Snippets
m2.product_collection: Get product collection
m2.customer_collection: Get customer collection
m2.order_collection: Get order collection
m2.category_collection: Get category collection
m2.invoice_collection: Get invoice collection
m2.shipment_collection: Get shipment collection
m2.creditmemo_collection: Get creditmemo collection
Collection Operations
m2.collection_filter: Add filter to collection
m2.collection_join: Add join to collection
m2.collection_left_join: Add left join to collection
m2.collection_group: Add group by to collection
m2.collection_order: Add order to collection
m2.collection_limit: Add limit to collection
Core Services
m2.scope_config: Get scope config value
m2.store_manager: Get store manager
m2.url_builder: Get URL builder
m2.resource_connection: Get database resource connection
m2.event_manager: Get event manager and dispatch event
m2.message_manager: Get message manager and add message
m2.cache_manager: Get cache manager
m2.registry: Get registry value
Search & Filtering
m2.search_criteria: Create search criteria
m2.filter_builder: Create filter
m2.sort_order: Create sort order
m2.product_search: Search products using repository
m2.customer_search: Search customers using repository
m2.order_search: Search orders using repository
CRUD Operations
m2.product_save: Save product using repository
m2.customer_save: Save customer using repository
m2.order_save: Save order using repository
m2.product_delete: Delete product using repository
m2.customer_delete: Delete customer using repository
Helpers & Utilities
m2.pricing_helper: Get pricing helper
m2.image_helper: Get image helper
m2.data_helper: Get data helper (escaper)
m2.json_helper: Get JSON helper
m2.serializer: Get serializer
m2.date_time: Get date time helper
m2.timezone: Get timezone helper
System Components
m2.file_system: Get filesystem directory
m2.request: Get request object
m2.response: Get response object
m2.redirect: Get redirect helper
m2.session_manager: Get session manager
m2.backend_session: Get backend session
Advanced Features
m2.custom_attribute: Create custom attribute
m2.extension_attributes: Handle extension attributes
m2.email_template: Send email template
m2.payment_method: Get payment methods
m2.shipping_method: Get shipping methods
m2.quote_management: Get quote management
m2.cart_totals: Get cart totals repository
Inventory & Catalog
m2.inventory_source: Get stock registry
m2.attribute_repository: Get attribute repository
m2.attribute_set: Get attribute set
m2.attribute_option: Get attribute option factory
m2.media_gallery: Add image to media gallery
m2.tier_price: Create tier price
m2.stock_item: Get stock item
XML Configuration Snippets
m2.system_config: System configuration scaffold
m2.system.tab: System configuration tab
m2.system.section: System configuration section
m2.system.group: System configuration group
m2.system.field: System configuration field
m2.acl: ACL scaffold
m2.menu: Admin menu scaffold
m2.webapi: Web API scaffold
m2.crontab: Crontab scaffold
m2.email_templates: Email templates configuration
m2.widget: Widget configuration
m2.db_schema: Database schema
m2.theme: Theme configuration
PHP Base Snippets
m2.class: Basic PHP class
m2.interface: Basic PHP interface
m2.getter: Getter method
m2.setter: Setter method
m2.property_private: Private property with docblock
m2.property_protected: Protected property with docblock
m2.property_public: Public property with docblock
m2.docblock: PHP docblock comment
Legacy Snippets (Maintained for Compatibility)
m2.routes: Create a routes file
m2.layout.block: Create a new block class
m2.logger: Create a log file
m2.object_manager: Create an object manager
m2.list_products: List all products
m2.list_users: List all users
m2.create_order: Create a new order
m2.get_order_by_id: Get an order by ID
m2.get_base_url: Get base URL
m2.helper: Get Helper
See complete legacy command list
m2.get_block_html: Get Block HTML.
m2.log_custom: Log custom message.
m2.log_json_encode: Log JSON encoded data.
m2.customer_session: Handle customer session.
m2.new_session_catalog: Create and handle catalog session.
m2.new_session_customer: Create and handle customer session.
m2.new_session_checkout: Create and handle checkout session.
m2.list_products: List all products.
m2.list_users: List all users.
m2.list_rules: List all sales rules.
m2.create_product: Create a new product.
m2.create_user: Create a new user.
m2.create_rule: Create a new sales rule.
m2.get_product_by_id: Get a product by ID.
m2.get_user_by_id: Get a user by ID.
m2.get_rule_by_id: Get a sales rule by ID.
m2.update_product: Update a product.
m2.update_user: Update a user.
m2.update_rule: Update a sales rule.
m2.delete_product: Delete a product.
m2.delete_user: Delete a user.
m2.delete_rule: Delete a sales rule.
m2.list_orders: List all orders.
m2.get_core_config_data: Get core configuration data.
m2.set_core_config_data: Set core configuration data.
m2.create_cart: Create a shopping cart.
m2.list_carts: List all shopping carts.
m2.layout.args: Add arguments.
m2.layout.arg: Add an argument.
m2.layout.attr.translate: Translate attribute.
m2.layout.attr.xsi:type: xsi:type attribute.
m2.layout.attr.handle: Handle attribute.
m2.layout.attr.destination: Destination attribute.
m2.layout.attr.element: Element attribute.
m2.layout.attr.htmlId: htmlId attribute.
m2.layout.attr.htmlClass: htmlClass attribute.
m2.layout.attr.htmlTag: htmlTag attribute.
m2.layout.attr.output: Output attribute.
m2.layout.attr.label: Label attribute.
m2.layout.attr.as: As attribute.
m2.layout.attr.template: Template attribute.
m2.layout.attr.class: Class attribute.
m2.layout.attr.name: Name attribute.
m2.layout.attr.before: Before attribute.
m2.layout.attr.after: After attribute.
m2.layout.attr.cacheable: Cacheable attribute.
m2.layout.attr.display: Display attribute.
m2.layout.attr.remove: Remove attribute.
m2.layout.container: A structure without content that holds other layout elements such as blocks and containers.
m2.layout.containerWrap: A structure without content that holds other layout elements such as blocks and containers.
m2.layout.refContainer: Targets containers and applies updates specified.
m2.layout.move: Sets the declared block or container element as a child of another element in the specified order.
m2.layout.remove: Is used only to remove the static resources linked in a page section.
m2.layout: Magento 2 layout scaffold.
m2.layout.update: Includes a certain layout file.
m2.module: Magento 2 module.xml.
m2.di: Magento 2 DI scaffold.
m2.di.pref: Magento 2 DI preference.
m2.di.type: Magento 2 DI type.
m2.di.plugin: Magento 2 DI plugin.
m2.di.args: Magento 2 DI type arguments.
m2.di.args.item: Magento 2 DI type argument item.
m2.di.virtualtype: Magento 2 DI virtual type.
m2.events: Magento 2 events scaffold.
m2.events.event: Magento 2 event.
m2.events.observer: Magento 2 event observer.
m2.routes: Magento 2 routes scaffold.
m2.routes.router: Magento 2 router.
m2.routes.route: Magento 2 route.
m2.__construct: Constructor.
m2.echo: 'echo' statement.
m2.foreach: foreach(iterable_expr as $value) {...}.
m2.foreach_k: foreach(iterable_expr as $key => $value) {...}.
m2.include: 'include' statement.
m2.include_once: 'include_once' statement.
m2.private_function: private function.
m2.private_static_function: private static function.
m2.protected_function: protected function.
m2.protected_static_function: protected static function.
m2.public_function: public function.
m2.public_static_function: public static function.
m2.require: 'require' statement.
m2.require_once: 'require_once' statement.
m2.throw_new: throw new.
m2.print_r: PHP print_r() function.
m2.try_catch: Exceptions Block.
m2.try_catch_example: Exceptions Block Example.
m2.globals: $GLOBALS array.
m2.server: $_SERVER array.
m2.request: $_REQUEST array.
m2.post: $_POST array.
m2.get: $_GET array.
m2.file: $_FILES array.
m2.env: $_ENV array.
m2.cookie: $_COOKIE array.
m2.session: $_SESSION array.
m2.for_loop: For loop.
m2.for_end: For end loop.
m2.loop_while: While loop.
m2.while_end: While end loop.
m2.do_while: Do while loop.
m2.switch_case: Switch block.
m2.case: Case addon block.
m2.if_ternary: Ternary statement.
m2.if: If block.
m2.if_else: If else block.
Showing the magento2 version

Customization
You can customize the extension to match your coding style and preferences. To do so, follow these steps:
- Go to File > Preferences > Settings.
- Search for "Magento2 snippets" in the search bar.
- You can modify existing snippets or add your own.
Contributions
This extension is open source, and contributions are welcome. If you have snippets or improvements to share, visit our GitHub repository to make a contribution.
Support
If you encounter issues, have suggestions, or need assistance, please visit the issue tracker on GitHub to report problems or request help.
Happy coding with Magento 2 Snippets in Visual Studio Code!
GitHub Repository
Report Issues
Support Contact