首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用规则来增加每个产品的自定义运输成本?

如何使用规则来增加每个产品的自定义运输成本?
EN

Drupal用户
提问于 2018-02-11 09:07:58
回答 2查看 474关注 0票数 1

我在Drupal 7站点上使用Drupal商业,使用产品显示和产品。

每个产品都有一个运输成本字段。

在结帐时,需要收集和显示订单中所有产品的运输成本。

我的方法是:我使用商业统一费率模块来创建统一费率传送方法。然后,我创建了一个统一费率的航运服务“自定义航运成本”。然后,我创建了一个新的运输规则,“应用自定义运输成本”,其中有一个循环,需要为订单中的每个产品获取字段自定义运输成本的字段值。

问:规则应该是什么样的呢?它获取连接到行项的产品的字段值,并将其添加到运输成本中。

以下规则(是嵌套的)不起作用。我发现使用了规则日志,在第三条规则中,实体拥有字段没有被传递。我试图得到的产品字段价值,需要添加到总运费率。

代码语言:javascript
复制
0.528 ms Evaluating the action component_rules_get_shipping_price_field_value. [edit]
" Evaluating rule get local shipping price field value. [edit]
0 ms Evaluating rule get local shipping price field value.
0.049 ms Evaluating conditions of rule get local shipping price field value. [edit]
0.11 ms The condition entity_has_field evaluated to FALSE [edit]
0.113 ms AND evaluated to FALSE.
0.123 ms Finished evaluation of rule get local shipping price field value.

================================================

代码语言:javascript
复制
{ "rules_apply_custom_product_shipping_costs" : {
    "LABEL" : "Apply custom product shipping costs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_shipping" ],
    "ON" : { "commerce_shipping_calculate_rate" : [] },
    "DO" : [
      { "drupal_message" : { "message" : "Calculating shipping rates." } },
      { "variable_add" : {
          "USING" : {
            "type" : "commerce_price",
            "value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
          },
          "PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "Custom shipping cost" } }
        }
      },
      { "component_rules_get_local_shipping_rate" : {
          "price" : [ "custom-shipping-cost" ],
          "line_item" : [ "commerce-line-item" ]
        }
      },
      { "component_rules_get_international_shipping_rate" : {
          "price" : [ "custom-shipping-cost" ],
          "line_item" : [ "commerce-line-item" ]
        }
      }
    ]
  }
}

==================

代码语言:javascript
复制
{ "rules_get_local_shipping_rate" : {
    "LABEL" : "Set local shipping rate",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_line_item" ],
    "USES VARIABLES" : {
      "price" : { "label" : "price", "type" : "commerce_price" },
      "line_item" : { "label" : "line item", "type" : "commerce_line_item" }
    },
    "IF" : [
      { "data_is" : { "data" : [ "line-item:type" ], "value" : "shipping" } },
      { "data_is" : {
          "data" : [ "line-item:commerce-shipping-service" ],
          "value" : "local_shipping_rate"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Start loop: collect line item local shipping price field values" } },
      { "LOOP" : {
          "USING" : { "list" : [ "line-item:order:commerce-line-items" ] },
          "ITEM" : { "lineitem" : "lineitem" },
          "DO" : [
            { "variable_add" : {
                "USING" : {
                  "type" : "commerce_price",
                  "value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
                },
                "PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "custom shipping cost" } }
              }
            },
            { "component_rules_get_shipping_price_field_value" : {
                "custom_shipping_cost" : [ "custom_shipping_cost" ],
                "line_item" : [ "line_item" ]
              }
            },
            { "commerce_line_item_unit_price_add" : {
                "commerce_line_item" : [ "line-item" ],
                "amount" : [ "price:amount" ],
                "component_name" : "flat_rate_local_shipping_rate",
                "round_mode" : "1"
              }
            },
            { "drupal_message" : { "message" : "Single product shipping cost is" } },
            { "drupal_message" : { "message" : [ "custom-shipping-cost:amount" ] } }
          ]
        }
      }
    ]
  }
}

==============

代码语言:javascript
复制
{ "rules_get_shipping_price_field_value" : {
    "LABEL" : "get local shipping price field value",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "custom_shipping_cost" : { "label" : "custom shipping cost", "type" : "commerce_price" },
      "line_item" : { "label" : "line item", "type" : "commerce_line_item" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } },
      { "entity_has_field" : {
          "entity" : [ "line-item:commerce-product" ],
          "field" : "field_shipping_rate_local_countr"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "line item has field commerce_product" } },
      { "data_set" : {
          "data" : [ "custom-shipping-cost" ],
          "value" : [ "line-item:commerce-product:field-shipping-rate-local-countr" ]
        }
      }
    ]
  }
}

===========================编辑以下问题导致了我的问题:在整个规则设置中,我使用多个变量作为行项。我错误地在循环之外使用了lineitem变量,而不是循环中的lineitem变量。在某种程度上,我有行项和行项,并将它们混合在一起,这使得它无法工作。另外,产品需要作为变量添加到循环中。

EN

回答 2

Drupal用户

回答已采纳

发布于 2018-02-11 13:18:52

下面是回答这个问题的一个尝试(最重要的是?)你问题的一部分是:

以下规则(是嵌套的)不起作用。我发现使用了规则日志,在第三条规则中,实体拥有字段没有被传递。我试图得到的产品字段价值,需要添加到总运费率。

  • 在第3条规则中,添加另一个参数,以传递与行项相关的"Commerce Product“。说你把它命名为"product_related_to_item“。
  • 在您的第二条规则中,使用与产品相关的行项令牌传递额外的parm值(= product_related_to_item),以调用第3条规则(确保您的第2条规则中有)。
  • 在您的第三条规则中,将第二个“实体有字段”替换为类似的规则条件,但现在引用实体product_related_to_item的(相同)字段(不确定您是否还需要第一个规则条件,然后为您准备作业…)。

通过进行这些修正,典型的规则魔术(在添加这样的规则条件之后)再次应用。例如,您可以在后续的规则操作中使用它们的值。是的,我知道/意识到,这看起来可能是“这毫无意义”,或者“你基本上是在做同样的事情”。至少这是我几天前第一次想到的,当时我遇到了一个类似的问题。但是可以肯定的是,这就是解决方案(结果表明,在处理规则中的“引用实体(即行项中引用的产品)”时,这与某些限制有关。

票数 1
EN

Drupal用户

发布于 2018-02-13 13:05:37

由于Pierre Vriens的提示,下面的方法起了作用,尽管我的具体用例有点复杂,因为它在本地和国际运输中使用了两个产品字段。我还在这里和那里使用了额外的变量,为了使令牌在system messages..so中可用,并不是所有的变量都是实际需要的。

票数 1
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/255568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档