购物车规则则相反 [英] Shopping cart Rules doing the opposite

查看:76
本文介绍了购物车规则则相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们添加了一个标记为是/否"的产品属性:是否允许该产品应用优惠券?"的默认值为是".

这样做的原因是,我们从不希望销售团队在数千种可用的核心产品中提供某些折扣.

我们可以将这些产品作为SKU添加到所创建的千张优惠券中的每张中,但是由于偶然或由于创建优惠券的销售团队未正确列出而可能会对其进行更改.

因此,我们已要求他们将此规则添加到他们的新优惠券中,以排除这些产品.我们还通过代码更新了所有其他优惠券,以包括此规则.

当我在Magento 1.5(社区版)上测试该规则时,它与条件所说的完全相反.

当我在其他企业解决方案(magento 1.9)上测试相同规则时,它实际上会执行预期的操作.

当我的购物车中有一个产品时,如果将此属性设置为否",并且我应用了优惠券代码,则该商品接受该优惠券代码.在另一个系统(企业)上,它会拒绝它.

还有其他人遇到过吗?

更新: 好的,这变得更加有趣了. 当我单步执行Rule类时,产品通过了,但是product属性没有通过.范围设置为网站,我确实检查了它是否保存在后端的该范围内.

这真的很奇怪...

在企业系统上,具有相同配置和数据的产品属性正在通过.

为了确保这一点,我什至尝试不使用带有另一个新属性的双重否定词. 违反以下规则,优惠券将被拒绝:优惠券代码无效." 这是正确的,因为产品设置为可以使用优惠券吗?=否".

但是问题是我不想针对所有其他产品进行检查,而是针对少数已标记为否"的产品进行检查.

无论如何这对我们都行不通,因为如果购物车中有任何商品的可以使用优惠券?"属性的值为否",我们就不希望应用优惠券. /p>

这是一个双重否定规则,无论您以哪种方式看待它.

当第二次添加产品时,我通过validate()函数逐步浏览了/app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php,并且数据显示在属性中,但是当我重新申请优惠券并再次执行此功能时,这些值都消失了.这很奇怪.

将新产品添加到购物车时:

重新申请优惠券代码时:

说明的内容在哪里出现,其他一些属性消失了?

更新说明我越来越近了. 当我从以下位置更改"/app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php"中的以下validate()函数时:

public function validate(Varien_Object $object)
{
    $product = false;
    if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
        $product = $object->getProduct();
    } else {
        $product = Mage::getModel('catalog/product')
            ->load($object->getProductId());
    }

    $product
        ->setQuoteItemQty($object->getQty())
        ->setQuoteItemPrice($object->getPrice())
        ->setQuoteItemRowTotal($object->getBaseRowTotal());

    return parent::validate($product);
}

为此:

public function validate(Varien_Object $object)
{
    $product = false;
    $product = Mage::getModel('catalog/product')
        ->load($object->getProductId());

    $product
        ->setQuoteItemQty($object->getQty())
        ->setQuoteItemPrice($object->getPrice())
        ->setQuoteItemRowTotal($object->getBaseRowTotal());

    return parent::validate($product);
}

然后工作正常!

解决方案

找出了实际的问题,这就是我为解决问题所做的事情:

以下位获取购物车价格规则的产品数据:

        if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
            $product = $object->getProduct();
        } else {
            $product = Mage::getModel('catalog/product')
                ->load($object->getProductId());
        }

罪魁祸首是这一行:$ product = $ object-> getProduct();

从"Quote/Item/Abstract.php"中调用一行是:$ product = $ this-> _ getData('product');

它将简单地从系统中获取缓存的数据,并且包含如下所示的选择:

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->setStoreId($this->getStoreId())
    ->addIdFilter($this->_productIds)
    ->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
    ->addOptionsToResult()
    ->addStoreFilter()
    ->addUrlRewrite()
    ->addTierPriceData();

问题在于,以下行:

->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())

这将仅获取在Config.xml中为报价项定义的属性:

    <quote>
        <item>
            <product_attributes>
                <sku/>
                <type_id/>
                <name/>
                <status/>
                <visibility/>
                <price/>
                <weight/>
                <url_path/>
                <url_key/>
                <thumbnail/>
                <small_image/>
                <tax_class_id/>
                <special_from_date/>
                <special_to_date/>
                <special_price/>
                <cost/>
                <is_recurring/><!-- for totals calculation, placing and processing order -->
                <recurring_profile/><!-- for placing order -->
                <gift_message_available/>
            </product_attributes>
        </item>
    </quote>

这将不会获得我想要的新的自定义属性,并且该规则将永远无法正确地将数据与期望值进行比较.

将以下xml添加到我的自定义销售模块config.xml中,该模块中包含一些其他内容的观察者,从而解决了该问题.

<sales>
    <quote>
        <item>
            <product_attributes>
                <exclude_from_coupon/>
            </product_attributes>
        </item>
    </quote>                
</sales>

然后一切正常!

为什么它在企业版的magento中起作用

真的不知道.我认为Enterprise可能正在寻找有关实际Attribute定义的设置,并将其包含在要收集的Attributes列表中,然后为它返回数据.我将对其进行研究,并将其更新到此处.

至少我现在知道真正的问题是什么,以及为什么购物车规则条件(优惠券)失败了.

感谢所有为我提供一些好的问题和建议的人.

另一天更明智的选择是magento ...

We have added a "yes/no" product attribute labelled: "Allow this product to have coupons applied?" with a default value of "yes".

The reason for this is, that we never want the sales team to give discounts on certain core products out of the thousands available.

We could add these products as SKU's to every one of the thousand coupons created, but that can then be changed by accident, or improperly listed by the sales team creating the coupons.

We therefore have asked them to add this rule to their new coupons, to exclude these products. We also updated all the other coupons via code, to include this rule.

When I test the rule on Magento 1.5, Community edition, it does exactly the opposite of what the condition says.

When I test the same rule on our other Enterprise solution (magento 1.9), it actually does what it is supposed to.

When I have one product in the shopping cart, being a product that has this attribute set to "No", and I apply a coupon code, it accepts the coupon code. On the other system (Enterprise) it rejects it, as it should.

Has anyone else came across this?

UPDATE: Ok, this just got more interesting. When I stepped through the Rule classes, the product was passed, but the product attribute did not come through. The scope is set to website, and I did check that is it saved under that scope in the back end.

This is really weird...

On the enterprise system, the product attribute, with the same configuration and data is coming through.

I even tried to not have double negatives, with another new attribute, to make sure. Running against the following rule, the coupon gets refused: "Coupon code is not valid." This is correct, as the product is set as "Can use coupon? = No".

But the problem is that I do not want to do a check against all the other products, but rather against, the few that has been marked as "No".

This is not going to work for us anyway, as we don't want to apply a coupon if there is any product in the cart that has a value of "No" for the attribute "Can use coupon?".

This is a double negative rule, no matter which way you look at it.

I stepped through /app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php through the validate() function, when adding the product a second time, and the data shows up in the attributes, but when I go and re-apply the coupon, and step through this function again, those values are gone. this is weird.

When adding new product to shopping cart:

When Re-applying Coupon Code:

Where the heck did description and some of the other attributes disappear to?

UPDATE NOTE I am getting a bit closer. When I change the following validate() function in "/app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php" from the following:

public function validate(Varien_Object $object)
{
    $product = false;
    if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
        $product = $object->getProduct();
    } else {
        $product = Mage::getModel('catalog/product')
            ->load($object->getProductId());
    }

    $product
        ->setQuoteItemQty($object->getQty())
        ->setQuoteItemPrice($object->getPrice())
        ->setQuoteItemRowTotal($object->getBaseRowTotal());

    return parent::validate($product);
}

To This:

public function validate(Varien_Object $object)
{
    $product = false;
    $product = Mage::getModel('catalog/product')
        ->load($object->getProductId());

    $product
        ->setQuoteItemQty($object->getQty())
        ->setQuoteItemPrice($object->getPrice())
        ->setQuoteItemRowTotal($object->getBaseRowTotal());

    return parent::validate($product);
}

It then works fine!

解决方案

Found out the actual problem, and here is what I did for the solution:

The following bit gets the product data for the Shopping Cart Price Rules:

        if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
            $product = $object->getProduct();
        } else {
            $product = Mage::getModel('catalog/product')
                ->load($object->getProductId());
        }

The culprit is this line: $product = $object->getProduct();

That calls a line from the "Quote/Item/Abstract.php" being: $product = $this->_getData('product');

It will simply get the cached data from the system, and that consists of a selection like this:

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->setStoreId($this->getStoreId())
    ->addIdFilter($this->_productIds)
    ->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
    ->addOptionsToResult()
    ->addStoreFilter()
    ->addUrlRewrite()
    ->addTierPriceData();

The problem with this is, the following line:

->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())

That will get only the attributes defined in the Config.xml for quote items being:

    <quote>
        <item>
            <product_attributes>
                <sku/>
                <type_id/>
                <name/>
                <status/>
                <visibility/>
                <price/>
                <weight/>
                <url_path/>
                <url_key/>
                <thumbnail/>
                <small_image/>
                <tax_class_id/>
                <special_from_date/>
                <special_to_date/>
                <special_price/>
                <cost/>
                <is_recurring/><!-- for totals calculation, placing and processing order -->
                <recurring_profile/><!-- for placing order -->
                <gift_message_available/>
            </product_attributes>
        </item>
    </quote>

This will not get the new custom attribute that I want, and the rule will never be able to compare the data with the expected value properly.

Adding the following xml to my custom sales module config.xml, which have some observers in it for other stuff, fixes the problem.

<sales>
    <quote>
        <item>
            <product_attributes>
                <exclude_from_coupon/>
            </product_attributes>
        </item>
    </quote>                
</sales>

Then it all works!

Why did it work in the enterprise version of magento

Don't really know. I think Enterprise is maybe looking for settings on the actual Attribute definition, and includes it to the list of Attributes to collect, and then return the data for it, my guess. I will research that, and update it here to the post.

At least I now know what the real issue is, and why the Shopping Cart Rule Condition (coupon) failed.

Thanks to all those who helped me with some good questions and suggestions.

Another day wiser with magento...

这篇关于购物车规则则相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆