Skip to content
intermediate Phase 36 · Service Contracts

Magento Interfaces

Product interface, order interface, customer interface, and creating custom interfaces.

45m
0 problems
Topic Progress 0%

Core Magento Interfaces

Magento provides interfaces for all major entities.

Product interface:

use Magento\Catalog\Api\Data\ProductInterface;

$product = $this->productFactory->create();
$product->setName('My Product');
$product->setSku('MY-SKU');
$product->setPrice(29.99);
$product->setStatus(1);
$product->setVisibility(4);

Key product interface methods:

getId()
getName() / setName()
getSku() / setSku()
getPrice() / setPrice()
getStatus() / setStatus()
getVisibility() / setVisibility()
getAttributeSetId() / setAttributeSetId()
getExtensionAttributes()

Order interface:

use Magento\Sales\Api\Data\OrderInterface;

$order = $this->orderRepository->get($orderId);
$order->getIncrementId();
$order->getStatus();
$order->getGrandTotal();
$order->getItems();
$order->getShippingAddress();

Customer interface:

use Magento\Customer\Api\Data\CustomerInterface;

$customer = $this->customerRepository->getById($customerId);
$customer->getEmail();
$customer->getFirstname();
$customer->getLastname();
$customer->getGroupId();

Custom Interface Creation

Creating custom interfaces for your modules.

Custom entity interface:

<?php
namespace Vendor\Blog\Api\Data;

interface PostInterface
{
    const ENTITY_ID = 'entity_id';
    const TITLE = 'title';
    const CONTENT = 'content';
    const STATUS = 'status';
    const AUTHOR_ID = 'author_id';
    const URL_KEY = 'url_key';
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';
    
    public function getId(): ?int;
    public function getTitle(): string;
    public function setTitle(string $title): self;
    public function getContent(): string;
    public function setContent(string $content): self;
    public function getStatus(): int;
    public function setStatus(int $status): self;
    public function getAuthorId(): ?int;
    public function setAuthorId(int $authorId): self;
    public function getUrlKey(): ?string;
    public function setUrlKey(string $urlKey): self;
    public function getCreatedAt(): ?string;
    public function setCreatedAt(string $createdAt): self;
    public function getUpdatedAt(): ?string;
    public function setUpdatedAt(string $updatedAt): self;
}

Interface naming conventions:

PostInterface — Data interface for entity
PostRepositoryInterface — Repository for CRUD
PostSearchResultsInterface — Search results
PostServiceInterface — Business operations
PostExtensionAttributesInterface — Extension attributes

Interface Usage Patterns

Common patterns for using interfaces in Magento.

Type-hinting interfaces:

// GOOD: Depend on interface
public function __construct(
    private \Magento\Catalog\Api\ProductRepositoryInterface $productRepo
) {}

// BAD: Depend on concrete class
public function __construct(
    private \Magento\Catalog\Model\ProductRepository $productRepo
) {}

Repository returning interface:

public function get($id): PostInterface
{
    $post = $this->postFactory->create();
    $this->resource->load($post, $id);
    
    if (!$post->getId()) {
        throw new NoSuchEntityException();
    }
    
    return $post; // Post implements PostInterface
}

Service method with interface:

public function publish(PostInterface $post): PostInterface
{
    $post->setStatus(PostInterface::STATUS_PUBLISHED);
    $post->setPublishedAt($this->date->formatDate());
    
    return $this->postRepository->save($post);
}

Interface in di.xml:

<!-- Bind interface to implementation -->
<preference for="Vendor\Blog\Api\PostRepositoryInterface"
            type="Vendor\Blog\Model\PostRepository"/>

<!-- Override for specific area -->
<type name="Vendor\Blog\Api\PostRepositoryInterface">
    <arguments>
        <argument name="cacheEnabled" xsi:type="boolean">true</argument>
    </arguments>
</type>

Interface Best Practices

Best practices for interface design.

1. Keep interfaces focused:

// BAD: Too many responsibilities
interface PostInterface
{
    // 50+ methods
}

// GOOD: Focused interface
interface PostInterface
{
    // Core data methods only
}

interface PostServiceInterface
{
    // Business operations
}

2. Return self for fluent interface:

interface PostInterface
{
    public function setTitle(string $title): self;
    public function setContent(string $content): self;
    public function setStatus(int $status): self;
}

// Usage: fluent chaining
$post->setTitle('Title')
    ->setContent('Content')
    ->setStatus(1);

3. Use nullable for optional fields:

interface PostInterface
{
    public function getId(): ?int; // Nullable
    public function getTitle(): string; // Required
    public function getAuthorId(): ?int; // Optional
}

4. Document interfaces:

/**
 * Post data interface
 *
 * @api
 */
interface PostInterface
{
    /**
     * Get post title
     *
     * @return string
     */
    public function getTitle(): string;
}

5. Don't change published interfaces:

- Adding methods: Minor version
- Changing signatures: Major version
- Removing methods: Breaking change

Quiz

1. What interface is used for product data?

Question 1 options

2. What does the @api annotation indicate?

Question 2 options

3. Why return self in interface methods?

Question 3 options

4. Where should interfaces be placed?

Question 4 options

Flashcards

Question

What is the product data interface?

Answer

Magento\Catalog\Api\Data\ProductInterface

Question

What does @api annotation mean?

Answer

Stable public interface, won't change without major version

Question

Why return self in methods?

Answer

Enables fluent method chaining

Question

Where are interfaces placed?

Answer

Module/Api/ directory

Question

How do you bind an interface?

Answer

<preference for="Interface" type="Implementation"/>

Revision Notes

Key Takeaways

  • 1. Core interfaces: ProductInterface, OrderInterface, CustomerInterface
  • 2. Custom interfaces go in module's Api/ directory
  • 3. Return self for fluent interface chaining
  • 4. Use nullable for optional fields
  • 5. @api annotation marks stable public API
  • 6. Bind interfaces via di.xml <preference>

Interview Tips

  • Name the core Magento entity interfaces
  • Explain how to create custom interfaces
  • Discuss interface naming conventions
  • Know how to bind interfaces to implementations

Cheat Sheet

Magento Interfaces Cheat Sheet

Core interfaces:

  • ProductInterface (Catalog)
  • OrderInterface (Sales)
  • CustomerInterface (Customer)

Custom interface naming:

  • EntityInterface — Data
  • EntityRepositoryInterface — CRUD
  • EntityServiceInterface — Business

Best practices:

  • Return self for fluent interface
  • Use nullable for optional
  • Document with @api
  • Keep focused

Binding:

<preference for="Interface" type="Implementation"/>