Skip to content

Instantly share code, notes, and snippets.

@Vinai
Vinai / M2 acl.xml
Last active October 17, 2023 05:38
My current Magento 2 PHPStorm File Templates (Feb 2016)
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
</resource>
</resources>
</acl>
</config>
@Vinai
Vinai / MockTraitMethodTest.php
Created November 12, 2014 16:48
How to mock a trait method using getMockForTrait()
class MockTraitMethodTest extends \PHPUnit_Framework_TestCase
{
public function testTraitMock()
{
// Inspect getMockForTrait signature for what the other arguments do.
$methodsToMock = ['getBar'];
$mockTrait = $this->getMockForTrait('\Example\Foo', [], '', true, true, true, $methodsToMock);
$mockTrait->expects($this->any())
->method('getBar')
->willReturn('buz');
@Vinai
Vinai / fix-url-keys.php
Last active September 2, 2022 16:24
This fixes the duplicate url_key issue in a Magento 1.8 / 1.13 installation.
<?php
/**
* Drop this into the shell directory in the Magento root and run with -h to see all options.
*/
require_once 'abstract.php';
/**
* Fix duplicate url keys for categories and products to work with the 1.8 alpha1 CE url key constraints.
@Vinai
Vinai / or-example.php
Last active September 28, 2021 20:48
SearchCriteria OR Example for Magento 2
<?php
declare(strict_types = 1);
namespace Training5\VendorRepository\Controller\Test;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\FilterBuilder;
@Vinai
Vinai / 1.factory-methods.md
Last active July 6, 2021 05:25
Model & Resource Model Class Resolution Steps

Debugging (Resource) Model Instantiation

Follow each step manually and any bugs during (resource) model instantiation will become obvious.

Since many people find model and resource model instantiation to be one of the more challenging things in Magento 1, these are all the steps Magento does to resolve the factory name to the real PHP class name.

To debug, simply follow each step manually until you find a non-match. This works really well in my experience.
Less guessing, more and faster results.

In the examples I use a factory name of "example/thing".

@Vinai
Vinai / releases.txt
Last active July 6, 2021 05:24
Magento Release Dates
Major Magento Releases
CE 1.0 (Mar 2008)
CE 1.1 (Jul 2008)
CE 1.2 (Dec 2008)
CE 1.3 (Apr 2009)
EE 1.6 (Nov 2009)
EE 1.7 (Jan 2010)
CE 1.4 (Feb 2010)
EE 1.8 (Apr 2010)
@Vinai
Vinai / example.php
Created June 20, 2014 08:34
Loading individual attribute values on catalog/product instances.
<?php
require('htdocs/app/Mage.php');
umask(0);
Mage::setIsDeveloperMode(true);
Mage::app();
$attributeCode = 'description'; // any attribute code
// load model without only static attribute values
@Vinai
Vinai / example.php
Created October 7, 2013 15:58
Set Product Group Price using setData()
$product->setData('group_price', array(
array(
'website_id' => 0,
'cust_group' => $customerGroupId,
'price' => 19.99
)
));
@Vinai
Vinai / magento-composer-issues.md
Last active April 15, 2020 03:09
My beef with composer and Magento
@Vinai
Vinai / 2016-48.md
Last active January 13, 2020 09:25
List of past code katas. The file names are the [year]-[week-of-year].md. I'll try to update the list each week.

The first kata is the classic BowlingGame Kata from Uncle Bob.

Write a class named Game that has two methods

  • roll(pins : int) is called each time the player rolls a ball. The argument is the number of pins knocked down.
  • score() : int is called only at the very end of the game. It returns the total score for that game.

Here is the original PowerPoint from Uncle Bob with the instructions including the solution steps. The PPT file also includes the rules for the scoring of a bowling game.