I face a problem with a collection. I have fresh products that need to be taken out from store 3 days before the expiration date. I created a command that is called by a cron. I've constructed it like that :
public function __construct(
CategoryCollection $categoryCollection, CategoryCollection $productCollection, Category $category, HelperData $helper, Utf8force $utf8helper, State $appState, ObjectManagerFactory $objectManagerFactory, \Magento\Framework\App\ResourceConnection $resource, $name = null
) {
$this->_categoryCollection = $categoryCollection;
$this->_productCollection = $productCollection;
$this->_categoryModel = $category;
$this->_moduleHelper = $helper;
$this->_utf8helper = $utf8helper;
$this->_state = $appState;
$this->_resource = $resource;
$params = $_SERVER;
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
$params[StoreManager::PARAM_RUN_TYPE] = 'store';
$this->objectManager = $objectManagerFactory->create($params);
parent::__construct($name);
}
This class contains other function that are working but are not relevant for this question (import new products and categories).
The function is called in the execute
. We did not set if we will deactivate or delete the product so yet it is called deactivate even if I call delete function.
private function _deactivateOldProducts() {
$today = new \DateTime();
$dlc = new \DateTime();
$dlc->add(new \DateInterval('P3D'));
$this->_productCollection->addAttributeToFilter('date_limite',
array(
'from' => $today->format('Y-m-d H:i:s'),
'to' => $dlc->format('Y-m-d H:i:s'),
'date' => true
)
);
foreach ($this->_productCollection as $product) {
$product->delete();
}
die();
}
I tried both things and they do not work. Whatever attribute I try, I get the exception : Invalid attribute name: *****
Thank you for any help.