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