从Woocommerce上的Gravity Forms获取输入字段

时间:2013-09-18 18:18:34

标签: php forms field woocommerce gravity

Woocommerce的Gravity Forms产品附加组件在购物车和结帐中显示用户的表单输入。我想在用户的帐户页面中显示这些表单字段及其订单。我已经尝试了一个星期,似乎无法将正确的PHP代码放在一起。我怎么能这样做?

我的代码就像这样:

$form_id = RGFormsModel::get_form_id('Domains');
$form = GFFormsModel::get_form_meta($form_id);
$field = GFFormsModel::get_field($form, 1);
$leads = RGFormsModel::get_leads($form['id']);

foreach($leads as $lead)
{
    foreach($field as $field_id)
    {
        $value = rgar($lead, (string) $field_id);
        echo $value;
    }
}

这将返回我想要的字段的所有条目,但是,我只想返回使用该特定产品提交的条目。帮助

这里有一个简单的例子,清楚我正在寻找什么。用户购买衬衫并从附在产品上的重力形式中选择大尺寸。在购物车和结帐页面上,它显示在产品标题“尺寸:大”下方。我想将相同的文字及其订单添加到“我的帐户”页面。

2 个答案:

答案 0 :(得分:0)

我解决类似问题的方式可能不是最好的编码实践,但它有效。它需要与Gravity Forms中的订单相关联的自定义数据,并将其显示在WooCommerce个别订单页面中的送货地址下:

<?php
add_action('woocommerce_admin_order_data_after_shipping_address', 'ips_show_signs_in_admin_under_shipping', 10, 1);
/*
function to put in functions.php of the theme to add custom gravityforms data to customer order page in admin area.
*/
function ips_show_signs_in_admin_under_shipping($order)
{
    global $woocommerce, $post;
    $order_items = $order->get_items(apply_filters('woocommerce_admin_order_item_types', array( 'line_item')));
    foreach($order_items as $item_id => $item)
    {
        if ($metadata = $order->has_meta($item_id)) //not a comparison, so one equal sign. so if there is any data in there it would assign it and thus making it true, if no data it would be false
        {
            foreach($metadata as $meta)
            {
                // Skip hidden woocommerce core fields just in case
                if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array(
                    '_qty',
                    '_tax_class',
                    '_product_id',
                    '_variation_id',
                    '_line_subtotal',
                    '_line_subtotal_tax',
                    '_line_total',
                    '_line_tax',
                    )))) continue;
                if (is_serialized($meta['meta_value'])) continue; // Skip serialised meta since we dont need it incase its in there
                $meta['meta_key'] = esc_attr($meta['meta_key']); //just to make sure there is no surprises in there
                $meta['meta_value'] = esc_textarea($meta['meta_value']); // using a textarea check for surprises (sql injection)
                if ("Signpost Address" === $meta['meta_key']) //here comes the custom data from gravity forms
                {
                   echo $meta['meta_value']; //this is what i was after from gravity forms collected with order
                }
            } //closes --- foreach($metadata as $meta)
        } //closes --- if ($metadata = $order->has_meta($item_id))
    } //closes --- foreach($order_items as $item_id => $item)
} //closes --- function
?>

编辑:此代码搜索订单元数据

答案 1 :(得分:0)

我已经在我的functions.php文件中添加了上面的代码,不幸的是,这对我没用。我已经全神贯注地寻找解决方案了,这似乎正是我所追求的。如果这是我的重力形式选项之一,我只是将“路标地址”替换为“Blanket Size”吗?