【问题标题】:How to get the catalog price rule programmatically in Magento2如何在 Magento2 中以编程方式获取目录价格规则
【发布时间】:2018-07-12 11:06:55
【问题描述】:

我只是想在结账时将目录价格规则应用于产品。我知道 Magento 1 的某些来源提供了很多解决方案,例如此博客 https://jutesenthil.wordpress.com/2015/09/28/get-catalog-rule-by-product-id-in-magento/,但尝试在 Magento 2 中获得相同的结果似乎不起作用。我的代码 sn-p 在下面。

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

有什么帮助或想法吗??

【问题讨论】:

  • 尝试回显选择查询并检查那里出了什么问题!

标签: magento2 rules price catalog discount


【解决方案1】:

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

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

【讨论】:

    【解决方案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 代替。这应该可行!

    【讨论】:

    • 感谢 Pallavi,但我对目录价格规则更感兴趣,而不是销售价格规则。
    • @afro-Nija,Okey,为误解表示歉意,我认为您需要购物车中所有已应用的规则 ID。但是我在您的代码中看到的是,您正在尝试调用 $resource->getRulesFromProduct() 并且您的类是 \Magento\CatalogRule\Model\Rule。不应该是\Magento\CatalogRule\Model\ResourceModel\Rule?
    • 哇,帕拉维,你做到了,我想把这个建议标记为答案,你能把它作为答案来标记它吗?
    • @afro-Nija,更新了我的答案!很高兴它有帮助:)
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多