0

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

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

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

4

1 回答 1

0

我不完全确定我是否遵循此解释,但如果它是产品线项目,您应该能够访问产品 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 函数。

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

(再次未经测试)

{ "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" : [] }
    ]
  }
}
于 2014-07-17T15:04:05.037 回答