I have found the some extension from Amasty and here is the snipped code to delete the data using objectManager :
public function deleteBy($methodId)
{
/**
* @var \Amasty\ShippingTableRates\Model\ResourceModel\Rate $resource
*/
$resource = $this->_objectManager->get('Amasty\ShippingTableRates\Model\ResourceModel\Rate');
$resource->deleteBy($methodId);
}
I know that from using objectManager is not the recommended way from here and here.
The problem using objectManager when I try to change the behavior of import csv file on this class using Plugin it breaks the delete function, and every time admin try to delete always shows this error :
array_values() expects parameter 1 to be array, object given in /var/www/html/vendor/magento/framework/Interception/Interceptor.php on line 146
Here is my Plugin :
use Amasty\ShippingTableRates\Model\Rate as RateAmasty;
class Rate extends RateAmasty
{
/**
* Change total number column
* We change the hidden column == 0 because additional column
*/
const COL_NUMS = 20;
const HIDDEN_COLUMNS = 0;
/**
* index for generated columns
*/
const NUM_ZIP_FROM = 16;
const NUM_ZIP_TO = 17;
/**
* import csv file
*/
public function aroundImport(RateAmasty $subject, \Closure $proceed,$methodId, $fileName)
{
$err = [];
$fp = $this->_file->fileOpen($fileName, 'r');
$methodId = intval($methodId);
if (!$methodId) {
$err[] = __('Specify a valid method ID.');
return $err;
}
$countryCodes = $this->_helper->getCountries();
$countryNames = $this->_helper->getCountries(true);
$typeLabels = $this->_helper->getTypes(true);
$data = [];
$currLineNum = 0;
while (($line = $this->_file->fileGetCsv($fp, self::MAX_LINE_LENGTH, ',', '"')) !== false) {
$currLineNum++;
if ($currLineNum == 1) {
continue;
}
if ((count($line) + self::HIDDEN_COLUMNS) != self::COL_NUMS) {
$err[] = 'Line #' . $currLineNum . ': warning, expected number of columns is ' . self::COL_NUMS;
if (count($line) > self::COL_NUMS) {
for ($i = 0; $i < count($line) - self::COL_NUMS; $i++) {
unset($line[self::COL_NUMS + $i]);
}
}
if (count($line) < self::COL_NUMS) {
for ($i = 0; $i < self::COL_NUMS - count($line); $i++) {
$line[count($line) + $i] = 0;
}
}
}
$dataZipFrom = $this->_helper->getDataFromZip($line[self::ZIP_FROM]);
$dataZipTo = $this->_helper->getDataFromZip($line[self::ZIP_TO]);
$line[self::NUM_ZIP_FROM] = $dataZipFrom['district'];
$line[self::NUM_ZIP_TO] = $dataZipTo['district'];
for ($i = 0; $i < self::COL_NUMS - self::HIDDEN_COLUMNS; $i++) {
$line[$i] = str_replace(["\r", "\n", "\t", "\\", '"', "'", "*"], '', $line[$i]);
}
$countries = [''];
if ($line[self::COUNTRY]) {
$countries = explode(',', $line[self::COUNTRY]);
} else {
$line[self::COUNTRY] = '0';
}
$line = $this->_setDefaultLineValues($line);
$typesData = $this->_prepareLineTypes($line, $err, $currLineNum, $typeLabels);
$line = $typesData['line'];
foreach ($countries as $country) {
if ($country == 'All') {
$country = 0;
}
if ($country && empty($countryCodes[$country])) {
if (in_array($country, $countryNames)) {
$countryCodes[$country] = array_search($country, $countryNames);
} else {
$err[] = 'Line #' . $currLineNum . ': invalid country code ' . $country;
continue;
}
}
$line[self::COUNTRY] = $country ? $countryCodes[$country] : '0';
$statesData = $this->_prepareLineStates($line, $err, $currLineNum, $country, $methodId);
}// countries
} // end while read
fclose($fp);
if (isset($statesData['data_index'])) {
$err = $this->returnErrors($statesData['data'], $methodId, $currLineNum, $statesData['err']);
}
return $err;
}
}
My question is how to modify this delete method to the best way in magento 2 ? Since I'm still new to this big platform and still don't know how.
Amasty\ShippingTableRates\Model\Rate. Should remove it. – Khoa TruongDinh Sep 23 '17 at 04:44