【发布时间】:2020-10-23 03:53:48
【问题描述】:
Woocommerce Gravity Forms Product Add-Ons 插件创建条目并将它们与订单连接起来,但您似乎无法从框中将订单 ID 和产品数量添加到 GravityForms 通知中。
也许这个答案会对某人有所帮助。
附:不喜欢之前写个评论说明为什么就好了?
【问题讨论】:
标签: php wordpress woocommerce gravity-forms-plugin
Woocommerce Gravity Forms Product Add-Ons 插件创建条目并将它们与订单连接起来,但您似乎无法从框中将订单 ID 和产品数量添加到 GravityForms 通知中。
也许这个答案会对某人有所帮助。
附:不喜欢之前写个评论说明为什么就好了?
【问题讨论】:
标签: php wordpress woocommerce gravity-forms-plugin
两个过滤器。我们将自定义标签添加到 GravityForms GUI 并对其进行处理。之后我们可以在此处添加它们 Admin ---> GravityForms ---> A form ---> Settings ---> Notification ---> Message field.
/**
* Add custom tags with Order ID & Order Item Quantity to the GravityForm notification.
*
* @link https://docs.gravityforms.com/gform_pre_replace_merge_tags/
* @see /plugins/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-entry.php
*/
function woo_gform_pre_replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
$order_number_tag = '{order_number}';
$order_item_quantity_tag = '{order_item_quantity}';
/*
* An WC Order contains some Items (products).
* One WC Order Item produces one GForm Entry.
* GForm performs email notification. We need Order ID & Product Amount into the notification.
*/
if ( isset( $entry['id'] ) && ! empty( $form ) ) {
$order_id = gform_get_meta( $entry['id'], 'woocommerce_order_number' );
$order_item_id = gform_get_meta( $entry['id'], 'woocommerce_order_item_number' );
$order_item_quantity = wc_get_order_item_meta( $order_item_id, '_qty', true );
// The {order_number} tag
if ( strpos( $text, $order_number_tag ) !== false ) {
$text = str_replace( $order_number_tag, $order_id, $text );
}
// The {order_item_quantity} tag
if ( strpos( $text, $order_item_quantity_tag ) !== false ) {
$text = str_replace( $order_item_quantity_tag, $order_item_quantity, $text );
}
}
return $text;
}
add_filter( 'gform_pre_replace_merge_tags', 'woo_gform_pre_replace_merge_tags', 10, 7 );
/**
* Add custom tags in the GravityForm UI.
*
* @see woo_gform_pre_replace_merge_tags()
*/
function woo_add_custom_merge_tags( $merge_tags, $form_id, $fields, $element_id ) {
$merge_tags[] = [
'label' => esc_html__( 'Order Number', 'textdomain' ),
'tag' => '{order_number}'
];
$merge_tags[] = [
'label' => esc_html__( 'Order Item Quantity', 'textdomain' ),
'tag' => '{order_item_quantity}'
];
return $merge_tags;
}
add_filter( 'gform_custom_merge_tags', 'woo_add_custom_merge_tags', 10, 4 );
【讨论】: