如何在Magento2中以编程方式获取目录价格规则

时间:2018-07-12 11:06:56

标签: magento2 rules price catalog discount

简而言之,我只想获取在结帐时应用于产品的目录价格规则。我知道从M​​agento 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。

有什么帮助或想法吗?

2 个答案:

答案 0 :(得分:2)

要在购物车中应用所有规则:

Class <your classname>
{
protected $_item;

public function __construct(
    ...
    \Magento\Quote\Model\Quote\Item $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。应该可以!

答案 1 :(得分:0)

对于需要此解决方案的任何人,就是这样

/**
 * @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