这可能无法真正回答问题,但是很有可能,如果您丢失了URL重写,则可能会将产品从产品集合中删除。正如您在中看到的那样,添加URL重写信息不是自动的\Magento\Catalog\Model\ResourceModel\Product\Collection::$_addUrlRewrite。
我设法强制添加URL重写的create()方法是在的方法上创建一个插件\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory。并且一旦您的代码(或Magento的核心代码)使用该工厂实例化产品集合(并且按照最佳做法),该插件就会强制\Magento\Catalog\Model\ResourceModel\Product\Collection::$_addUrlRewriteto true。
然后,将产品URL重写成功添加到产品中,而无需在其上循环并重新加载它们。因此,它解决了@Raphael谈到的性能下降问题。
这是插件XML定义(在您的di.xml文件中):
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
<plugin name="your_plugin_unique_nane" type="Your\Plugin\Namespace\Plugin" />
</type>
</config>
和插件代码:
namespace Your\Plugin\Namespace;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as CoreCollectionFactory;
class Plugin
{
/**
* @param CoreCollectionFactory $subject
* @param Collection $collection
* @return Collection
*/
public function afterCreate(CoreCollectionFactory $subject, Collection $collection)
{
$collection->addUrlRewrite();
return $collection;
}
}