【问题标题】:Link between Gravity Forms and Woocommerce APIsGravity Forms 和 Woocommerce API 之间的链接
【发布时间】:2014-12-08 06:44:32
【问题描述】:

我有一个 Woocommerce 网站,我使用 Gravity Forms 进一步扩展每个订单。

我正在编写一个管理工具,它使用这两个 API 来制作一些统计信息和其他管理工具。

我可以获得重力表格条目列表,以及订单列表。我遇到的问题是我不知道如何获取与特定订单相关的条目。

有没有办法做到这一点?

【问题讨论】:

标签: php wordpress woocommerce gravity-forms-plugin


【解决方案1】:

您是否尝试过使用 woocomerce 历史插件或从项目中提取原始元数据¿?

wc_get_order_item_meta($order_item_id, "_gravity_forms_history"); wc_get_order_item_meta($order_item_id, "_gravity_form_data");

请记住,这将需要创建一个新的端点,而不是直接使用。

【讨论】:

    【解决方案2】:

    我上次使用 WooCommerce Gravity Forms 产品插件时(大约一年前)它没有将订单 ID 存储在条目中(必须在创建条目和创建订单之后发生),或订单中的条目 ID。后者可能更有意义,但两者都需要自定义代码。

    再说一次,我已经有一段时间没有使用该插件了。我会 ping WC 支持,看看他们是否有任何关于实施支持的提示。

    【讨论】:

      【解决方案3】:

      我在这里找到了 WooCommerce 和重力形式产品插件之间的链接:

      在数据库中,找到 your_table_prefix_posts 表中的订单,并获取其 ID。我正在过滤 post_type “shop_order”。

      your_table_prefix_woocommerce_order_items 表中,找到刚刚找到的 ID,并在“order_item_type”列中过滤“line_item”,并获取“order_item_id”。

      使用该“order_item_id”在表格your_table_prefix_woocommerce_order_itemmeta 中查找订单的元数据。

      订单的所有重力形式数据都在其中。看起来 woo 所做的事情和重力之间并没有实际的联系,只是表格被填写并且数据被抓取并粘贴到 your_table_prefix_woocommerce_order_itemmeta。我找不到任何将特定订单与特定 gf 条目联系起来的东西,但您可以从 Woo 获取数据,并至少使用它来搜索 GF 条目。

      【讨论】:

        【解决方案4】:

        我能够通过三步流程使用 Gravity Forms、Gravity Forms Product Addons 和 Woocommerce 做到这一点:

        第 1 步:在创建条目时获取 GF 条目 ID/s,并将其存储在会话中。这发生在结帐完成之前,有时在用户登录之前。

        add_action( 'gform_after_submission', 'blb_get_lead_entry_id', 10, 2 );
        function blb_get_lead_entry_id( $entry, $form ) {
        
            $meta_value = $entry['id'];
        
            //session array with all the entries because GF creates 2 entries each time for validation purposes apparently
            if (!isset($_SESSION['entryclump'])) {
            $_SESSION['entryclump'] = array();
            }
            $_SESSION['entryclump'][]  = $meta_value;
        
            //also an array with just the current entry which will end up be the later of the two entries added by GF
            $_SESSION[ 'gf-entry-id' ] = $meta_value;
        }
        

        第 2 步:将您刚刚收集的条目 ($_SESSION[ 'gf-entry-id' ]) 包含在购物车项目元数据中,然后保存。

        在这种情况下,我保存了一个名为“_gf_entry_ID”的字段,它将以正确的 order_item_id 和两个 GF 条目中的后者作为元值添加到 woocommerce_order_itemmeta 表中。

        //add cart item data
        add_filter( 'woocommerce_add_cart_item_data', 'blb_add_gfentry_to_cart_data', 10, 3 );
        function blb_add_gfentry_to_cart_data( $cartItemData, $productId, $variationId ) {
            $entryid=$_SESSION[ 'gf-entry-id' ];
            $cartItemData['GFentryID'] = $entryid;
            return $cartItemData;
            unset($_SESSION[ 'gf-entry-id' ]);
        }
        
        
        //add cart item data: session stuff
        add_filter( 'woocommerce_get_cart_item_from_session', 'blb_cart_item_session', 10, 3 );
        function blb_cart_item_session( $cartItemData, $cartItemSessionData, $cartItemKey ) {
            if ( isset( $cartItemSessionData['GFentryID'] ) ) {
                $cartItemData['GFentryID'] = $cartItemSessionData['GFentryID'];
            }
        
            return $cartItemData;
        }
        
        //save the data
        add_action( 'woocommerce_add_order_item_meta', 'blb_save_gfentry', 10, 3 );
        function blb_save_gfentry( $itemId, $values, $key ) {
            if ( isset( $values['GFentryID'] ) ) {
            wc_add_order_item_meta( $itemId, '_gf_entry_ID', $values['GFentryID'] );
            }
        }
        

        第 3 步(可选):更新 GF 表单以反映正确的 created_by 用户 ID。现在结帐已完成并且用户已登录,我们可以这样做了。

        function blb_woocommerce_thankyou( $order_id ) { 
            //Current User
            $currentUserID = wp_get_current_user()->ID;
        
            //GF Entry Array for Order
            $order = new WC_Order( $order_id );
            $items = $order->get_items();
            $order_item_ids = array();
            $gf_entry_ids = array();
            foreach ( $items as $key=>$item ) {
              $gf_entry_ids[] = $item['item_meta']['_gf_entry_ID'][0];   
            }
        
            //First real quick clear all the entries in the entry clump (in case the user was already logged in)
            //This is important because GF creates two forms for product add ons with Woocommerce and we only want one to show up in the list and edit plugin
            $entryclump = $_SESSION[ 'entryclump' ];
            foreach ( $entryclump as $entry ) {
            global $wpdb;
            $wpdb->update('wp_rg_lead', array('created_by' => null), array('id' => $entry));
            }
        
        
            //Update wp_rg_lead
            if (($currentUserID!=0) && (isset($_SESSION[ 'entryclump' ])) ) {
                foreach ( $gf_entry_ids as $gf_entry_id ) {
                global $wpdb;
                $wpdb->update('wp_rg_lead', array('created_by' => $currentUserID), array('id' => $gf_entry_id));
                } //foreach
            } //if
        
            unset($_SESSION[ 'entryclump' ]);
        
        
        }; 
        add_action( 'woocommerce_thankyou', 'blb_woocommerce_thankyou', 10, 1 ); 
        

        【讨论】:

          猜你喜欢
          • 2016-12-19
          • 2018-12-20
          • 1970-01-01
          • 2015-02-10
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 2014-12-06
          相关资源
          最近更新 更多