/
gravity-cart-capture.php
136 lines (109 loc) · 4.55 KB
/
gravity-cart-capture.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* Plugin Name: Gravity Forms: WooCommerce Cart Capture
* Plugin URI: https://github.com/lucasstark/gravityforms-woocommerce-cart-capture
* Description: Automatically capture the contents of a users cart when they submit a gravity form. To use Set gravity form to pre-populate a field. Set the key as woocommerce_cart. The cart contents at the time of submission will automatically be captured.
* Author: Lucas Stark
* Author URI: http://www.github.com/lucasstark/
* License: GPLv3
*/
class GFWC_Cart_Capture {
private static $instance;
public static function register() {
if ( self::$instance == null ) {
self::$instance = new GFWC_Cart_Capture();
}
}
private function __construct() {
add_filter( 'gform_field_value_woocommerce_cart', array($this, 'record_cart_item_meta') );
add_filter( 'gform_entry_field_value', array($this, 'display_cart_item_meta'), 10, 4 );
}
public function record_cart_item_meta( $value ) {
$items = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$items[$cart_item_key] = array(
'title' => apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ),
'quantity' => $cart_item['quantity'],
'meta' => $this->get_cart_item_data($cart_item)
);
}
}
$value = base64_encode( serialize( $items ) );
return $value;
}
public function display_cart_item_meta( $value, $field, $lead, $form ) {
if ( isset( $field['inputName'] ) && $field['inputName'] == 'woocommerce_cart' ) {
$output = '<ul>';
$cart_items = unserialize( base64_decode( $value ) );
if ( $cart_items && is_array( $cart_items ) ) :
foreach ( $cart_items as $cart_item ) :
$output .= '<li style="margin-bottom:10px;">';
$output .= '<strong>' . $cart_item['title'] . '</strong>: ( ' . $cart_item['quantity'] . ' )';
if ( isset( $cart_item['meta'] ) ) :
$output .= '<ul style="margin-left:15px;margin-top:5px;">';
foreach ( $cart_item['meta'] as $cart_item_meta ) :
$output .= '<li><strong>' . $cart_item_meta['key'] . '</strong>: ' . $cart_item_meta['value'] . '</li>';
endforeach;
$output .= '</ul>';
endif;
$output .= '</li>';
$output .= '<li><hr /></li>';
endforeach;
endif;
$output .= '</ul>';
return $output;
} else {
return $value;
}
}
private function get_cart_item_data( $cart_item ) {
$item_data = array();
// Variation data
if ( !empty( $cart_item['data']->variation_id ) && is_array( $cart_item['variation'] ) ) {
foreach ( $cart_item['variation'] as $name => $value ) {
if ( '' === $value ) {
continue;
}
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
// If this is a term slug, get the term's nice name
if ( taxonomy_exists( $taxonomy ) ) {
$term = get_term_by( 'slug', $value, $taxonomy );
if ( !is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
// If this is a custom option slug, get the options name
} else {
$value = apply_filters( 'woocommerce_variation_option_name', $value );
$product_attributes = $cart_item['data']->get_attributes();
if ( isset( $product_attributes[str_replace( 'attribute_', '', $name )] ) ) {
$label = wc_attribute_label( $product_attributes[str_replace( 'attribute_', '', $name )]['name'] );
} else {
$label = $name;
}
}
$item_data[] = array(
'key' => $label,
'value' => $value
);
}
}
// Filter item data to allow 3rd parties to add more to the array
$item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item );
// Format item data ready to display
foreach ( $item_data as $key => $data ) {
// Set hidden to true to not display meta on cart.
if ( !empty( $data['hidden'] ) ) {
unset( $item_data[$key] );
continue;
}
$item_data[$key]['key'] = !empty( $data['key'] ) ? $data['key'] : $data['name'];
$item_data[$key]['display'] = !empty( $data['display'] ) ? $data['display'] : $data['value'];
}
return $item_data;
}
}
GFWC_Cart_Capture::register();