【问题标题】:Drupal commerce - validating coupon against product programaticallyDrupal commerce - 以编程方式验证针对产品的优惠券
【发布时间】:2014-07-17 13:21:27
【问题描述】:

我的所有产品都与一些“杂志”内容类型对象相关——即每个产品都有对杂志对象的节点引用。此外,我在优惠券中添加了相同的节点引用字段,再次引用“杂志”内容类型的节点。我想要实现的是拥有仅适用于某些杂志的优惠券。也就是说,如果优惠券杂志与产品杂志优惠券相匹配,则有效。其他方式不是。我不能用规则来做到这一点,因为我无法以任何方式接近该产品的杂志领域。我只能看到订单项,我无法进一步了解产品。我希望我可以从代码中做到这一点。有没有办法以编程方式设置某些优惠券是否有效。

我只想查看所有订单项并检查其中一些是否具有与优惠券相同的杂志集。

我还想知道将优惠券与单个产品/订单项相关联是否有意义?

【问题讨论】:

    标签: drupal drupal-7 coupon commerce


    【解决方案1】:

    我不完全确定我是否遵循此解释,但如果它是产品线项目,您应该能够访问产品 ID,加载产品并检查有效杂志。

    或者,您可以为允许的杂志在优惠券类型中添加一个字段,并为杂志节点/产品添加一个实体参考字段。然后通过代码或规则,对照该行项目的产品检查该特定优惠券的可接受杂志列表。通过您自己的验证或计算方法调用此代码。 IE。您可以在代码中创建自定义优惠券,也可以添加规则来验证“杂志优惠券”类型的优惠券,并在规则中调用一些自定义 php 代码。

    // ** note: this is totally untested/rough code **
    // Just to give you an idea of how it *could* work 
    
    // assuming we have a $coupon and a $line_item at this point 
    
    // some basic set up
    $coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    
    $product_id = $line_item_wrapper->product_id->raw();
    $product = commerce_product_load($product_id);
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
    
    $magazine_id = $product_wrapper->magazine->nid->raw();
    $eligible_magazines = $coupon_wrapper->coupon_magazines->value();
    
    // for each eligible product on the list from the coupon
    for($i = 0; $i < count($eligible_products); $i++) {   
    
        // compare our product's magazine value to the eligible magazine id
        if($eligible_magazines[$i]->nid  == $magazine_id) {
            // do stuff here, whether return true for the validation etc
        }
    }
    

    或者,如果您只有订单,您可以执行非常类似的操作并循环浏览订单上的订单项。我相信该订单只有 ID,因此您必须使用 commerce_line_item_load 函数。

    在 cmets 中回答您的问题 - 是的,您可以添加一个钩子来在模块中执行此代码,也可以创建一个规则来执行此操作。 – 您可以为优惠券添加验证规则,例如

    (再次未经测试)

    { "rules_coupon_check_magazine" : {
        "LABEL" : "Coupon: Check Magazine",
        "PLUGIN" : "reaction rule",
        "REQUIRES" : [ "rules", "php", "commerce_coupon" ],
        "ON" : [ "commerce_coupon_validate" ],
        "IF" : [
          {"entity_has_field" : {
          "entity" : [ "commerce-order" ],
          "field" : "commerce_coupon_order_reference"
        }
      },
      { "NOT data_is_empty" : { "data" : [ "commerce-order:commerce-coupon-order-reference" ] } },
          { "php_eval" : { "code" : "\/\/ my validation code here - either return true or false\r\nreturn true;" } }
        ],
        "DO" : [
          { "drupal_message" : {
              "message" : "Sorry, you cannot apply this coupon to the order",
              "type" : "error"
            }
          },
          { "commerce_coupon_action_is_invalid_coupon" : [] }
        ]
      }
    }
    

    【讨论】:

    • 感谢金佰利的代码和建议。但问题是我不知道该把代码放在哪里?是否有一些钩子功能或什么?当我发现优惠券无效时,我该怎么办?我如何才能仅针对该订单使其无效。
    • 通过遵循规则逻辑,我希望应该有一些优惠券挂钩操作,您可以在其中获取优惠券和订单并对优惠券参考做一些事情......或返回真假......或其他东西。我想我什至找到了那个功能,但它不是钩子。我需要一个可以输入代码的钩子函数 - 不想更改模块代码。
    • api.drupalcommerce.org/api/Drupal%20Commerce/… 钩子函数在哪里?是不是我看错地方了?
    • 我猜应该挂钩这个函数:drupalcontrib.org/api/drupal/…
    • 是的,您可以添加一个钩子来在模块中执行此代码,也可以创建一个规则来执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    相关资源
    最近更新 更多