Override existing OXID eShop functionality

This page describes how to override default OXID eShop functionality.

Note

Examples described here are made in the already installed module. Module installation procedure is described here.

Extending ‘add to basket’ functionality

In this section the existing “module-template” module will be used which logs a product’s id when it is added to the basket.

Override functionality

To override functionality there is a need to create a class in model. Here, the “moduletemplate” module will be used as an example.

There is a need to create a child class - OxidEsales\ModuleTemplate\Model\Basket - which should override OXID eShop class OxidEsales\EshopCommunity\Application\Model\Basket method addToBasket:

└──moduletemplate
   └── src
      └── Model
              └── Basket.php

Note

moduletemplate - This is the root directory of the module-template.

Note

You can also extend module classes, just like shop classes: \OxidEsales\ModuleTemplate\Controller\GreetingController::class => \ExampleVendor\ExampleModule\Controller\GreetingController::class

The class OxidEsales\ModuleTemplate\Model\Basket could have contents like this:

namespace OxidEsales\ModuleTemplate\Model;
use OxidEsales\ModuleTemplate\Service\BasketItemLogger;
use OxidEsales\ModuleTemplate\Traits\ServiceContainer;

class Basket extends Basket_parent
{
    use ServiceContainer;

   /**
    * Method overrides eShop method and adds logging functionality.
    * {@inheritDoc}
    */
    public function addToBasket(
          $productID,
          $amount,
          $sel = null,
          $persParam = null,
          $override = false,
          $bundle = false,
          $oldBasketItemId = null
    ) {
          $logger = $this->getServiceFromContainer(BasketItemLogger::class);
          $logger->log($productID);

          return parent::addToBasket($productID, $amount, $sel, $persParam, $override, $bundle, $oldBasketItemId);
    }
}

In this example method addToBasket is overridden and it adds logging functionality. To override the method one needs to:

  • Extend a Unified Namespace class - <className>_parent, in this case it is Basket_parent.

  • Call parent method, so the chain would not be broken.

Override templates or blocks

For some example how to add/modify the template, check our Tutorials and recipes section here

Don’t forget to register the files to the metadata.php like described here.

Autoload module classes

The file composer.json in module root directory must be created, the modules namespace and autoloading must be defined.

The composer.json file in module root directory could look like this:

{
  "name": "oxid-esales/module-template",
  "description": "This package contains module template for OXID eShop.",
  "type": "oxideshop-module",
  "keywords": ["oxid", "modules", "eShop", "demo"],
  "homepage": "https://www.oxid-esales.com/en/home.html",
  "license": [
    "GPL-3.0-only",
    "proprietary"
  ],
  "require": {
    "php": "^8.0 | ^8.1",
    "symfony/filesystem": "^6.0"
  },
  "autoload": {
    "psr-4": {
      "OxidEsales\\ModuleTemplate\\": "src/",
      "OxidEsales\\ModuleTemplate\\Tests\\": "tests/"
    }
  },
  "minimum-stability": "dev",
  "prefer-stable": true
}

To register a namespace and download dependencies there is a need to run composer update command in project root directory:

composer update

Composer will generate the PSR-4 autoload file with included module. So at this point OXID eShop will be able to autoload classes.

Add entry to module metadata file

OXID eShop needs to know which class should be extended, to do this there is a need to add a record in metadata.php file:

'extend' => [
  \OxidEsales\Eshop\Application\Model\Basket::class => \OxidEsales\ModuleTemplate\Model\Basket::class,
],

For overwriting the shop templates, or some parts of them (blocks), register your module templates in the templates/blocks sections. Read more about the metadata.php under the link for the latest version here: here.