I couldn't get a solution working using the code example above. In the end I created an observer on admin product save which caught the post parameters and saved them to the database.
The three columns I added are:
- supplier_name
- supplier_price
- supplier_update
So bit different to my example in bitbucket
Update
As requested. It's not great code. I just wanted to get the job out the door.
etc/adminhtml/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_save_after">
<observer instance="Xigen\NameCost\Observer\Backend\Catalog\ProductSaveAfter" name="xigen_namecost_observer_backend_catalog_productsaveafter_catalog_product_save_after"/>
</event>
</config>
Observer/Backend/Catalog/ProductSaveAfter.php
<?php
namespace Xigen\NameCost\Observer\Backend\Catalog;
class ProductSaveAfter implements \Magento\Framework\Event\ObserverInterface
{
private $logger;
private $request;
private $productTierPriceInterface;
private $productRepository;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\RequestInterface $request,
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $productTierPriceInterface,
\Magento\Catalog\Api\Data\TierPriceInterfaceFactory $tierPriceFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
)
{
$this->logger = $logger;
$this->request = $request;
$this->productTierPriceInterface = $productTierPriceInterface;
$this->tierPriceFactory = $tierPriceFactory;
$this->productRepository = $productRepository;
}
/**
* Execute observer
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$_product = $observer->getProduct();
$params = $this->request->getPostValue();
$dateFormat = "d/m/y";
// print_r($params['product']['tier_price']);
if(isset($params['product']['tier_price'])) {
foreach($params['product']['tier_price'] as $tier) {
if(isset($tier['price_id']) && isset($tier['supplier_price']) && isset($tier['supplier_name'])) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('catalog_product_entity_tier_price');
$select = $connection
->select()
->from(['cpetp' => $tableName])
->where('cpetp.value_id = ?', $tier['price_id']);
$existing = $connection->fetchRow($select);
if($tier['supplier_price'] != $existing['supplier_price'] ||
$tier['supplier_name'] != $existing['supplier_name']) {
try {
$connection->beginTransaction();
$connection->update(
$tableName,
[
'supplier_name' => ((trim($tier['supplier_name']) == '') ? null : $tier['supplier_name']),
'supplier_price' => $tier['supplier_price'],
'supplier_update' => date($dateFormat)
],
['value_id = ?' => $tier['price_id']]
);
$connection->commit();
} catch(\Exception $e) {
$connection->rollBack();
$this->logger->critical($e->getMessage());
}
}
}
}
}
}
}