Magento2: существующая цитата не загружается через задание cron
Я хочу получить существующую цитату через задание cron, но она возвращает значение null.
Хочу пройти через это $this->quoteFactory->create()->loadByCustomer($custoemrId)
Или
$this->quoteFactory->create()->load($quoteId)
.
Он работает без задания cron, но мне нужно выполнить задание cron.
$checkCartExist = $this->quoteFactory->create()->loadByCustomer($customerId);
$checkCartExist->getAllVisibleItems();
var_dump($checkCartExist->getData()); // returns 0 through a cron
if (!$checkCartExist['entity_id']) {
$cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
$quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote
} else {
$quote = $this->cartRepositoryInterface->getForCustomer($customerId);
}
Cron работает, потому что каждый раз он создает новый элемент предложения.
2 answers
Вы можете попробовать использовать приведенный ниже код в своем файле cron для получения предложений с идентификатором клиента.
protected $quoteFactory;
public function __construct(
\Magento\Quote\Model\QuoteFactory $quoteFactory
) {
$this->quoteFactory = $quoteFactory;
$this->quoteModel=$quoteModel;
}
public function getQuoteCollection($customerId)
{
$quote = $this->quoteFactory->create()->getCollection()->addFieldToFilter('customer_id',$customerId);
return $quote;
}
Надеюсь, это поможет!!!
Вместо использования фабрики цитат используйте репозиторий цитатMagento\Quote\Api\CartRepositoryInterface::getForCustomer
Для этого метода требуется два параметра $customerId, array $sharedStoreIds = []
Таким образом, вы должны предоставить эти два параметра
/** * Where $this->quote is instance of Magento\Quote\Api\CartRepositoryInterface */ $this->quote->getForCustomer({CustomerId}, [{MyStoreId}]);