使用旋转值

时间:2017-05-05 05:27:54

标签: wordpress forms rotation gravity auto-populate

我使用重力表格将销售查询发送到CRM。

我们有3名销售人员,每人都需要轮流接收查询。

例如:

  • 第1条 - 汤姆
  • 第2条 - 理查德
  • 第3条 - 哈利
  • 第4条 - 汤姆
  • 第5条 - 理查德
  • 第6条 - 哈利

每次在联系表单上有新条目时,我都需要在轮换中包含一个隐藏字段,其中包含下一个销售人员的姓名,以传递给我们的CRM。

我有一些PHP经验,但不熟悉Gravity。希望有人能指出我找到解决方案的正确方向。

1 个答案:

答案 0 :(得分:1)

以下是如何完成此操作的示例:

/**
 * Gravity Wiz // Gravity Forms // Rotating Values
 * http://gravitywiz.com/
 */
add_filter( 'gform_entry_post_save', function( $entry ) {

    // Specify an array of values that will be rotated through.
    $rotation = array( 'Tom', 'Richard', 'Harry' );

    // Specify the form ID for which this functioanlity applies.
    $form_id = 1722;

    // Specify the field ID in which the current rotation value will be saved.
    $field_id = 1;

    /* DON'T EDIT BELOW THIS LINE */

    // Bail out of this is not our designated form.
    if( $entry['form_id'] != $form_id ) {
        return $entry;
    }

    // Get the last submitted entry.
    $last_entry = rgar( GFAPI::get_entries( $entry['form_id'], array( 'status' => 'active' ), array( 'direction' => 'desc' ), array( 'offset' => 1, 'page_size' => 1 ) ), 0 );

    // Get the value submitted for our designated field in the last entry.
    $last_value = rgar( $last_entry, $field_id );

    // Determine the next index at which to fetch our value.
    $next_index = empty( $last_value ) ? 0 : array_search( $last_value, $rotation ) + 1;
    if( $next_index > count( $rotation ) - 1 ) {
        $next_index = 0;
    }

    // Get the next value based on our rotation.
    $next_value = $rotation[ $next_index ];

    // Update the value of our designated field in the database.
    GFAPI::update_entry_field( $entry['id'], $field_id, $next_value );

    // Update the value of our designated field in the $entry object that will be used to continuing processing the current submission.
    $entry[ $field_id ] = $next_value;

    return $entry;
} );

也可作为后人的要点:https://gist.github.com/spivurno/8908a589aa6ceaf6ff0a874324bfbf93