简单地说,我只想在结账时获得适用于产品的目录价格规则。我知道Magento 1有很多解决方案,例如这个博客https://jutesenthil.wordpress.com/2015/09/28/get-catalog-rule-by-product-id-in-magento/但试图在《马根托2》中得到同样的结果似乎并不奏效。我的代码片段如下。
/**
* @param $productId
* @param $customerGroupId
* @return mixed
*/
public function getCatalogPriceRuleFromProduct($productId, $customerGroupId)
{
/**
* @var MagentoCatalogModelProductFactory
*/
$product = $this->_objectManager->create('MagentoCatalogModelProductFactory')->create()->load($productId);
$storeId = $product->getStoreId();
$store = $this->_store_manager->getStore($storeId);
$websiteId = $store->getWebsiteId();
/**
* @var MagentoFrameworkStdlibDateTimeDateTime
*/
$date = $this->_objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
$dateTs = $date->gmtDate();
/**
* @var MagentoCatalogRuleModelRule
*/
$resource = $this->_objectManager->create('MagentoCatalogRuleModelRule');
// $resource = $this->_objectManager->create('MagentoCatalogRuleModelRuleFactory');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
/*$rules = $resource->getCollection()
->addFieldToFilter('from_time', $dateTs)
->addFieldToFilter('to_time', $dateTs)
->addFieldToFilter('product_id', $productId)
->addFieldToFilter('store_id', $storeId)
->addFieldToFilter('website_id', $websiteId)
->addFieldToFilter('customer_group_id', $customerGroupId);*/
return $rules;
}
但总是返回null。
对此有什么帮助或想法吗??
对于任何需要此解决方案的人来说,这就是
/**
* @param $productId
* @param $customerGroupId
* @return mixed
*/
public function getCatalogPriceRuleFromProduct($productId, $customerGroupId)
{
/**
* @var MagentoCatalogModelProductFactory
*/
$product = $this->_objectManager->create('MagentoCatalogModelProductFactory')->create()->load($productId);
$storeId = $product->getStoreId();
$store = $this->_store_manager->getStore($storeId);
$websiteId = $store->getWebsiteId();
/**
* @var MagentoFrameworkStdlibDateTimeDateTime
*/
$date = $this->_objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
$dateTs = $date->gmtDate();
/**
* @var MagentoCatalogRuleModelResourceModelRule
*/
$resource = $this->_objectManager->create('MagentoCatalogRuleModelResourceModelRule');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
return $rules;
}
如果你需要得到实际的折扣金额,也可以使用这段代码。
/**
* @var MagentoCatalogRuleModelRuleFactory
*/
$rule = $this->_objectManager->create('MagentoCatalogRuleModelRuleFactory')->create();
$discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
感谢@Pallavi
用于在您的购物车中应用所有规则:
Class <your classname>
{
protected $_item;
public function __construct(
...
MagentoQuoteModelQuoteItem $item
...
) {
...
$this->_item = $item;
...
}
public function GetAppliedRulesDetails() {
$appliedIds = $this->_item->getAppliedRuleIds();
/* here you need to load the results ids and get required details */
}
}
您可以检查vendor/magento/module-sales-rule/Observer/SalesOrderAfterPlaceObserver.php
文件是否循环通过规则。
我在您的代码中看到的是,您正试图调用$resource->getRulesFromProduct((,而您的类是\Magento\CatalogRule\Model\Rule。请尝试调用\Magento\CatalogRule\Model\ResourceModel\Rule。这应该有效!