简而言之,我只想在结账时将目录价格规则应用于产品。我知道 Magento 1 的一些来源提供了很多解决方案,一个例子是这个博客https://jutesenthil.wordpress.com/2015/09/28/get-catalog-rule-by-product-id-in- magento/但试图在 Magento 2 中获得相同的结果似乎不起作用。我的代码片段如下。
/**
* @param $productId
* @param $customerGroupId
* @return mixed
*/
public function getCatalogPriceRuleFromProduct($productId, $customerGroupId)
{
/**
* @var \Magento\Catalog\Model\ProductFactory
*/
$product = $this->_objectManager->create('\Magento\Catalog\Model\ProductFactory')->create()->load($productId);
$storeId = $product->getStoreId();
$store = $this->_store_manager->getStore($storeId);
$websiteId = $store->getWebsiteId();
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
$date = $this->_objectManager->create('\Magento\Framework\Stdlib\DateTime\DateTime');
$dateTs = $date->gmtDate();
/**
* @var \Magento\CatalogRule\Model\Rule
*/
$resource = $this->_objectManager->create('\Magento\CatalogRule\Model\Rule');
// $resource = $this->_objectManager->create('\Magento\CatalogRule\Model\RuleFactory');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
/*$rules = $resource->getCollection()
->addFieldToFilter('from_time', $dateTs)
->addFieldToFilter('to_time', $dateTs)
->addFieldToFilter('product_id', …Run Code Online (Sandbox Code Playgroud)