我希望覆盖\ app \ code \ Magento \ Catalog \ Model \ ProductRepository.php中的标准Magento2 getList()方法
我尝试了$ collection-> addFinalPrice()和$ collection-> addMinimalPrice(),但是,即使有商品目录折扣,仍会返回完整的商品价格。
如果我添加$ collection-> addWebsiteNamesToResult() - 没有任何反应,我想添加一个产品分配到该集合的网站列表 - 我将如何解决这个问题?
以下是已编辑代码的示例(仅用于测试目的)。
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();
$this->extensionAttributesJoinProcessor->process($collection);
foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) {
$collection->addAttributeToSelect($metadata->getAttributeCode());
}
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
//Add filters from root filter group to the collection
foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}
/** @var SortOrder $sortOrder */
foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
$field = $sortOrder->getField();
$collection->addOrder(
$field,
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
//ADDED CODE HERE------------------------------
$collection->addWebsiteNamesToResult();
$collection->addFinalPrice();
$collection->applyFrontendPriceLimitations();
//END ADDED CODE-----------------------------------
$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
$collection->load();
$searchResult = $this->searchResultsFactory->create();
$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setItems($collection->getItems());
$searchResult->setTotalCount($collection->getSize());
return $searchResult;
}