magento2 - 如何在 Magento2 中以编程方式获取目录价格规则
问题描述
只是我只想在结帐时将目录价格规则应用于产品。我知道 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', $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 \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\ResourceModel\Rule
*/
$resource = $this->_objectManager->create('\Magento\CatalogRule\Model\ResourceModel\Rule');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
return $rules;
}
如果您需要获得实际的折扣金额,也可以使用这段代码。
/**
* @var \Magento\CatalogRule\Model\RuleFactory
*/
$rule = $this->_objectManager->create('\Magento\CatalogRule\Model\RuleFactory')->create();
$discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
感谢@Pallavi
推荐阅读
- react-native - 状态更改时 React-Native-Component 不呈现
- java - 使用 Runtime.getRuntime().exec 执行 cmd 时如何隐藏 cmd 窗口
- php - 在 CENTOS 下运行 CRON JOBS
- angular - Angular - 多个订阅序列不起作用
- flutter - Flutter Navigation Drawer ExpansionTile 在下一个选项被选中时不会折叠以前打开的选项
- .net - 使用 C# 提取 7z SFX 文件
- python - 存储在 Python 方法中定义的数据帧和变量
- sapui5 - 调整页面大小时重命名列标题
- r - How to create multiple dataframes with lapply()?
- javascript - 如何从外部文件访问反应组件