Are you saying you want to do some additional processing with data on the Product
model inside your plugin?
A before plugin/interceptor should allow you to access the parameters being passed to the original method. In your case I'm guessing the plugin is on Magento\Catalog\Api\ProductRepositoryInterface::save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
method.
In which case, you should be able to access data off of the $product
parameter.
$product->getName()
$product->getPrice()
etc...
Example Plugin
<?php
namespace MyCompany\CustomModule\Plugin;
class BeforeSavePlugin
{
public function beforeSave(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product,
$saveOptions = false
) {
// TODO: this would throw exception for a new product. Need to catch new product scenario.
$oldProduct = $subject->get($product->getSku());
$oldPrice = $oldProduct->getPrice();
$newPrice = $product->getPrice();
// Business logic to alter product before being saved
// ... $product->setPrice()
return [$product, $saveOptions];
}
}
Update 1
Let's say your custom Service class is called \MyCompany\CustomModule\Service\ProductService
.
How To Use Custom Service In Other Classes
- Add a use statement pointing to your Model/Service
- Add your Service to the constructor of the class that needs it (This is Dependency Injection / DI)
- In the body of the class constructor, set the new dependency as a property of the class it's being used in
- Use your Service by referencing the new property you set
See code below for example.
Why the Factory?
Ideally, your model/service class is stateless, which is to say, it doesn't remember anything about what its done whenever you call it. If this is the case, then you don't need to use the Factory.
If there is a chance that whenever your service gets called it will hold on to some information about the product or anything else during processing, you need to create a new instance of it via a Factory to guarantee it doesn't remember anything from the last time it was called.
The Plugin (With Factory):
<?php
namespace MyCompany\CustomModule\Plugin;
// STEP 1
use MyCompany\CustomModule\Service\ProductService;
use MyCompany\CustomModule\Service\ProductServiceFactory;
class BeforeSavePlugin
{
// STEP 3
protected $productServiceFactory;
// STEP 2
public function __construct(
ProductServiceFactory $productServiceFactory
) {
// STEP 3
$this->productServiceFactory = $productServiceFactory;
}
public function beforeSave(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product,
$saveOptions = false
) {
// STEP 4
/** @var ProductService $productService */
$productService = $this->productServiceFactory->create();
$productService->process($product);
return [$product, $saveOptions];
}
}
The Plugin (Without Factory):
<?php
namespace MyCompany\CustomModule\Plugin;
// STEP 1
use MyCompany\CustomModule\Service\ProductService;
class BeforeSavePlugin
{
// STEP 3
protected $productService;
// STEP 2
public function __construct(
ProductService $productService
) {
// STEP 3
$this->productService = $productService;
}
public function beforeSave(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $product,
$saveOptions = false
) {
// STEP 4
$this->productService->process($product);
return [$product, $saveOptions];
}
}