Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Magento2中以编程方式获取目录价格规则_Magento2_Rules_Price_Catalog_Discount - Fatal编程技术网

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

如何在Magento2中以编程方式获取目录价格规则,magento2,rules,price,catalog,discount,Magento2,Rules,Price,Catalog,Discount,我只是想在结账时将目录价格规则应用于产品。我知道Magento 1的很多解决方案都来自于某些来源,例如本博客,但在Magento 2中尝试获得相同的结果似乎不起作用。下面是我的代码片段 /** * @param $productId * @param $customerGroupId * @return mixed */ public function getCatalogPriceRuleFromProduct($productId, $customerGroupId) { /

我只是想在结账时将目录价格规则应用于产品。我知道Magento 1的很多解决方案都来自于某些来源,例如本博客,但在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


关于如何在购物车中应用所有规则,有什么帮助或想法吗

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

}
类
{
受保护的美元项目;
公共函数构造(
...
\Magento\Quote\Model\Quote\Item$Item
...
) {
...
$this->_item=$item;
...
}
公共函数GetAppliedRulesDetails(){
$appliedIds=$this->_item->getAppliedRuleIds();
/*在这里,您需要加载结果ID并获得所需的详细信息*/
}
}
您可以检查
vendor/magento/module sales rule/Observer/saleorderafterplaceobserver.php
文件中的规则循环


我在代码中看到的是,您试图调用$resource->getRulesFromProduct(),而您的类是\Magento\CatalogRule\Model\Rule。请尝试调用\Magento\CatalogRule\Model\ResourceModel\Rule。这应该管用

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

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

}
类
{
受保护的美元项目;
公共函数构造(
...
\Magento\Quote\Model\Quote\Item$Item
...
) {
...
$this->_item=$item;
...
}
公共函数GetAppliedRulesDetails(){
$appliedIds=$this->_item->getAppliedRuleIds();
/*在这里,您需要加载结果ID并获得所需的详细信息*/
}
}
您可以检查
vendor/magento/module sales rule/Observer/saleorderafterplaceobserver.php
文件中的规则循环


我在代码中看到的是,您试图调用$resource->getRulesFromProduct(),而您的类是\Magento\CatalogRule\Model\Rule。请尝试调用\Magento\CatalogRule\Model\ResourceModel\Rule。这应该管用

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

/**
 * @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());

这一切都要感谢

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

/**
 * @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());

多亏了

尝试回显select查询并检查那里出了什么问题!尝试回显select查询并检查哪里出了问题!谢谢Pallavi,但我更感兴趣的是目录价格规则,而不是销售价格规则。@afro Nija,Okey,Apologize因为误解了我认为您需要在购物车中使用所有应用的规则ID。然而,我在代码中看到的是,您试图调用$resource->getRulesFromProduct(),而您的类是\Magento\CatalogRule\Model\Rule。应该是\Magento\CatalogRule\Model\ResourceModel\Rule吗?哇,帕拉维,你答对了。我想把这个建议标记为答案,你能把它作为答案来标记吗。@afro Nija,更新了我的答案!很高兴它有帮助:)谢谢Pallavi,但我更感兴趣的是目录价格规则,而不是销售价格规则。@afro Nija,Okey,aplogize因为误解我认为您需要在购物车中应用所有规则ID。然而,我在代码中看到的是,您试图调用$resource->getRulesFromProduct(),而您的类是\Magento\CatalogRule\Model\Rule。应该是\Magento\CatalogRule\Model\ResourceModel\Rule吗?哇,帕拉维,你答对了。我想把这个建议标记为答案,你能把它作为答案来标记吗。@afro Nija,更新了我的答案!很高兴它有所帮助:)