/var/www/html_de/wp-content/plugins/klaviyo/includes/wck-cart-rebuild.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
 * WooCommerceKlaviyo Cart Rebuild Functions
 *
 * Functions for rebuilding carts for Abandoned Checkout recovery.
 *
 * @package   WooCommerceKlaviyo/Functions
 * @version   2.0.0
 */

/**
 * Helper function to normalize normal product.
 *
 * @param array $item Cart item.
 * @return array Normalized cart item.
 */
function normalize_normal_product$item ) {
    return array(
        
'product_id'   => $item['product_id'],
        
'quantity'     => $item['quantity'],
        
'variation_id' => $item['variation_id'],
        
'variation'    => $item['variation'],
    );
}

/**
 * Helper function for composite products.
 *
 * @param array $container_ids container ids.
 * @param array $values values.
 * @return array Composite product.
 */
function add_encoded_composite$container_ids$values ) {
    
$composite_product = array();
    foreach ( 
$container_ids as $container_id => $container_values ) {
        
$components = array();
        if ( isset( 
$container_values['attributes'] ) ) {
            
$components = array(
                
'composite_id'       => $container_values['composite_id'],
                
'composite_quantity' => $values['quantity'],
                
'item'               => array(
                    
'product_id'   => $container_values['product_id'],
                    
'quantity'     => $container_values['quantity'],
                    
'container_id' => $container_id,
                    
'attributes'   => $container_values['attributes'],
                    
'variation_id' => isset( $container_values['variation_id'] ) ? $container_values['variation_id'] : null,
                ),
            );
        } else {
            
$components = array(
                
'composite_id'       => $container_values['composite_id'],
                
'composite_quantity' => $values['quantity'],
                
'item'               => array(
                    
'product_id'   => $container_values['product_id'],
                    
'quantity'     => $container_values['quantity'],
                    
'container_id' => $container_id,
                ),
            );
        }
        
array_push$composite_product$components );
    }
    return 
$composite_product;
}


/**
 * Build the wck_cart and return the event_data.
 *
 * @param WC_Cart $cart The woocommerce cart.
 * @return array Normalized event data
 */
function wck_build_cart_data$cart ) {
    
$event_data         = array(
        
'CurrencySymbol' => get_woocommerce_currency_symbol(),
        
'Currency'       => get_woocommerce_currency(),
        
'$value'         => $cart->total,
        
'$extra'         => array(
            
'Items'         => array(),
            
'SubTotal'      => $cart->subtotal,
            
'ShippingTotal' => $cart->shipping_total,
            
'TaxTotal'      => $cart->tax_total,
            
'GrandTotal'    => $cart->total,
        ),
    );
    
$wck_cart           = array();
    
$composite_products = array();
    
$normal_products    = array();
    
$all_categories     = array();
    
$item_names         = array();
    
$all_tags           = array();
    
$item_count         0;

    foreach ( 
$cart->get_cart() as $cart_item_key => $values ) {
        
$product           $values['data'];
        
$parent_product_id $product->get_parent_id();

        if ( 
$product->get_parent_id() == ) {
            
$parent_product_id $product->get_id();
        }

        
$categories       = array();
        
$categories_array get_the_terms$parent_product_id'product_cat' );
        if ( 
$categories_array && ! is_wp_error$categories_array ) ) {
            
$categories wp_list_pluck$categories_array'name' );

            foreach ( 
$categories as $category ) {
                
array_push$all_categories$category );
            }
        }
        
$tags_array get_the_terms$parent_product_id'product_tag' );
        if ( 
$tags_array && ! is_wp_error$tags_array ) ) {
            
$tags wp_list_pluck$tags_array'name' );

            foreach ( 
$tags as $tag ) {
                
array_push$all_tags$tag );
            }
        }

        
$is_composite_child false;

        
$is_chained_product is_chained_product$values );

        if ( 
class_exists'WC_Composite_Products' ) ) {
            
$product_encoded    json_encode$product );
            
$is_composite_child wc_cp_is_composited_cart_item$values );
            
$container          wc_cp_get_composited_cart_item_container$values );

            if ( 
$product->get_type() == 'composite' ) {
                
$composite_product = array();

                foreach ( 
wc_cp_get_composited_cart_items$values ) as $key => $val ) {
                    
$composite_product add_encoded_composite$val['composite_data'], $values );
                    break;
                }
                
array_push$composite_products$composite_product );
            } elseif ( ! 
$is_composite_child && ! $is_chained_product ) {
                    
$normal_products$cart_item_key ] = normalize_normal_product$values );
            }
        } elseif ( ! 
$is_chained_product ) {
                
$normal_products$cart_item_key ] = normalize_normal_product$values );
        }

        
$image wp_get_attachment_urlget_post_thumbnail_id$product->get_id() ) );

        if ( 
false == $image ) {
            
$image wp_get_attachment_urlget_post_thumbnail_id$parent_product_id ) );
        }

        
/**
         * Somehow price/tax values can be missing on a product. Defaulting to null
         * in these cases to differentiate from a zero value.
         */
        
$line_total $values['line_total'] ?? null;
        
$line_tax   $values['line_tax'] ?? null;
        
$item       = array(
            
'Quantity' => $values['quantity'],
            
'ProductID' => $parent_product_id,
            
'VariantID' => $product->get_id(),
            
'Name' => $product->get_name(),
            
'URL' => $product->get_permalink(),
            
'Images' => array(
                array(
                    
'URL' => $image,
                ),
            ),
            
'Categories' => $categories,
            
// Default value is an empty array. See WC_Cart::add_to_cart arg, $variation.
            
'Variation' => $values['variation'] ?? array(),
            
'SubTotal' => $values['line_subtotal'] ?? null,
            
'Total' => $values['line_subtotal_tax'] ?? null,
            
'LineTotal' => $line_total,
            
'Tax' => $line_tax,
            
// Ensure both values are non-null e.g. zero is fine here.
            
'TotalWithTax' => isset($line_total$line_tax) ? $line_total $line_tax null,
        );

        
/**
        * Allow developers to customise the payload before it is sent to Klaviyo.
        *
        * The `kl_checkout_item` filter allows you to add additional properties to the line items that make up the
        * [Started Checkout](https://help.klaviyo.com/hc/en-us/articles/360030732832#started-checkout1) event.
        *
        * The example below shows you how to add custom fields to the line items on the Started Checkout event
        *
        * add_filter('kl_checkout_item','kl_modify_checkout_item',1,3);
        *
        * function kl_modify_checkout_item($line_item, $product, $values) {
        *        $product_lead_time =  get_field('leadtime',$product->get_id());
        *        $product_designer = get_field('designer', $product->get_id());
        *        $line_item['LeadTime'] = $product_lead_time;
        *        $line_item['Designer'] = $product_designer;
        *        return $line_item;
        * }
        *
        * @since 3.0.12
        *
        * @param array      $item The item in the cart which will be saved to the Klaviyo event
        * @param WC_Product $product The product being added to the cart
        * @param array      $values The full cart item data
        */
        
$event_data['$extra']['Items'][] = apply_filters'kl_checkout_item'$item$product$values );

        
$item_count              += $values['quantity'];
        
$all_categories           array_valuesarray_unique$all_categories ) );
        
$event_data['Categories'] = $all_categories;
        
$all_tags                 array_valuesarray_unique$all_tags ) );
        
$event_data['Tags']       = $all_tags;
        
array_push$item_names$product->get_name() );
    }

    
$event_data['Quantity']                 = $item_count;
    
$event_data['ItemNames']                = $item_names;
    
$wck_cart['composite']                  = $composite_products;
    
$wck_cart['normal_products']            = $normal_products;
    
$event_data['$extra']['CartRebuildKey'] = base64_encodejson_encode$wck_cart ) );

    return 
$event_data;
}

/**
 * Check if product instance of WooCommerce Chained Products plugin: https://woocommerce.com/products/chained-products/
 *
 * @param object $cart_item_properties cart properties key/values.
 * @return boolean
 */
function is_chained_product$cart_item_properties ) {

    if ( 
class_exists'WC_Chained_Products' ) && ! empty( $cart_item_properties['chained_item_of'] ) ) {
        return 
true;
    }
    return 
false;
}