Completed
Push — master ( 01d710...fba304 )
by
unknown
25:36 queued 11:18
created
includes/librairies/catalog/products.class.php 2 patches
Indentation   +3272 added lines, -3272 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if (!defined('ABSPATH')) {
2
-    exit;
2
+	exit;
3 3
 }
4 4
 
5 5
 /**
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
14 14
 
15 15
 /*    Check if file is include. No direct access possible with file url    */
16 16
 if (!defined('WPSHOP_VERSION')) {
17
-    die(__('Access is not allowed by this way', 'wpshop'));
17
+	die(__('Access is not allowed by this way', 'wpshop'));
18 18
 }
19 19
 
20 20
 /**
@@ -26,231 +26,231 @@  discard block
 block discarded – undo
26 26
  */
27 27
 class wpshop_products
28 28
 {
29
-    /**
30
-     *    Définition du code de la classe courante
31
-     */
32
-    const currentPageCode = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
33
-    const current_page_variation_code = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION;
34
-
35
-    public function add_product_to_current_order_interface()
36
-    {
37
-        $output = '';
38
-        $output .= self::products_list_js();
39
-        $output .= '<input type="text" id="wps_order_search_product" />';
40
-        echo $output;
41
-        die();
42
-    }
43
-
44
-    public function products_list_js()
45
-    {
46
-        global $wpdb;
47
-        /** Create a JS Array of products **/
29
+	/**
30
+	 *    Définition du code de la classe courante
31
+	 */
32
+	const currentPageCode = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
33
+	const current_page_variation_code = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION;
34
+
35
+	public function add_product_to_current_order_interface()
36
+	{
37
+		$output = '';
38
+		$output .= self::products_list_js();
39
+		$output .= '<input type="text" id="wps_order_search_product" />';
40
+		echo $output;
41
+		die();
42
+	}
43
+
44
+	public function products_list_js()
45
+	{
46
+		global $wpdb;
47
+		/** Create a JS Array of products **/
48 48
 //         $query = $wpdb->prepare('SELECT ID, post_title FROM ' .$wpdb->posts. '  WHERE post_type = %s AND post_status = %s', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'publish');
49
-        //         $products_post = $wpdb->get_results( $query );
50
-
51
-        $products_post = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'post_status' => 'publish', 'posts_per_page' => -1));
52
-
53
-        if (!empty($products_post)) {
54
-            $products_js_array = 'var products = [';
55
-            foreach ($products_post as $product) {
56
-                $barcode = get_post_meta($product->ID, '_barcode', true);
57
-                $products_js_array .= '{label:"#' . $product->ID . ' ' . str_replace('"', '', $product->post_title) . ' - ' . ((!empty($barcode)) ? $barcode : '') . '", ';
58
-                $products_js_array .= 'value:"' . $product->ID . '", ';
59
-                $products_js_array .= (!empty($barcode)) ? 'desc:"' . $barcode . '"},' : '},';
60
-            }
61
-            $products_js_array .= '];';
62
-        }
63
-        $output = wpshop_display::display_template_element('wps_orders_products_list_js', array('PRODUCTS_JS_ARRAY' => $products_js_array), array(), 'admin');
64
-        return $output;
65
-    }
66
-
67
-    /**
68
-     *    Déclaration des produits et variations en tant que "post" de wordpress
69
-     *
70
-     *    @see register_post_type()
71
-     */
72
-    public static function create_wpshop_products_type()
73
-    {
74
-
75
-        $options = get_option('wpshop_catalog_product_option', array());
76
-        //$options2 = get_option('wpshop_catalog_categories_option', array());
77
-        /*echo '<pre>'; print_r( $options ); echo '</pre>';
49
+		//         $products_post = $wpdb->get_results( $query );
50
+
51
+		$products_post = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'post_status' => 'publish', 'posts_per_page' => -1));
52
+
53
+		if (!empty($products_post)) {
54
+			$products_js_array = 'var products = [';
55
+			foreach ($products_post as $product) {
56
+				$barcode = get_post_meta($product->ID, '_barcode', true);
57
+				$products_js_array .= '{label:"#' . $product->ID . ' ' . str_replace('"', '', $product->post_title) . ' - ' . ((!empty($barcode)) ? $barcode : '') . '", ';
58
+				$products_js_array .= 'value:"' . $product->ID . '", ';
59
+				$products_js_array .= (!empty($barcode)) ? 'desc:"' . $barcode . '"},' : '},';
60
+			}
61
+			$products_js_array .= '];';
62
+		}
63
+		$output = wpshop_display::display_template_element('wps_orders_products_list_js', array('PRODUCTS_JS_ARRAY' => $products_js_array), array(), 'admin');
64
+		return $output;
65
+	}
66
+
67
+	/**
68
+	 *    Déclaration des produits et variations en tant que "post" de wordpress
69
+	 *
70
+	 *    @see register_post_type()
71
+	 */
72
+	public static function create_wpshop_products_type()
73
+	{
74
+
75
+		$options = get_option('wpshop_catalog_product_option', array());
76
+		//$options2 = get_option('wpshop_catalog_categories_option', array());
77
+		/*echo '<pre>'; print_r( $options ); echo '</pre>';
78 78
         echo '<pre>'; print_r( $options2 ); echo '</pre>'; exit();*/
79 79
 
80
-        $slug = array(
81
-            'slug' => '',
82
-            'with_front' => false,
83
-        );
84
-
85
-        if (empty($options['wpshop_catalog_product_slug']) || $options['wpshop_catalog_product_slug'] == '/') {
86
-            $slug = false;
87
-        } else {
88
-            $slug['slug'] = $options['wpshop_catalog_product_slug'];
89
-            if (!empty($options['wpshop_catalog_product_slug_with_category'])) {
90
-                $slug['slug'] .= '/%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%';
91
-            }
92
-        }
93
-        /*$product_slug .= '/%' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '%';*/
94
-
95
-        /*    Définition des produits     */
96
-        register_post_type(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, apply_filters(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_register', array(
97
-            'labels' => array(
98
-                'name' => __('Products', 'wpshop'),
99
-                'singular_name' => __('Catalog', 'wpshop'),
100
-                'add_new_item' => __('Add new product', 'wpshop'),
101
-                'add_new' => __('Add new product', 'wpshop'),
102
-                'add_new_item' => __('Add new product', 'wpshop'),
103
-                'edit_item' => __('Edit product', 'wpshop'),
104
-                'new_item' => __('New product', 'wpshop'),
105
-                'view_item' => __('View product', 'wpshop'),
106
-                'search_items' => __('Search products', 'wpshop'),
107
-                'not_found' => __('No products found', 'wpshop'),
108
-                'not_found_in_trash' => __('No products found in Trash', 'wpshop'),
109
-                'parent_item_colon' => '',
110
-            ),
111
-            'supports' => unserialize(WPSHOP_REGISTER_POST_TYPE_SUPPORT),
112
-            'public' => true,
113
-            'has_archive' => false,
114
-            'show_in_nav_menus' => true,
115
-            //'rewrite'                 => false,    //    For information see below
116
-            'taxonomies' => array(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES),
117
-            'menu_icon' => 'dashicons-archive',
118
-            'rewrite' => $slug,
119
-        )));
120
-
121
-        /*    Définition des variations de produit (Déclinaisons)    */
122
-        register_post_type(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, apply_filters(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '_register', array(
123
-            'labels' => array(
124
-                'name' => __('Variations', 'wpshop'),
125
-                'singular_name' => __('Variation', 'wpshop'),
126
-                'add_new' => __('Add Variation', 'wpshop'),
127
-                'add_new_item' => __('Add New Variation', 'wpshop'),
128
-                'edit' => __('Edit', 'wpshop'),
129
-                'edit_item' => __('Edit Variation', 'wpshop'),
130
-                'new_item' => __('New Variation', 'wpshop'),
131
-                'view' => __('View Variation', 'wpshop'),
132
-                'view_item' => __('View Variation', 'wpshop'),
133
-                'search_items' => __('Search Variations', 'wpshop'),
134
-                'not_found' => __('No Variations found', 'wpshop'),
135
-                'not_found_in_trash' => __('No Variations found in trash', 'wpshop'),
136
-                'parent_item_colon' => '',
137
-            ),
138
-            'supports' => unserialize(WPSHOP_REGISTER_POST_TYPE_SUPPORT),
139
-            'public' => true,
140
-            'has_archive' => true,
141
-            'show_in_nav_menus' => false,
142
-            'show_in_menu' => false, //'edit.php?post_type=' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
143
-
144
-            'publicly_queryable' => false,
145
-            'exclude_from_search' => true,
146
-            'hierarchical' => false,
147
-
148
-            //             'public'                 => true,
149
-            //             'show_ui'                 => false,
150
-            //             'rewrite'                 => false,
151
-            //             'query_var'                => true,
152
-            //             'supports'                 => array( 'title', 'editor', 'page-attributes', 'thumbnail' ),
153
-            //             'show_in_nav_menus'     => false
154
-        )
155
-        ));
156
-
157
-        // add to our plugin init function
158
-        //global $wp_rewrite;
159
-        /*    Slug url is set into option    */
160
-        /*$options = get_option('wpshop_catalog_product_option', array());
80
+		$slug = array(
81
+			'slug' => '',
82
+			'with_front' => false,
83
+		);
84
+
85
+		if (empty($options['wpshop_catalog_product_slug']) || $options['wpshop_catalog_product_slug'] == '/') {
86
+			$slug = false;
87
+		} else {
88
+			$slug['slug'] = $options['wpshop_catalog_product_slug'];
89
+			if (!empty($options['wpshop_catalog_product_slug_with_category'])) {
90
+				$slug['slug'] .= '/%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%';
91
+			}
92
+		}
93
+		/*$product_slug .= '/%' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '%';*/
94
+
95
+		/*    Définition des produits     */
96
+		register_post_type(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, apply_filters(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_register', array(
97
+			'labels' => array(
98
+				'name' => __('Products', 'wpshop'),
99
+				'singular_name' => __('Catalog', 'wpshop'),
100
+				'add_new_item' => __('Add new product', 'wpshop'),
101
+				'add_new' => __('Add new product', 'wpshop'),
102
+				'add_new_item' => __('Add new product', 'wpshop'),
103
+				'edit_item' => __('Edit product', 'wpshop'),
104
+				'new_item' => __('New product', 'wpshop'),
105
+				'view_item' => __('View product', 'wpshop'),
106
+				'search_items' => __('Search products', 'wpshop'),
107
+				'not_found' => __('No products found', 'wpshop'),
108
+				'not_found_in_trash' => __('No products found in Trash', 'wpshop'),
109
+				'parent_item_colon' => '',
110
+			),
111
+			'supports' => unserialize(WPSHOP_REGISTER_POST_TYPE_SUPPORT),
112
+			'public' => true,
113
+			'has_archive' => false,
114
+			'show_in_nav_menus' => true,
115
+			//'rewrite'                 => false,    //    For information see below
116
+			'taxonomies' => array(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES),
117
+			'menu_icon' => 'dashicons-archive',
118
+			'rewrite' => $slug,
119
+		)));
120
+
121
+		/*    Définition des variations de produit (Déclinaisons)    */
122
+		register_post_type(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, apply_filters(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '_register', array(
123
+			'labels' => array(
124
+				'name' => __('Variations', 'wpshop'),
125
+				'singular_name' => __('Variation', 'wpshop'),
126
+				'add_new' => __('Add Variation', 'wpshop'),
127
+				'add_new_item' => __('Add New Variation', 'wpshop'),
128
+				'edit' => __('Edit', 'wpshop'),
129
+				'edit_item' => __('Edit Variation', 'wpshop'),
130
+				'new_item' => __('New Variation', 'wpshop'),
131
+				'view' => __('View Variation', 'wpshop'),
132
+				'view_item' => __('View Variation', 'wpshop'),
133
+				'search_items' => __('Search Variations', 'wpshop'),
134
+				'not_found' => __('No Variations found', 'wpshop'),
135
+				'not_found_in_trash' => __('No Variations found in trash', 'wpshop'),
136
+				'parent_item_colon' => '',
137
+			),
138
+			'supports' => unserialize(WPSHOP_REGISTER_POST_TYPE_SUPPORT),
139
+			'public' => true,
140
+			'has_archive' => true,
141
+			'show_in_nav_menus' => false,
142
+			'show_in_menu' => false, //'edit.php?post_type=' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
143
+
144
+			'publicly_queryable' => false,
145
+			'exclude_from_search' => true,
146
+			'hierarchical' => false,
147
+
148
+			//             'public'                 => true,
149
+			//             'show_ui'                 => false,
150
+			//             'rewrite'                 => false,
151
+			//             'query_var'                => true,
152
+			//             'supports'                 => array( 'title', 'editor', 'page-attributes', 'thumbnail' ),
153
+			//             'show_in_nav_menus'     => false
154
+		)
155
+		));
156
+
157
+		// add to our plugin init function
158
+		//global $wp_rewrite;
159
+		/*    Slug url is set into option    */
160
+		/*$options = get_option('wpshop_catalog_product_option', array());
161 161
         $gallery_structure = (!empty($options['wpshop_catalog_product_slug']) ? $options['wpshop_catalog_product_slug'] : 'catalog');
162 162
         $gallery_structure .= !empty($options['wpshop_catalog_product_slug_with_category']) ? '/%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%' : '';
163 163
         $gallery_structure .= '/%' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '%';
164 164
         $wp_rewrite->add_permastruct(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, $gallery_structure, false);
165 165
         $wp_rewrite->add_rewrite_tag('%' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '%', '([^/]+)', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . "=");*/
166
-        //    flush_rewrite_rules();
167
-    }
168
-
169
-    public static function hidden_meta_boxes($hidden, $screen, $use_defaults)
170
-    {
171
-        global $wpdb;
172
-        if (WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT === $screen->post_type) {
173
-            $user_meta_for_wpshop = array('metaboxhidden_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
174
-            foreach ($user_meta_for_wpshop as $meta_to_check) {
175
-                $metas_hidden = get_user_meta(get_current_user_id(), $meta_to_check, true);
176
-                if (empty($metas_hidden) && !is_array($metas_hidden)) {
177
-                    $hidden = unserialize(WPSHOP_PRODUCT_HIDDEN_METABOX);
178
-                }
179
-            }
180
-        }
181
-        return $hidden;
182
-    }
183
-
184
-    /**
185
-     *    Create the different bow for the product management page looking for the attribute set to create the different boxes
186
-     */
187
-    public static function add_meta_boxes()
188
-    {
189
-        global $post, $currentTabContent;
190
-
191
-        if (!empty($post->post_type) && (($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) || ($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION))) {
192
-            /*    Get the attribute set list for the current entity    */
193
-            $attributeEntitySetList = wpshop_attributes_set::get_attribute_set_list_for_entity(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode));
194
-            /*    Check if the meta information of the current product already exists     */
195
-            $post_attribute_set_id = get_post_meta($post->ID, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true);
196
-            /*    Check if the product has been saved without meta information set    */
197
-            $attribute_set_id = wpshop_attributes::get_attribute_value_content('product_attribute_set_id', $post->ID, self::currentPageCode);
198
-            /*    Unset 'free_product' from list of attributes set    */
199
-            foreach ($attributeEntitySetList as $key_attribute_set => $attribute_set) {
200
-                if ($attribute_set->slug == 'free_product') {
201
-                    unset($attributeEntitySetList[$key_attribute_set]);
202
-                    break;
203
-                }
204
-            }
205
-
206
-            /*    Check if an attribute has already been choosen for the curernt entity or if the user has to choose a entity set before continuing    */
207
-            if (((count($attributeEntitySetList) == 1) || ((count($attributeEntitySetList) > 1) && (($post_attribute_set_id > 0) || (isset($attribute_set_id->value) && ($attribute_set_id->value > 0)))))) {
208
-                if ((count($attributeEntitySetList) == 1) || (($post_attribute_set_id <= 0) && ($attribute_set_id->value <= 0))) {
209
-                    $post_attribute_set_id = $attributeEntitySetList[0]->id;
210
-                } elseif (($post_attribute_set_id <= 0) && ($attribute_set_id->value > 0)) {
211
-                    $post_attribute_set_id = $attribute_set_id->value;
212
-                }
213
-
214
-                $currentTabContent = wpshop_attributes::entities_attribute_box($post_attribute_set_id, self::currentPageCode, $post->ID);
215
-
216
-                $fixed_box_exist = false;
217
-                /*    Get all the other attribute set for hte current entity    */
218
-                if (isset($currentTabContent['box']) && count($currentTabContent['box']) > 0) {
219
-                    foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
220
-                        if (!empty($currentTabContent['box'][$boxIdentifier . '_backend_display_type']) && ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'movable-tab')) {
221
-                            add_meta_box('wpshop_product_' . $boxIdentifier, __($boxTitle, 'wpshop'), array('wpshop_products', 'meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'default', array('boxIdentifier' => $boxIdentifier));
222
-                        } else {
223
-                            $fixed_box_exist = true;
224
-                        }
225
-
226
-                    }
227
-                }
228
-                if ($fixed_box_exist) {
229
-                    add_meta_box('wpshop_product_fixed_tab', __('Product data', 'wpshop'), array('wpshop_products', 'product_data_meta_box'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'high', array('currentTabContent' => $currentTabContent));
230
-                    add_meta_box('wpshop_product_fixed_tab', __('Product data', 'wpshop'), array('wpshop_products', 'product_data_meta_box'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'normal', 'high', array('currentTabContent' => $currentTabContent));
231
-                }
232
-
233
-                add_meta_box('wpshop_wpshop_variations', __('Product variation', 'wpshop'), array('wpshop_products', 'meta_box_variations'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'default');
234
-                // Actions
235
-                add_meta_box('wpshop_product_actions', __('Actions', 'wpshop'), array('wpshop_products', 'product_actions_meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'side', 'default');
236
-
237
-                /**    Product option    */
238
-                add_meta_box('wpshop_product_options', __('Options', 'wpshop'), array('wpshop_products', 'product_options_meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'side', 'default');
239
-            } else if (count($attributeEntitySetList) > 1) {
240
-                $input_def['id'] = 'product_attribute_set_id';
241
-                $input_def['name'] = 'product_attribute_set_id';
242
-                $input_def['value'] = '';
243
-                $input_def['type'] = 'select';
244
-                $input_def['possible_value'] = $attributeEntitySetList;
245
-
246
-                $input_def['value'] = '';
247
-                foreach ($attributeEntitySetList as $set) {
248
-                    if ($set->default_set == 'yes') {
249
-                        $input_def['value'] = $set->id;
250
-                    }
251
-                }
252
-
253
-                $currentTabContent['boxContent']['attribute_set_selector'] = '
166
+		//    flush_rewrite_rules();
167
+	}
168
+
169
+	public static function hidden_meta_boxes($hidden, $screen, $use_defaults)
170
+	{
171
+		global $wpdb;
172
+		if (WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT === $screen->post_type) {
173
+			$user_meta_for_wpshop = array('metaboxhidden_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
174
+			foreach ($user_meta_for_wpshop as $meta_to_check) {
175
+				$metas_hidden = get_user_meta(get_current_user_id(), $meta_to_check, true);
176
+				if (empty($metas_hidden) && !is_array($metas_hidden)) {
177
+					$hidden = unserialize(WPSHOP_PRODUCT_HIDDEN_METABOX);
178
+				}
179
+			}
180
+		}
181
+		return $hidden;
182
+	}
183
+
184
+	/**
185
+	 *    Create the different bow for the product management page looking for the attribute set to create the different boxes
186
+	 */
187
+	public static function add_meta_boxes()
188
+	{
189
+		global $post, $currentTabContent;
190
+
191
+		if (!empty($post->post_type) && (($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) || ($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION))) {
192
+			/*    Get the attribute set list for the current entity    */
193
+			$attributeEntitySetList = wpshop_attributes_set::get_attribute_set_list_for_entity(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode));
194
+			/*    Check if the meta information of the current product already exists     */
195
+			$post_attribute_set_id = get_post_meta($post->ID, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true);
196
+			/*    Check if the product has been saved without meta information set    */
197
+			$attribute_set_id = wpshop_attributes::get_attribute_value_content('product_attribute_set_id', $post->ID, self::currentPageCode);
198
+			/*    Unset 'free_product' from list of attributes set    */
199
+			foreach ($attributeEntitySetList as $key_attribute_set => $attribute_set) {
200
+				if ($attribute_set->slug == 'free_product') {
201
+					unset($attributeEntitySetList[$key_attribute_set]);
202
+					break;
203
+				}
204
+			}
205
+
206
+			/*    Check if an attribute has already been choosen for the curernt entity or if the user has to choose a entity set before continuing    */
207
+			if (((count($attributeEntitySetList) == 1) || ((count($attributeEntitySetList) > 1) && (($post_attribute_set_id > 0) || (isset($attribute_set_id->value) && ($attribute_set_id->value > 0)))))) {
208
+				if ((count($attributeEntitySetList) == 1) || (($post_attribute_set_id <= 0) && ($attribute_set_id->value <= 0))) {
209
+					$post_attribute_set_id = $attributeEntitySetList[0]->id;
210
+				} elseif (($post_attribute_set_id <= 0) && ($attribute_set_id->value > 0)) {
211
+					$post_attribute_set_id = $attribute_set_id->value;
212
+				}
213
+
214
+				$currentTabContent = wpshop_attributes::entities_attribute_box($post_attribute_set_id, self::currentPageCode, $post->ID);
215
+
216
+				$fixed_box_exist = false;
217
+				/*    Get all the other attribute set for hte current entity    */
218
+				if (isset($currentTabContent['box']) && count($currentTabContent['box']) > 0) {
219
+					foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
220
+						if (!empty($currentTabContent['box'][$boxIdentifier . '_backend_display_type']) && ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'movable-tab')) {
221
+							add_meta_box('wpshop_product_' . $boxIdentifier, __($boxTitle, 'wpshop'), array('wpshop_products', 'meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'default', array('boxIdentifier' => $boxIdentifier));
222
+						} else {
223
+							$fixed_box_exist = true;
224
+						}
225
+
226
+					}
227
+				}
228
+				if ($fixed_box_exist) {
229
+					add_meta_box('wpshop_product_fixed_tab', __('Product data', 'wpshop'), array('wpshop_products', 'product_data_meta_box'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'high', array('currentTabContent' => $currentTabContent));
230
+					add_meta_box('wpshop_product_fixed_tab', __('Product data', 'wpshop'), array('wpshop_products', 'product_data_meta_box'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'normal', 'high', array('currentTabContent' => $currentTabContent));
231
+				}
232
+
233
+				add_meta_box('wpshop_wpshop_variations', __('Product variation', 'wpshop'), array('wpshop_products', 'meta_box_variations'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'default');
234
+				// Actions
235
+				add_meta_box('wpshop_product_actions', __('Actions', 'wpshop'), array('wpshop_products', 'product_actions_meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'side', 'default');
236
+
237
+				/**    Product option    */
238
+				add_meta_box('wpshop_product_options', __('Options', 'wpshop'), array('wpshop_products', 'product_options_meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'side', 'default');
239
+			} else if (count($attributeEntitySetList) > 1) {
240
+				$input_def['id'] = 'product_attribute_set_id';
241
+				$input_def['name'] = 'product_attribute_set_id';
242
+				$input_def['value'] = '';
243
+				$input_def['type'] = 'select';
244
+				$input_def['possible_value'] = $attributeEntitySetList;
245
+
246
+				$input_def['value'] = '';
247
+				foreach ($attributeEntitySetList as $set) {
248
+					if ($set->default_set == 'yes') {
249
+						$input_def['value'] = $set->id;
250
+					}
251
+				}
252
+
253
+				$currentTabContent['boxContent']['attribute_set_selector'] = '
254 254
 	<ul class="attribute_set_selector" >
255 255
 		<li class="attribute_set_selector_title_select" ><label for="title" >' . __('Choose a title for your product', 'wpshop') . '</label></li>
256 256
 		<li class="attribute_set_selector_group_selector" ><label for="' . $input_def['id'] . '" >' . __('Choose an attribute group for this product', 'wpshop') . '</label>&nbsp;' . wpshop_form::check_input_type($input_def, self::currentPageCode . '_attribute[integer]') . '</li>
@@ -258,787 +258,787 @@  discard block
 block discarded – undo
258 258
 		<li class="attribute_set_selector_after_save_instruction" >' . __('Once the group chosen, the different attribute will be displayed here', 'wpshop') . '</li>
259 259
 	</ul>';
260 260
 
261
-                add_meta_box('wpshop_product_attribute_set_selector', __('Product attributes', 'wpshop'), array('wpshop_products', 'meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'high', array('boxIdentifier' => 'attribute_set_selector'));
262
-            }
263
-        }
261
+				add_meta_box('wpshop_product_attribute_set_selector', __('Product attributes', 'wpshop'), array('wpshop_products', 'meta_box_content'), WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'normal', 'high', array('boxIdentifier' => 'attribute_set_selector'));
262
+			}
263
+		}
264 264
 
265
-    }
265
+	}
266 266
 
267
-    /**
268
-     * Add a box into product edition page for options on the product
269
-     *
270
-     * @param object $post
271
-     */
272
-    public static function product_options_meta_box_content($post)
273
-    {
274
-        $output = '';
267
+	/**
268
+	 * Add a box into product edition page for options on the product
269
+	 *
270
+	 * @param object $post
271
+	 */
272
+	public static function product_options_meta_box_content($post)
273
+	{
274
+		$output = '';
275 275
 
276
-        $product_current_options = get_post_meta($post->ID, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options', true);
276
+		$product_current_options = get_post_meta($post->ID, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options', true);
277 277
 
278
-        $tpl_component = array();
279
-        $tpl_component['ADMIN_PRODUCT_OPTION_FOR_CART_AUTOADD_CHECKBOX_STATE'] = (!empty($product_current_options['cart']) && !empty($product_current_options['cart']['auto_add'])) ? ' checked="checked"' : '';
280
-        $output .= wpshop_display::display_template_element('wpshop_admin_product_option_for_cart', $tpl_component, array('type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'id' => $post->ID), 'admin');
278
+		$tpl_component = array();
279
+		$tpl_component['ADMIN_PRODUCT_OPTION_FOR_CART_AUTOADD_CHECKBOX_STATE'] = (!empty($product_current_options['cart']) && !empty($product_current_options['cart']['auto_add'])) ? ' checked="checked"' : '';
280
+		$output .= wpshop_display::display_template_element('wpshop_admin_product_option_for_cart', $tpl_component, array('type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'id' => $post->ID), 'admin');
281 281
 
282
-        echo $output;
283
-    }
282
+		echo $output;
283
+	}
284 284
 
285
-    /**
286
-     * Display the fixed box
287
-     */
288
-    public static function product_data_meta_box($post, $metaboxArgs)
289
-    {
290
-        $output = '';
285
+	/**
286
+	 * Display the fixed box
287
+	 */
288
+	public static function product_data_meta_box($post, $metaboxArgs)
289
+	{
290
+		$output = '';
291 291
 
292
-        $currentTabContent = $metaboxArgs['args']['currentTabContent'];
292
+		$currentTabContent = $metaboxArgs['args']['currentTabContent'];
293 293
 
294
-        echo '<div id="fixed-tabs" class="wpshop_tabs wpshop_detail_tabs wpshop_product_attribute_tabs" >
294
+		echo '<div id="fixed-tabs" class="wpshop_tabs wpshop_detail_tabs wpshop_product_attribute_tabs" >
295 295
 				<ul>';
296
-        if (!empty($currentTabContent['box'])) {
297
-            foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
298
-                if (!empty($currentTabContent['boxContent'][$boxIdentifier])) {
299
-                    if ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'fixed-tab') {
300
-                        echo '<li><a href="#tabs-' . sanitize_title($boxIdentifier) . '">' . __($boxTitle, 'wpshop') . '</a></li>';
301
-                    }
302
-                }
303
-            }
304
-        }
305
-        echo '<li><a href="#tabs-product-related">' . __('Related products', 'wpshop') . '</a></li>';
306
-        echo '<li><a href="#tabs-product-provider">' . __('Providers', 'wpshop') . '</a></li>';
307
-        echo '<li class="wpshop_product_data_display_tab" ><a href="#tabs-product-display">' . __('Product display', 'wpshop') . '</a></li>';
308
-        echo '</ul>';
309
-
310
-        if (!empty($currentTabContent['box'])) {
311
-            foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
312
-                if (!empty($currentTabContent['boxContent'][$boxIdentifier])) {
313
-                    if ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'fixed-tab') {
314
-                        echo '<div id="tabs-' . sanitize_title($boxIdentifier) . '">' . $currentTabContent['boxContent'][$boxIdentifier] . '</div>';
315
-                    }
316
-                }
317
-            }
318
-        }
319
-
320
-        echo '<div id="tabs-product-related">' . self::related_products_meta_box_content($post) . '</div>';
321
-        echo '<div id="tabs-product-provider">' . self::provider_products_meta_box_content($post) . '</div>';
322
-        echo '<div id="tabs-product-display">' . self::product_frontend_display_config_meta_box($post) . '</div>';
323
-        if (!empty($currentTabContent['boxMore'])) {
324
-            echo $currentTabContent['boxMore'];
325
-        }
326
-        echo '</div>';
327
-
328
-        echo $output;
329
-    }
330
-
331
-    /**
332
-     * Output the content for related product metabox
333
-     * @param object $post The current edited post
334
-     * @return string
335
-     */
336
-    public static function related_products_meta_box_content($post)
337
-    {
338
-        $content = $existing_selection = '';
339
-
340
-        if (!empty($post->ID)) {
341
-            $related_products_id = get_post_meta($post->ID, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
342
-            if (!empty($related_products_id) && !empty($related_products_id[0])) {
343
-                foreach ($related_products_id as $related_product_id) {
344
-                    $existing_selection .= '<option selected value="' . $related_product_id . '" >' . get_the_title($related_product_id) . '</option>';
345
-                }
346
-            }
347
-        }
348
-
349
-        $content = '<p>' . __('Type the begin of the product name in the field below in order to add it to the related product list', 'wpshop') . '</p>
296
+		if (!empty($currentTabContent['box'])) {
297
+			foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
298
+				if (!empty($currentTabContent['boxContent'][$boxIdentifier])) {
299
+					if ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'fixed-tab') {
300
+						echo '<li><a href="#tabs-' . sanitize_title($boxIdentifier) . '">' . __($boxTitle, 'wpshop') . '</a></li>';
301
+					}
302
+				}
303
+			}
304
+		}
305
+		echo '<li><a href="#tabs-product-related">' . __('Related products', 'wpshop') . '</a></li>';
306
+		echo '<li><a href="#tabs-product-provider">' . __('Providers', 'wpshop') . '</a></li>';
307
+		echo '<li class="wpshop_product_data_display_tab" ><a href="#tabs-product-display">' . __('Product display', 'wpshop') . '</a></li>';
308
+		echo '</ul>';
309
+
310
+		if (!empty($currentTabContent['box'])) {
311
+			foreach ($currentTabContent['box'] as $boxIdentifier => $boxTitle) {
312
+				if (!empty($currentTabContent['boxContent'][$boxIdentifier])) {
313
+					if ($currentTabContent['box'][$boxIdentifier . '_backend_display_type'] == 'fixed-tab') {
314
+						echo '<div id="tabs-' . sanitize_title($boxIdentifier) . '">' . $currentTabContent['boxContent'][$boxIdentifier] . '</div>';
315
+					}
316
+				}
317
+			}
318
+		}
319
+
320
+		echo '<div id="tabs-product-related">' . self::related_products_meta_box_content($post) . '</div>';
321
+		echo '<div id="tabs-product-provider">' . self::provider_products_meta_box_content($post) . '</div>';
322
+		echo '<div id="tabs-product-display">' . self::product_frontend_display_config_meta_box($post) . '</div>';
323
+		if (!empty($currentTabContent['boxMore'])) {
324
+			echo $currentTabContent['boxMore'];
325
+		}
326
+		echo '</div>';
327
+
328
+		echo $output;
329
+	}
330
+
331
+	/**
332
+	 * Output the content for related product metabox
333
+	 * @param object $post The current edited post
334
+	 * @return string
335
+	 */
336
+	public static function related_products_meta_box_content($post)
337
+	{
338
+		$content = $existing_selection = '';
339
+
340
+		if (!empty($post->ID)) {
341
+			$related_products_id = get_post_meta($post->ID, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
342
+			if (!empty($related_products_id) && !empty($related_products_id[0])) {
343
+				foreach ($related_products_id as $related_product_id) {
344
+					$existing_selection .= '<option selected value="' . $related_product_id . '" >' . get_the_title($related_product_id) . '</option>';
345
+				}
346
+			}
347
+		}
348
+
349
+		$content = '<p>' . __('Type the begin of the product name in the field below in order to add it to the related product list', 'wpshop') . '</p>
350 350
 			<select name="related_products_list[]" id="related_products_list" class="ajax_chosen_select_related_products" multiple >' . $existing_selection . '</select>
351 351
 			<input type="hidden" id="wpshop_ajax_search_element_type_related_products" name="wpshop_ajax_search_element_type" value="' . $post->post_type . '" />
352 352
 			<input type="hidden" id="wpshop_nonce_ajax_search_related_products" name="wpshop_nonce_ajax_search" value="' . wp_create_nonce("wpshop_element_search") . '" />';
353 353
 
354
-        return $content;
355
-    }
356
-
357
-    /**
358
-     * Output the content for related product metabox
359
-     * @param object $post The current edited post
360
-     * @return string
361
-     */
362
-    public static function provider_products_meta_box_content($post)
363
-    {
364
-        $content = $existing_selection = '';
365
-
366
-        if (!empty($post->ID)) {
367
-            $providers_id = get_post_meta($post->ID, WPSHOP_PRODUCT_PROVIDER, true);
368
-            if (!empty($providers_id) && !empty($providers_id[0])) {
369
-                foreach ($providers_id as $provider_id) {
370
-                    $existing_selection .= '<option selected value="' . $provider_id . '" >' . get_the_title($provider_id) . '</option>';
371
-                }
372
-            }
373
-        }
374
-
375
-        $content = '<p>' . __('Type the begin of a provider last name/first name in the field below to add', 'wpshop') . '</p>
354
+		return $content;
355
+	}
356
+
357
+	/**
358
+	 * Output the content for related product metabox
359
+	 * @param object $post The current edited post
360
+	 * @return string
361
+	 */
362
+	public static function provider_products_meta_box_content($post)
363
+	{
364
+		$content = $existing_selection = '';
365
+
366
+		if (!empty($post->ID)) {
367
+			$providers_id = get_post_meta($post->ID, WPSHOP_PRODUCT_PROVIDER, true);
368
+			if (!empty($providers_id) && !empty($providers_id[0])) {
369
+				foreach ($providers_id as $provider_id) {
370
+					$existing_selection .= '<option selected value="' . $provider_id . '" >' . get_the_title($provider_id) . '</option>';
371
+				}
372
+			}
373
+		}
374
+
375
+		$content = '<p>' . __('Type the begin of a provider last name/first name in the field below to add', 'wpshop') . '</p>
376 376
 			<select name="provider_list[]" id="provider_list" class="ajax_chosen_select_provider_products" multiple >' . $existing_selection . '</select>
377 377
 			<input type="hidden" id="wpshop_ajax_search_element_type_provider_products" name="wpshop_ajax_search_element_type" value="' . WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS . '" />
378 378
 			<input type="hidden" id="wpshop_nonce_ajax_search_provider_products" name="wpshop_nonce_ajax_search" value="' . wp_create_nonce("wpshop_element_search") . '" />';
379 379
 
380
-        return $content;
381
-    }
382
-
383
-    /**
384
-     * Define the metabox content for the action box
385
-     * @param obejct $post The current element being edited
386
-     */
387
-    public static function product_actions_meta_box_content($post)
388
-    {
389
-        $output = '';
390
-        /*
380
+		return $content;
381
+	}
382
+
383
+	/**
384
+	 * Define the metabox content for the action box
385
+	 * @param obejct $post The current element being edited
386
+	 */
387
+	public static function product_actions_meta_box_content($post)
388
+	{
389
+		$output = '';
390
+		/*
391 391
          * Template parameters
392 392
          */
393
-        $template_part = 'wpshop_duplicate_product';
394
-        $tpl_component = array();
395
-        $tpl_component['PRODUCT_ID'] = $post->ID;
396
-        $tpl_component['PRINT_PRODUCT_SHEET_LINK'] = admin_url('admin-post.php?action=wps_product_sheet&pid=' . $post->ID);
397
-        /*
393
+		$template_part = 'wpshop_duplicate_product';
394
+		$tpl_component = array();
395
+		$tpl_component['PRODUCT_ID'] = $post->ID;
396
+		$tpl_component['PRINT_PRODUCT_SHEET_LINK'] = admin_url('admin-post.php?action=wps_product_sheet&pid=' . $post->ID);
397
+		/*
398 398
          * Build template
399 399
          */
400
-        $output = wpshop_display::display_template_element($template_part, $tpl_component, array(), 'admin');
401
-        unset($tpl_component);
402
-
403
-        echo $output;
404
-    }
405
-
406
-    /**
407
-     *    Define the content of the product main information box
408
-     */
409
-    public static function meta_box_content($post, $metaboxArgs)
410
-    {
411
-        global $currentTabContent;
412
-
413
-        /*    Add the extra fields defined by the default attribute group in the general section    */
414
-        echo '<div class="wpshop_extra_field_container" >' . $currentTabContent['boxContent'][$metaboxArgs['args']['boxIdentifier']] . '</div>';
415
-    }
416
-
417
-    /**
418
-     * Define the metabox content for product custom display in product
419
-     * @param object $post The current element being edited
420
-     * @return string The metabox content
421
-     */
422
-    public static function product_frontend_display_config_meta_box($post)
423
-    {
424
-        $content = '';
425
-
426
-        $product_attribute_frontend_display_config = null;
427
-        if (!empty($post->ID)) {
428
-            $product_attribute_frontend_display_config = get_post_meta($post->ID, WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, true);
429
-
430
-            $extra_options = get_option('wpshop_extra_options', array());
431
-            $column_count = (!empty($extra_options['WPSHOP_COLUMN_NUMBER_PRODUCT_EDITION_FOR_FRONT_DISPLAY']) ? $extra_options['WPSHOP_COLUMN_NUMBER_PRODUCT_EDITION_FOR_FRONT_DISPLAY'] : 3);
432
-            $attribute_list = wpshop_attributes::getElementWithAttributeAndValue(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $post->ID, WPSHOP_CURRENT_LOCALE);
433
-            $column = 1;
434
-
435
-            if (WPSHOP_DEFINED_SHOP_TYPE == 'sale') {
436
-                $sub_tpl_component = array();
437
-                $sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_NAME'] = __('Action on product', 'wpshop');
438
-
439
-                $tpl_component = array();
440
-                $tpl_component['ADMIN_ATTRIBUTE_LABEL'] = __('Add to cart button', 'wpshop');
441
-                $tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[product_action_button][add_to_cart]';
442
-                $tpl_component['ADMIN_ATTRIBUTE_FD_ID'] = $post->ID . '_product_action_button_add_to_cart';
443
-                $button_is_set_to_be_displayed = (WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no';
444
-
445
-                $tpl_component['ADMIN_ATTRIBUTE_COMPLETE_SHEET_CHECK'] = wpshop_attributes::check_attribute_display($button_is_set_to_be_displayed, $product_attribute_frontend_display_config, 'product_action_button', 'add_to_cart', 'complete_sheet') ? ' checked="checked"' : '';
446
-                $tpl_component['ADMIN_ATTRIBUTE_MINI_OUTPUT_CHECK'] = wpshop_attributes::check_attribute_display($button_is_set_to_be_displayed, $product_attribute_frontend_display_config, 'product_action_button', 'add_to_cart', 'mini_output') ? ' checked="checked"' : '';
447
-                $sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_CONTENT'] = wpshop_display::display_template_element('wpshop_admin_attr_config_for_front_display', $tpl_component, array(), 'admin');
448
-                unset($tpl_component);
449
-
450
-                $sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[product_action_button][add_to_cart]';
451
-                $sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_ID'] = 'product_action_button_add_to_cart';
452
-                $sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_INPUT_CHECKBOX'] = '';
453
-                $sub_content[1] = wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display', $sub_tpl_component, array(), 'admin');
454
-            }
455
-
456
-            if (!empty($attribute_list[$post->ID]) && is_array($attribute_list[$post->ID])) {
457
-                foreach ($attribute_list[$post->ID] as $attribute_set_section_name => $attribute_set_section_content) {
458
-                    if (!isset($sub_content[$column])) {
459
-                        $sub_content[$column] = '';
460
-                    }
461
-
462
-                    $attribute_sub_output = '';
463
-                    foreach ($attribute_set_section_content['attributes'] as $attribute_id => $attribute_def) {
464
-                        if ($attribute_def['attribute_code'] != 'product_attribute_set_id') {
465
-                            $tpl_component = array();
466
-                            $tpl_component['ADMIN_ATTRIBUTE_LABEL'] = $attribute_def['frontend_label'];
467
-                            $tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[attribute][' . $attribute_def['attribute_code'] . ']';
468
-                            $tpl_component['ADMIN_ATTRIBUTE_FD_ID'] = $post->ID . '_' . $attribute_def['attribute_code'];
469
-                            $tpl_component['ADMIN_ATTRIBUTE_COMPLETE_SHEET_CHECK'] = wpshop_attributes::check_attribute_display($attribute_def['is_visible_in_front'], $product_attribute_frontend_display_config, 'attribute', $attribute_def['attribute_code'], 'complete_sheet') ? ' checked="checked"' : '';
470
-                            $tpl_component['ADMIN_ATTRIBUTE_MINI_OUTPUT_CHECK'] = wpshop_attributes::check_attribute_display($attribute_def['is_visible_in_front_listing'], $product_attribute_frontend_display_config, 'attribute', $attribute_def['attribute_code'], 'mini_output') ? ' checked="checked"' : '';
471
-                            $attribute_sub_output .= wpshop_display::display_template_element('wpshop_admin_attr_config_for_front_display', $tpl_component, array(), 'admin');
472
-                            unset($tpl_component);
473
-                        }
474
-                    }
475
-
476
-                    $tpl_component = array();
477
-                    $tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_NAME'] = $attribute_set_section_name;
478
-                    $tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_CONTENT'] = $attribute_sub_output;
479
-                    $tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[attribute_set_section][' . $attribute_set_section_content['code'] . ']';
480
-                    $tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_ID'] = $attribute_set_section_content['code'];
481
-                    $ADMIN_ATTRIBUTE_SET_SECTION_COMPLETE_SHEET_CHECK = wpshop_attributes::check_attribute_display($attribute_set_section_content['display_on_frontend'], $product_attribute_frontend_display_config, 'attribute_set_section', $attribute_set_section_content['code'], 'complete_sheet') ? ' checked="checked"' : '';
482
-                    $tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_INPUT_CHECKBOX'] = '<input type="checkbox" name="' . self::currentPageCode . '_attr_frontend_display[attribute_set_section][' . $attribute_set_section_content['code'] . '][complete_sheet]" id="' . $attribute_set_section_content['code'] . '_complete_sheet" value="yes"' . $ADMIN_ATTRIBUTE_SET_SECTION_COMPLETE_SHEET_CHECK . ' /><label for="' . $attribute_set_section_content['code'] . '_complete_sheet" >' . __('Display in product page', 'wpshop') . '</label>';
483
-                    $sub_content[$column] .= wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display', $tpl_component, array(), 'admin');
484
-                    $column++;
485
-                    if ($column > $column_count) {
486
-                        $column = 1;
487
-                    }
488
-                }
489
-            }
490
-            $tpl_component = array();
491
-            $tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT'] = '';
492
-            for ($i = 1; $i <= $column_count; $i++) {
493
-                if (!empty($sub_content[$i])) {
494
-                    $tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT'] .= '<div class="alignleft" >' . $sub_content[$i] . '</div>';
495
-                }
496
-
497
-            }
498
-            $tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT_CLASS'] = empty($product_attribute_frontend_display_config) ? ' class="wpshopHide" ' : '';
499
-            $tpl_component['ADMIN_PRODUCT_ATTRIBUTE_FRONTEND_DISPLAY_MAIN_CHOICE_CHECK'] = empty($product_attribute_frontend_display_config) ? ' checked="checked"' : '';
500
-            $tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display';
501
-
502
-            $content = wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display_default_choice', $tpl_component, array(), 'admin') . '<div class="wpshop_cls"></div>';
503
-        }
504
-
505
-        return $content;
506
-    }
507
-
508
-    /**
509
-     * Retrieve the attribute list used for sorting product into frontend listing
510
-     * @return array The attribute list to use for listing sorting
511
-     */
512
-    public static function get_sorting_criteria()
513
-    {
514
-        global $wpdb;
515
-
516
-        $data = array(array('code' => 'title', 'frontend_label' => __('Product name', 'wpshop')), array('code' => 'date', 'frontend_label' => __('Date added', 'wpshop')), array('code' => 'modified', 'frontend_label' => __('Date modified', 'wpshop')));
517
-
518
-        $query = $wpdb->prepare("SELECT code, frontend_label FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE is_used_for_sort_by = %s", 'yes');
519
-        $results = $wpdb->get_results($query, ARRAY_A);
520
-        if (!empty($results)) {
521
-            $data = array_merge($data, $results);
522
-        }
523
-
524
-        return $data;
525
-    }
526
-
527
-    public static function get_products_matching_attribute($attr_name, $attr_value)
528
-    {
529
-        global $wpdb;
530
-
531
-        $products = array();
532
-        $query = "SELECT * FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code=%s";
533
-        $data = (array) $wpdb->get_row($wpdb->prepare($query, $attr_name));
534
-
535
-        if (!empty($data)) {
536
-            if ($data['data_type_to_use'] == 'custom') {
537
-                // Find which table to take
538
-                if ($data['data_type'] == 'datetime') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME;} elseif ($data['data_type'] == 'decimal') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL;} elseif ($data['data_type'] == 'integer') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER;} elseif ($data['data_type'] == 'options') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;} elseif ($data['data_type'] == 'text') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT;} elseif ($data['data_type'] == 'varchar') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR;}
539
-
540
-                if (isset($table_name)) {
541
-                    // If the value is an id of a select, radio or checkbox
542
-                    if (in_array($data['backend_input'], array('select', 'multiple-select', 'radio', 'checkbox'))) {
543
-
544
-                        $query = $wpdb->prepare("
400
+		$output = wpshop_display::display_template_element($template_part, $tpl_component, array(), 'admin');
401
+		unset($tpl_component);
402
+
403
+		echo $output;
404
+	}
405
+
406
+	/**
407
+	 *    Define the content of the product main information box
408
+	 */
409
+	public static function meta_box_content($post, $metaboxArgs)
410
+	{
411
+		global $currentTabContent;
412
+
413
+		/*    Add the extra fields defined by the default attribute group in the general section    */
414
+		echo '<div class="wpshop_extra_field_container" >' . $currentTabContent['boxContent'][$metaboxArgs['args']['boxIdentifier']] . '</div>';
415
+	}
416
+
417
+	/**
418
+	 * Define the metabox content for product custom display in product
419
+	 * @param object $post The current element being edited
420
+	 * @return string The metabox content
421
+	 */
422
+	public static function product_frontend_display_config_meta_box($post)
423
+	{
424
+		$content = '';
425
+
426
+		$product_attribute_frontend_display_config = null;
427
+		if (!empty($post->ID)) {
428
+			$product_attribute_frontend_display_config = get_post_meta($post->ID, WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, true);
429
+
430
+			$extra_options = get_option('wpshop_extra_options', array());
431
+			$column_count = (!empty($extra_options['WPSHOP_COLUMN_NUMBER_PRODUCT_EDITION_FOR_FRONT_DISPLAY']) ? $extra_options['WPSHOP_COLUMN_NUMBER_PRODUCT_EDITION_FOR_FRONT_DISPLAY'] : 3);
432
+			$attribute_list = wpshop_attributes::getElementWithAttributeAndValue(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $post->ID, WPSHOP_CURRENT_LOCALE);
433
+			$column = 1;
434
+
435
+			if (WPSHOP_DEFINED_SHOP_TYPE == 'sale') {
436
+				$sub_tpl_component = array();
437
+				$sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_NAME'] = __('Action on product', 'wpshop');
438
+
439
+				$tpl_component = array();
440
+				$tpl_component['ADMIN_ATTRIBUTE_LABEL'] = __('Add to cart button', 'wpshop');
441
+				$tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[product_action_button][add_to_cart]';
442
+				$tpl_component['ADMIN_ATTRIBUTE_FD_ID'] = $post->ID . '_product_action_button_add_to_cart';
443
+				$button_is_set_to_be_displayed = (WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no';
444
+
445
+				$tpl_component['ADMIN_ATTRIBUTE_COMPLETE_SHEET_CHECK'] = wpshop_attributes::check_attribute_display($button_is_set_to_be_displayed, $product_attribute_frontend_display_config, 'product_action_button', 'add_to_cart', 'complete_sheet') ? ' checked="checked"' : '';
446
+				$tpl_component['ADMIN_ATTRIBUTE_MINI_OUTPUT_CHECK'] = wpshop_attributes::check_attribute_display($button_is_set_to_be_displayed, $product_attribute_frontend_display_config, 'product_action_button', 'add_to_cart', 'mini_output') ? ' checked="checked"' : '';
447
+				$sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_CONTENT'] = wpshop_display::display_template_element('wpshop_admin_attr_config_for_front_display', $tpl_component, array(), 'admin');
448
+				unset($tpl_component);
449
+
450
+				$sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[product_action_button][add_to_cart]';
451
+				$sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_ID'] = 'product_action_button_add_to_cart';
452
+				$sub_tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_INPUT_CHECKBOX'] = '';
453
+				$sub_content[1] = wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display', $sub_tpl_component, array(), 'admin');
454
+			}
455
+
456
+			if (!empty($attribute_list[$post->ID]) && is_array($attribute_list[$post->ID])) {
457
+				foreach ($attribute_list[$post->ID] as $attribute_set_section_name => $attribute_set_section_content) {
458
+					if (!isset($sub_content[$column])) {
459
+						$sub_content[$column] = '';
460
+					}
461
+
462
+					$attribute_sub_output = '';
463
+					foreach ($attribute_set_section_content['attributes'] as $attribute_id => $attribute_def) {
464
+						if ($attribute_def['attribute_code'] != 'product_attribute_set_id') {
465
+							$tpl_component = array();
466
+							$tpl_component['ADMIN_ATTRIBUTE_LABEL'] = $attribute_def['frontend_label'];
467
+							$tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[attribute][' . $attribute_def['attribute_code'] . ']';
468
+							$tpl_component['ADMIN_ATTRIBUTE_FD_ID'] = $post->ID . '_' . $attribute_def['attribute_code'];
469
+							$tpl_component['ADMIN_ATTRIBUTE_COMPLETE_SHEET_CHECK'] = wpshop_attributes::check_attribute_display($attribute_def['is_visible_in_front'], $product_attribute_frontend_display_config, 'attribute', $attribute_def['attribute_code'], 'complete_sheet') ? ' checked="checked"' : '';
470
+							$tpl_component['ADMIN_ATTRIBUTE_MINI_OUTPUT_CHECK'] = wpshop_attributes::check_attribute_display($attribute_def['is_visible_in_front_listing'], $product_attribute_frontend_display_config, 'attribute', $attribute_def['attribute_code'], 'mini_output') ? ' checked="checked"' : '';
471
+							$attribute_sub_output .= wpshop_display::display_template_element('wpshop_admin_attr_config_for_front_display', $tpl_component, array(), 'admin');
472
+							unset($tpl_component);
473
+						}
474
+					}
475
+
476
+					$tpl_component = array();
477
+					$tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_NAME'] = $attribute_set_section_name;
478
+					$tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_CONTENT'] = $attribute_sub_output;
479
+					$tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_NAME'] = self::currentPageCode . '_attr_frontend_display[attribute_set_section][' . $attribute_set_section_content['code'] . ']';
480
+					$tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_FD_ID'] = $attribute_set_section_content['code'];
481
+					$ADMIN_ATTRIBUTE_SET_SECTION_COMPLETE_SHEET_CHECK = wpshop_attributes::check_attribute_display($attribute_set_section_content['display_on_frontend'], $product_attribute_frontend_display_config, 'attribute_set_section', $attribute_set_section_content['code'], 'complete_sheet') ? ' checked="checked"' : '';
482
+					$tpl_component['ADMIN_ATTRIBUTE_SET_SECTION_INPUT_CHECKBOX'] = '<input type="checkbox" name="' . self::currentPageCode . '_attr_frontend_display[attribute_set_section][' . $attribute_set_section_content['code'] . '][complete_sheet]" id="' . $attribute_set_section_content['code'] . '_complete_sheet" value="yes"' . $ADMIN_ATTRIBUTE_SET_SECTION_COMPLETE_SHEET_CHECK . ' /><label for="' . $attribute_set_section_content['code'] . '_complete_sheet" >' . __('Display in product page', 'wpshop') . '</label>';
483
+					$sub_content[$column] .= wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display', $tpl_component, array(), 'admin');
484
+					$column++;
485
+					if ($column > $column_count) {
486
+						$column = 1;
487
+					}
488
+				}
489
+			}
490
+			$tpl_component = array();
491
+			$tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT'] = '';
492
+			for ($i = 1; $i <= $column_count; $i++) {
493
+				if (!empty($sub_content[$i])) {
494
+					$tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT'] .= '<div class="alignleft" >' . $sub_content[$i] . '</div>';
495
+				}
496
+
497
+			}
498
+			$tpl_component['ADMIN_ATTRIBUTE_FRONTEND_DISPLAY_CONTENT_CLASS'] = empty($product_attribute_frontend_display_config) ? ' class="wpshopHide" ' : '';
499
+			$tpl_component['ADMIN_PRODUCT_ATTRIBUTE_FRONTEND_DISPLAY_MAIN_CHOICE_CHECK'] = empty($product_attribute_frontend_display_config) ? ' checked="checked"' : '';
500
+			$tpl_component['ADMIN_ATTRIBUTE_FD_NAME'] = self::currentPageCode . '_attr_frontend_display';
501
+
502
+			$content = wpshop_display::display_template_element('wpshop_admin_attr_set_section_for_front_display_default_choice', $tpl_component, array(), 'admin') . '<div class="wpshop_cls"></div>';
503
+		}
504
+
505
+		return $content;
506
+	}
507
+
508
+	/**
509
+	 * Retrieve the attribute list used for sorting product into frontend listing
510
+	 * @return array The attribute list to use for listing sorting
511
+	 */
512
+	public static function get_sorting_criteria()
513
+	{
514
+		global $wpdb;
515
+
516
+		$data = array(array('code' => 'title', 'frontend_label' => __('Product name', 'wpshop')), array('code' => 'date', 'frontend_label' => __('Date added', 'wpshop')), array('code' => 'modified', 'frontend_label' => __('Date modified', 'wpshop')));
517
+
518
+		$query = $wpdb->prepare("SELECT code, frontend_label FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE is_used_for_sort_by = %s", 'yes');
519
+		$results = $wpdb->get_results($query, ARRAY_A);
520
+		if (!empty($results)) {
521
+			$data = array_merge($data, $results);
522
+		}
523
+
524
+		return $data;
525
+	}
526
+
527
+	public static function get_products_matching_attribute($attr_name, $attr_value)
528
+	{
529
+		global $wpdb;
530
+
531
+		$products = array();
532
+		$query = "SELECT * FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code=%s";
533
+		$data = (array) $wpdb->get_row($wpdb->prepare($query, $attr_name));
534
+
535
+		if (!empty($data)) {
536
+			if ($data['data_type_to_use'] == 'custom') {
537
+				// Find which table to take
538
+				if ($data['data_type'] == 'datetime') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME;} elseif ($data['data_type'] == 'decimal') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL;} elseif ($data['data_type'] == 'integer') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER;} elseif ($data['data_type'] == 'options') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;} elseif ($data['data_type'] == 'text') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT;} elseif ($data['data_type'] == 'varchar') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR;}
539
+
540
+				if (isset($table_name)) {
541
+					// If the value is an id of a select, radio or checkbox
542
+					if (in_array($data['backend_input'], array('select', 'multiple-select', 'radio', 'checkbox'))) {
543
+
544
+						$query = $wpdb->prepare("
545 545
 							SELECT " . $table_name . ".entity_id FROM " . $table_name . "
546 546
 							INNER JOIN {$wpdb->posts} AS P ON ( ( P.ID = " . $table_name . ".entity_id ) AND ( P.post_status = 'publish' ) )
547 547
 							LEFT JOIN " . WPSHOP_DBT_ATTRIBUTE . " AS ATT ON ATT.id = " . $table_name . ".attribute_id
548 548
 							LEFT JOIN " . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . " AS ATT_OPT ON " . $table_name . ".value = ATT_OPT.id
549 549
 							WHERE ATT.code=%s AND ( ATT_OPT.value=%s OR ATT_OPT.value=%s )", $attr_name, strtolower(__($attr_value, 'wpshop')), $attr_value
550
-                        );
551
-                        $data = $wpdb->get_results($query);
552
-                    } else {
550
+						);
551
+						$data = $wpdb->get_results($query);
552
+					} else {
553 553
 
554
-                        $query = $wpdb->prepare("
554
+						$query = $wpdb->prepare("
555 555
 							SELECT " . $table_name . ".entity_id FROM " . $table_name . "
556 556
 							INNER JOIN {$wpdb->posts} AS P ON ( ( P.ID = " . $table_name . ".entity_id ) AND ( P.post_status = 'publish' ) )
557 557
 							INNER JOIN " . WPSHOP_DBT_ATTRIBUTE . " AS ATT ON ATT.id = " . $table_name . ".attribute_id
558 558
 								INNER JOIN {$wpdb->posts} AS P ON ( ( P.ID = " . $table_name . ".entity_id ) AND ( P.post_status = 'publish' ) )
559 559
 							WHERE ATT.code=%s AND " . $table_name . ".value=%s", $attr_name, sprintf('%.5f', $attr_value) // force useless zero like 48.58000
560
-                        );
561
-                        $data = $wpdb->get_results($query);
562
-
563
-                    }
564
-                } else {
565
-                    return __('Incorrect shortcode', 'wpshop');
566
-                }
567
-
568
-            } elseif ($data['data_type_to_use'] == 'internal') {
569
-                /** Check the ID of manufacturer **/
570
-                $default_value = unserialize($data['default_value']);
571
-                if (!empty($default_value) && !empty($default_value['default_value'])) {
572
-                    $query = get_post($attr_value);
573
-                    $pid = (!empty($query) && !empty($query->ID)) ? $query->ID : '';
574
-                    if (!empty($pid)) {
575
-                        $query = $wpdb->prepare('SELECT post_id AS entity_id FROM ' . $wpdb->postmeta . ' WHERE post_id = ID AND post_status = %s AND meta_key = %s AND meta_value = %s', 'publish', '_' . $data['code'], $pid);
576
-                        $data = $wpdb->get_results($query);
577
-                    }
578
-                }
579
-            } else {
580
-                return __('Incorrect shortcode', 'wpshop');
581
-            }
582
-
583
-        } else {
584
-            return __('Incorrect shortcode', 'wpshop');
585
-        }
586
-
587
-        if (!empty($data)) {
588
-            foreach ($data as $p) {
589
-                if (!empty($p) && is_object($p) && !empty($p->entity_id)) {
590
-                    $products[] = $p->entity_id;
591
-                }
592
-            }
593
-        }
594
-        return $products;
595
-    }
596
-
597
-    /**
598
-     * Related product shortcode reader
599
-     *
600
-     * @param array $atts {
601
-     *    pid : Product idenfifier to get related element for
602
-     *    display_mode : The output mode if defined (grid || list)
603
-     * }
604
-     *
605
-     * @return string
606
-     *
607
-     */
608
-    public static function wpshop_related_products_func($atts)
609
-    {
610
-        global $wp_query;
611
-
612
-        $atts['product_type'] = 'related';
613
-        if (empty($atts['pid'])) {
614
-            $atts['pid'] = $wp_query->posts[0]->ID;
615
-        }
616
-
617
-        $related_product_output = self::wpshop_products_func($atts);
618
-
619
-        $output = '';
620
-        if (!empty($related_product_output)) {
621
-            ob_start();
622
-            require wpshop_tools::get_template_part(WPS_PRODUCT_DIR, WPS_PRODUCT_TEMPLATES_MAIN_DIR, 'frontend', 'related_product');
623
-            $output = ob_get_contents();
624
-            ob_end_clean();
625
-        }
626
-
627
-        return $output;
628
-    }
629
-
630
-    /**
631
-     * Display a list of product from a shortcode
632
-     *
633
-     * @param array $atts {
634
-     *    limit : The number of element to display
635
-     *    order : The information to order list by
636
-     *    sorting : List order (ASC | DESC)
637
-     *    display : Display size (normal | mini)
638
-     *    type : Display tyep (grid | list) only work with display=normal
639
-     *    pagination : The number of element per page
640
-     * display_pagination : Display the pagination or not
641
-     * }
642
-     *
643
-     * @return string
644
-     *
645
-     **/
646
-    public static function wpshop_products_func($atts)
647
-    {
648
-        global $wpdb, $wp_query;
649
-
650
-        $string = '';
651
-        $have_results = false;
652
-        $output_results = true;
653
-        $type = (empty($atts['type']) or !in_array($atts['type'], array('grid', 'list'))) ? WPSHOP_DISPLAY_LIST_TYPE : $atts['type'];
654
-        $pagination = isset($atts['pagination']) ? intval($atts['pagination']) : WPSHOP_ELEMENT_NB_PER_PAGE;
655
-        $cid = !empty($atts['cid']) ? $atts['cid'] : 0;
656
-        $pid = !empty($atts['pid']) ? $atts['pid'] : 0;
657
-        $order_by_sorting = (!empty($atts['sorting']) && ($atts['sorting'] == 'DESC')) ? 'DESC' : 'ASC';
658
-        $limit = isset($atts['limit']) ? intval($atts['limit']) : 0;
659
-        $grid_element_nb_per_line = !empty($atts['grid_element_nb_per_line']) ? $atts['grid_element_nb_per_line'] : WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE;
660
-        $display_pagination = (!empty($atts['display_pagination'])) ? (($atts['display_pagination'] === "yes") ? true : false) : true;
661
-        $attr = '';
662
-
663
-        $sorting_criteria = self::get_sorting_criteria();
664
-
665
-        /** Get products which have att_name equal to att_value    */
666
-        if (!empty($atts['att_name']) && !empty($atts['att_value'])) {
667
-            $attr = $atts['att_name'] . ':' . $atts['att_value'];
668
-
669
-            $products = self::get_products_matching_attribute($atts['att_name'], $atts['att_value']);
670
-
671
-            // Foreach on the found products
672
-            if (!empty($products)) {
673
-                $pid = implode(',', $products);
674
-                if (empty($pid)) {
675
-                    $output_results = false;
676
-                }
677
-
678
-            } else {
679
-                $output_results = false;
680
-            }
681
-
682
-        }
683
-
684
-        /** Get related products    */
685
-        if (!empty($atts['product_type'])) {
686
-            switch ($atts['product_type']) {
687
-                case 'related':
688
-                    $product_id = !empty($atts['pid']) ? (int) $atts['pid'] : get_the_ID();
689
-                    $type = !empty($atts['display_mode']) && in_array($atts['display_mode'], array('list', 'grid')) ? $atts['display_mode'] : WPSHOP_DISPLAY_LIST_TYPE;
690
-                    $grid_element_nb_per_line = !empty($atts['grid_element_nb_per_line']) ? $atts['grid_element_nb_per_line'] : WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE;
691
-
692
-                    $pids = get_post_meta((int) $product_id, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
693
-                    if (!empty($pids) && !empty($pids[0])) {
694
-                        $pid = implode(',', $pids);
695
-                    }
696
-                    if (empty($pid) || $pid == $product_id) {
697
-                        $output_results = false;
698
-                    }
699
-
700
-                    break;
701
-            }
702
-        }
703
-
704
-        /** Output all the products    */
705
-        if ($output_results) {
706
-            $data = self::wpshop_get_product_by_criteria((!empty($atts['order']) ? $atts['order'] : (!empty($atts['creator']) ? ($atts['creator'] == 'current') : '')), $cid, $pid, $type, $order_by_sorting, 1, $pagination, $limit, $grid_element_nb_per_line, $display_pagination);
707
-
708
-            if ($data[0]) {
709
-                $have_results = true;
710
-                $string = $data[1];
711
-            }
712
-        }
713
-
714
-        /** If there are result to display    */
715
-        if ($have_results) {
716
-            $sorting = '';
717
-            if (!empty($pid)) {
718
-                $product_list = explode(',', $pid);
719
-                if (count($product_list) == 1) {
720
-                    $atts['sorting'] = 'no';
721
-                }
722
-            }
723
-
724
-            /*
560
+						);
561
+						$data = $wpdb->get_results($query);
562
+
563
+					}
564
+				} else {
565
+					return __('Incorrect shortcode', 'wpshop');
566
+				}
567
+
568
+			} elseif ($data['data_type_to_use'] == 'internal') {
569
+				/** Check the ID of manufacturer **/
570
+				$default_value = unserialize($data['default_value']);
571
+				if (!empty($default_value) && !empty($default_value['default_value'])) {
572
+					$query = get_post($attr_value);
573
+					$pid = (!empty($query) && !empty($query->ID)) ? $query->ID : '';
574
+					if (!empty($pid)) {
575
+						$query = $wpdb->prepare('SELECT post_id AS entity_id FROM ' . $wpdb->postmeta . ' WHERE post_id = ID AND post_status = %s AND meta_key = %s AND meta_value = %s', 'publish', '_' . $data['code'], $pid);
576
+						$data = $wpdb->get_results($query);
577
+					}
578
+				}
579
+			} else {
580
+				return __('Incorrect shortcode', 'wpshop');
581
+			}
582
+
583
+		} else {
584
+			return __('Incorrect shortcode', 'wpshop');
585
+		}
586
+
587
+		if (!empty($data)) {
588
+			foreach ($data as $p) {
589
+				if (!empty($p) && is_object($p) && !empty($p->entity_id)) {
590
+					$products[] = $p->entity_id;
591
+				}
592
+			}
593
+		}
594
+		return $products;
595
+	}
596
+
597
+	/**
598
+	 * Related product shortcode reader
599
+	 *
600
+	 * @param array $atts {
601
+	 *    pid : Product idenfifier to get related element for
602
+	 *    display_mode : The output mode if defined (grid || list)
603
+	 * }
604
+	 *
605
+	 * @return string
606
+	 *
607
+	 */
608
+	public static function wpshop_related_products_func($atts)
609
+	{
610
+		global $wp_query;
611
+
612
+		$atts['product_type'] = 'related';
613
+		if (empty($atts['pid'])) {
614
+			$atts['pid'] = $wp_query->posts[0]->ID;
615
+		}
616
+
617
+		$related_product_output = self::wpshop_products_func($atts);
618
+
619
+		$output = '';
620
+		if (!empty($related_product_output)) {
621
+			ob_start();
622
+			require wpshop_tools::get_template_part(WPS_PRODUCT_DIR, WPS_PRODUCT_TEMPLATES_MAIN_DIR, 'frontend', 'related_product');
623
+			$output = ob_get_contents();
624
+			ob_end_clean();
625
+		}
626
+
627
+		return $output;
628
+	}
629
+
630
+	/**
631
+	 * Display a list of product from a shortcode
632
+	 *
633
+	 * @param array $atts {
634
+	 *    limit : The number of element to display
635
+	 *    order : The information to order list by
636
+	 *    sorting : List order (ASC | DESC)
637
+	 *    display : Display size (normal | mini)
638
+	 *    type : Display tyep (grid | list) only work with display=normal
639
+	 *    pagination : The number of element per page
640
+	 * display_pagination : Display the pagination or not
641
+	 * }
642
+	 *
643
+	 * @return string
644
+	 *
645
+	 **/
646
+	public static function wpshop_products_func($atts)
647
+	{
648
+		global $wpdb, $wp_query;
649
+
650
+		$string = '';
651
+		$have_results = false;
652
+		$output_results = true;
653
+		$type = (empty($atts['type']) or !in_array($atts['type'], array('grid', 'list'))) ? WPSHOP_DISPLAY_LIST_TYPE : $atts['type'];
654
+		$pagination = isset($atts['pagination']) ? intval($atts['pagination']) : WPSHOP_ELEMENT_NB_PER_PAGE;
655
+		$cid = !empty($atts['cid']) ? $atts['cid'] : 0;
656
+		$pid = !empty($atts['pid']) ? $atts['pid'] : 0;
657
+		$order_by_sorting = (!empty($atts['sorting']) && ($atts['sorting'] == 'DESC')) ? 'DESC' : 'ASC';
658
+		$limit = isset($atts['limit']) ? intval($atts['limit']) : 0;
659
+		$grid_element_nb_per_line = !empty($atts['grid_element_nb_per_line']) ? $atts['grid_element_nb_per_line'] : WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE;
660
+		$display_pagination = (!empty($atts['display_pagination'])) ? (($atts['display_pagination'] === "yes") ? true : false) : true;
661
+		$attr = '';
662
+
663
+		$sorting_criteria = self::get_sorting_criteria();
664
+
665
+		/** Get products which have att_name equal to att_value    */
666
+		if (!empty($atts['att_name']) && !empty($atts['att_value'])) {
667
+			$attr = $atts['att_name'] . ':' . $atts['att_value'];
668
+
669
+			$products = self::get_products_matching_attribute($atts['att_name'], $atts['att_value']);
670
+
671
+			// Foreach on the found products
672
+			if (!empty($products)) {
673
+				$pid = implode(',', $products);
674
+				if (empty($pid)) {
675
+					$output_results = false;
676
+				}
677
+
678
+			} else {
679
+				$output_results = false;
680
+			}
681
+
682
+		}
683
+
684
+		/** Get related products    */
685
+		if (!empty($atts['product_type'])) {
686
+			switch ($atts['product_type']) {
687
+				case 'related':
688
+					$product_id = !empty($atts['pid']) ? (int) $atts['pid'] : get_the_ID();
689
+					$type = !empty($atts['display_mode']) && in_array($atts['display_mode'], array('list', 'grid')) ? $atts['display_mode'] : WPSHOP_DISPLAY_LIST_TYPE;
690
+					$grid_element_nb_per_line = !empty($atts['grid_element_nb_per_line']) ? $atts['grid_element_nb_per_line'] : WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE;
691
+
692
+					$pids = get_post_meta((int) $product_id, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
693
+					if (!empty($pids) && !empty($pids[0])) {
694
+						$pid = implode(',', $pids);
695
+					}
696
+					if (empty($pid) || $pid == $product_id) {
697
+						$output_results = false;
698
+					}
699
+
700
+					break;
701
+			}
702
+		}
703
+
704
+		/** Output all the products    */
705
+		if ($output_results) {
706
+			$data = self::wpshop_get_product_by_criteria((!empty($atts['order']) ? $atts['order'] : (!empty($atts['creator']) ? ($atts['creator'] == 'current') : '')), $cid, $pid, $type, $order_by_sorting, 1, $pagination, $limit, $grid_element_nb_per_line, $display_pagination);
707
+
708
+			if ($data[0]) {
709
+				$have_results = true;
710
+				$string = $data[1];
711
+			}
712
+		}
713
+
714
+		/** If there are result to display    */
715
+		if ($have_results) {
716
+			$sorting = '';
717
+			if (!empty($pid)) {
718
+				$product_list = explode(',', $pid);
719
+				if (count($product_list) == 1) {
720
+					$atts['sorting'] = 'no';
721
+				}
722
+			}
723
+
724
+			/*
725 725
              * Template parameters
726 726
              */
727
-            $template_part = 'product_listing_sorting';
728
-            $tpl_component = array();
727
+			$template_part = 'product_listing_sorting';
728
+			$tpl_component = array();
729 729
 
730
-            /*
730
+			/*
731 731
              * Build template
732 732
              */
733
-            $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
734
-            if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
735
-                /*    Include the old way template part    */
736
-                ob_start();
737
-                require wpshop_display::get_template_file($tpl_way_to_take[1]);
738
-                $sorting = ob_get_contents();
739
-                ob_end_clean();
740
-            } else {
741
-                /*
733
+			$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
734
+			if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
735
+				/*    Include the old way template part    */
736
+				ob_start();
737
+				require wpshop_display::get_template_file($tpl_way_to_take[1]);
738
+				$sorting = ob_get_contents();
739
+				ob_end_clean();
740
+			} else {
741
+				/*
742 742
                  * Display hidden field every times
743 743
                  */
744
-                $sub_template_part = 'product_listing_sorting_hidden_field';
745
-                $sub_tpl_component = array();
746
-                $sub_tpl_component['DISPLAY_TYPE'] = $type;
747
-                $sub_tpl_component['ORDER'] = $order_by_sorting;
748
-                $sub_tpl_component['PRODUCT_NUMBER'] = $pagination;
749
-                $sub_tpl_component['CURRENT_PAGE'] = 1;
750
-                $sub_tpl_component['CATEGORY_ID'] = $cid;
751
-                $sub_tpl_component['PRODUCT_ID'] = $pid;
752
-                $sub_tpl_component['ATTR'] = $attr;
753
-                $tpl_component['SORTING_HIDDEN_FIELDS'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component, array(), 'admin');
754
-                unset($sub_tpl_component);
755
-
756
-                if ((!empty($sorting_criteria) && is_array($sorting_criteria))) {
757
-                    $sub_template_part = 'product_listing_sorting_criteria';
758
-                    $sub_tpl_component = array();
759
-                    $criteria = '';
760
-                    foreach ($sorting_criteria as $c):
761
-                        $criteria .= '<option value="' . $c['code'] . '">' . __($c['frontend_label'], 'wpshop') . '</option>';
762
-                    endforeach;
763
-                    $sub_tpl_component['SORTING_CRITERIA_LIST'] = $criteria;
764
-                    $tpl_component['SORTING_CRITERIA'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component);
765
-                    unset($sub_tpl_component);
766
-                }
767
-
768
-                if (empty($atts['sorting']) || (!empty($atts['sorting']) && ($atts['sorting'] != 'no'))) {
769
-                    $tpl_component['DISPLAY_TYPE_STATE_GRID'] = $type == 'grid' ? ' active' : null;
770
-                    $tpl_component['DISPLAY_TYPE_STATE_LIST'] = $type == 'list' ? ' active' : null;
771
-                    $sorting = wpshop_display::display_template_element($template_part, $tpl_component);
772
-                } else if (!empty($atts['sorting']) && ($atts['sorting'] == 'no')) {
773
-                    $sub_template_part = 'product_listing_sorting_criteria_hidden';
774
-                    $sub_tpl_component = array();
775
-                    $sub_tpl_component['CRITERIA_DEFAULT'] = !empty($sorting_criteria[0]['code']) ? $sorting_criteria[0]['code'] : 'title';
776
-                    $tpl_component['SORTING_CRITERIA'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component, array(), 'admin');
777
-                    unset($sub_tpl_component);
778
-
779
-                    $template_part = 'product_listing_sorting_hidden';
780
-                    $sorting = wpshop_display::display_template_element($template_part, $tpl_component, array(), 'admin');
781
-                }
782
-            }
783
-            unset($tpl_component);
784
-
785
-            if (!empty($atts) && !empty($atts['container']) && $atts['container'] == 'no') {
786
-                $string = $sorting . '<div data-nonce="' . wp_create_nonce('products_by_criteria') . '" class="wps-catalog-container wps-bloc-loader">' . $string . '</div>';
787
-            } else {
788
-                $string = '<div class="wpshop_products_block">' . $sorting . '<div data-nonce="' . wp_create_nonce('products_by_criteria') . '" class="wps-catalog-container wps-bloc-loader">' . $string . '</div></div>';
789
-            }
790
-        } else if (empty($atts['no_result_message']) || ($atts['no_result_message'] != 'no')) {
791
-            $string = __('There is nothing to output here', 'wpshop');
792
-        }
793
-
794
-        return do_shortcode($string);
795
-    }
796
-
797
-    public static function wpshop_get_product_by_criteria($criteria = null, $cid = 0, $pid = 0, $display_type, $order = 'ASC', $page_number, $products_per_page = 0, $nb_of_product_limit = 0, $grid_element_nb_per_line = WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE, $display_pagination = true)
798
-    {
799
-        global $wpdb;
800
-
801
-        $string = '<span id="wpshop_loading">&nbsp;</span>';
802
-        $have_results = false;
803
-        $display_type = (!empty($display_type) && in_array($display_type, array('grid', 'list'))) ? $display_type : 'grid';
804
-
805
-        /** Check if Discount are activated */
806
-        $discount_option = get_option('wpshop_catalog_product_option');
807
-
808
-        if ($criteria == 'product_price' && !empty($discount_option) && !empty($discount_option['discount']) && $discount_option['discount'] == 'on') {
809
-            $criteria = 'wpshop_displayed_price';
810
-        }
811
-
812
-        $query = array(
813
-            'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
814
-            'order' => $order,
815
-            'posts_per_page' => $products_per_page,
816
-            'paged' => $page_number,
817
-            'post_status' => 'publish',
818
-        );
819
-
820
-        // If the limit is greater than zero, hide pagination and change posts_per_page var
821
-        if ($nb_of_product_limit > 0) {
822
-            $query['posts_per_page'] = $nb_of_product_limit;
823
-            unset($query['paged']);
824
-        }
825
-        if (!empty($pid)) {
826
-            if (!is_array($pid)) {
827
-                $pid = explode(',', $pid);
828
-            }
829
-
830
-            $query['post__in'] = $pid;
831
-        }
832
-        if (!empty($cid)) {
833
-            $cid = explode(',', $cid);
834
-            $query['tax_query'] = array(array(
835
-                'taxonomy' => WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES,
836
-                'field' => 'id',
837
-                'terms' => $cid,
838
-                'operator' => 'IN',
839
-            ));
840
-        }
841
-        if ($criteria != null) {
842
-            switch ($criteria) {
843
-                case 'creator':
844
-                case 'author':
845
-                    $query['author'] = get_current_user_id();
846
-                    break;
847
-                case 'title':
848
-                case 'date':
849
-                case 'modified':
850
-                case 'rand':
851
-                    $query['orderby'] = $criteria;
852
-                    break;
853
-                default:
854
-                    if (!empty($pid)) {
855
-                        $post_meta = get_post_meta($pid, '_' . $criteria, true);
856
-                    } else {
857
-                        $check_meta = $wpdb->prepare("SELECT COUNT(meta_id) as meta_criteria FROM " . $wpdb->postmeta . " WHERE meta_key = %s", '_' . $criteria);
858
-                        $post_meta = $wpdb->get_var($check_meta);
859
-                    }
860
-                    if (!empty($post_meta)) {
861
-                        if (in_array($criteria, array('wpshop_displayed_price', 'product_price'))) {
862
-                            $query['orderby'] = 'meta_value_num';
863
-                        } else {
864
-                            $query['orderby'] = 'meta_value';
865
-                        }
866
-                        $query['meta_key'] = '_' . $criteria;
867
-                    }
868
-                    break;
869
-            }
870
-        } else {
871
-            $query['orderby'] = 'menu_order ID';
872
-        }
873
-        $post_per_page = $query['posts_per_page'];
874
-        $total_products = (!empty($query['post__in'])) ? $query['post__in'] : 0;
875
-        if (!empty($pid) && !empty($query['post__in']) && count($query['post__in']) > $query['posts_per_page']) {
876
-            $tmp_array = array();
877
-
878
-            if (empty($page_number) || $page_number == 1) {
879
-                for ($i = 0; $i < $query['posts_per_page']; $i++) {
880
-                    $tmp_array[] = $query['post__in'][$i];
881
-                }
882
-            } else {
883
-                $begin_number = (($page_number - 1) * $query['posts_per_page']);
884
-                for ($i = $begin_number; $i < $query['posts_per_page'] + $begin_number; $i++) {
885
-                    if (!empty($query['post__in'][$i])) {
886
-                        $tmp_array[] = $query['post__in'][$i];
887
-                    }
888
-                }
889
-            }
890
-            unset($query['post__in']);
891
-            $query['post__in'] = $tmp_array;
892
-            $query['posts_per_page'] = -1;
893
-        }
894
-
895
-        $query['post_status'] = 'publish';
896
-
897
-        $custom_query = new WP_Query($query);
898
-
899
-        if ($custom_query->have_posts()) {
900
-            $have_results = true;
901
-
902
-            // ---------------- //
903
-            // Products listing //
904
-            // ---------------- //
905
-            $current_position = 1;
906
-            $product_list = '';
907
-            while ($custom_query->have_posts()): $custom_query->the_post();
908
-                $cats = get_the_terms(get_the_ID(), WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
909
-                $cats = !empty($cats) && is_array($cats) ? array_values($cats) : array();
910
-                $cat_id = empty($cats) ? 0 : $cats[0]->term_id;
911
-                $product_list .= self::product_mini_output(get_the_ID(), $cat_id, $display_type, $current_position, $grid_element_nb_per_line);
912
-                $current_position++;
913
-            endwhile;
914
-            $tpl_component = array();
915
-            $tpl_component['PRODUCT_CONTAINER_TYPE_CLASS'] = ($display_type == 'grid' ? ' ' . $display_type . '_' . $grid_element_nb_per_line : '') . ' ' . $display_type . '_mode';
916
-            $tpl_component['PRODUCT_LIST_DISPLAY_TYPE'] = $display_type;
917
-            $tpl_component['PRODUCT_LIST_PER_LINE'] = $grid_element_nb_per_line;
918
-            $tpl_component['PRODUCT_LIST'] = $product_list;
919
-            $tpl_component['CROSSED_OUT_PRICE'] = '';
920
-            $tpl_component['LOW_STOCK_ALERT_MESSAGE'] = '';
921
-            $string = wpshop_display::display_template_element('product_list_container', $tpl_component);
922
-
923
-            // --------------------- //
924
-            // Pagination management //
925
-            // --------------------- //
926
-            if ($display_pagination) {
927
-                if ($nb_of_product_limit == 0) {
928
-
929
-                    $paginate = paginate_links(array(
930
-                        'base' => '%_%',
931
-                        'format' => '/?page_product=%#%',
932
-                        'current' => $page_number,
933
-                        'total' => $custom_query->max_num_pages,
934
-                        'type' => 'array',
935
-                        'prev_next' => false,
936
-                    ));
937
-                    if (!empty($paginate)) {
938
-                        $string .= '<ul class="pagination wps-pagination">';
939
-                        foreach ($paginate as $p) {
940
-                            $string .= '<li>' . $p . '</li>';
941
-                        }
942
-                        $string .= '</ul>';
943
-                    }
944
-                }
945
-
946
-                if (!empty($pid) && !empty($query['post__in']) && count($total_products) > $post_per_page) {
947
-                    $paginate = paginate_links(array(
948
-                        'base' => '%_%',
949
-                        'format' => '/?page_product=%#%',
950
-                        'current' => $page_number,
951
-                        'total' => ceil(count($total_products) / $post_per_page),
952
-                        'type' => 'array',
953
-                        'prev_next' => false,
954
-                    ));
955
-                    if (!empty($paginate)) {
956
-                        $string .= '<ul class="pagination wps-pagination">';
957
-                        foreach ($paginate as $p) {
958
-                            $string .= '<li>' . $p . '</li>';
959
-                        }
960
-                        $string .= '</ul>';
961
-                    }
962
-                }
963
-            }
964
-
965
-        }
966
-        wp_reset_query(); // important
967
-
968
-        return array($have_results, $string);
969
-    }
970
-
971
-    /**
972
-     * Update quantity for a product
973
-     * @param integer $product_id The product we want to update quantity for
974
-     * @param decimal $qty The new quantity
975
-     */
976
-    public static function reduce_product_stock_qty($product_id, $qty, $variation_id = '')
977
-    {
978
-        global $wpdb;
979
-
980
-        $product = self::get_product_data($product_id);
981
-        /** Check if there is variation ***/
982
-        if (!empty($variation_id) && $variation_id != $product_id) {
983
-            $variation_post_type = get_post_type($variation_id);
984
-            if ($variation_post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
985
-                /** Check if variation def is combined **/
986
-                $variations = get_post_meta($product_id, '_wpshop_variation_defining', true);
987
-                if (!empty($variations) && !empty($variations['options']) && !empty($variations['options']['priority']) && in_array('combined', $variations['options']['priority'])) {
988
-                    /** Get post meta of variation */
989
-                    $variation_metadata = get_post_meta($variation_id, '_wpshop_product_metadata', true);
990
-                    if (!empty($variation_metadata) && isset($variation_metadata['product_stock'])) {
991
-                        $product = self::get_product_data($variation_id);
992
-                        $product_id = $variation_id;
993
-                    }
994
-                }
995
-            }
996
-        }
997
-
998
-        if (!empty($product)) {
999
-            $newQty = $product['product_stock'] - $qty;
1000
-            $value_id = 0;
1001
-            if ($newQty >= 0) {
1002
-                $query = '
744
+				$sub_template_part = 'product_listing_sorting_hidden_field';
745
+				$sub_tpl_component = array();
746
+				$sub_tpl_component['DISPLAY_TYPE'] = $type;
747
+				$sub_tpl_component['ORDER'] = $order_by_sorting;
748
+				$sub_tpl_component['PRODUCT_NUMBER'] = $pagination;
749
+				$sub_tpl_component['CURRENT_PAGE'] = 1;
750
+				$sub_tpl_component['CATEGORY_ID'] = $cid;
751
+				$sub_tpl_component['PRODUCT_ID'] = $pid;
752
+				$sub_tpl_component['ATTR'] = $attr;
753
+				$tpl_component['SORTING_HIDDEN_FIELDS'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component, array(), 'admin');
754
+				unset($sub_tpl_component);
755
+
756
+				if ((!empty($sorting_criteria) && is_array($sorting_criteria))) {
757
+					$sub_template_part = 'product_listing_sorting_criteria';
758
+					$sub_tpl_component = array();
759
+					$criteria = '';
760
+					foreach ($sorting_criteria as $c):
761
+						$criteria .= '<option value="' . $c['code'] . '">' . __($c['frontend_label'], 'wpshop') . '</option>';
762
+					endforeach;
763
+					$sub_tpl_component['SORTING_CRITERIA_LIST'] = $criteria;
764
+					$tpl_component['SORTING_CRITERIA'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component);
765
+					unset($sub_tpl_component);
766
+				}
767
+
768
+				if (empty($atts['sorting']) || (!empty($atts['sorting']) && ($atts['sorting'] != 'no'))) {
769
+					$tpl_component['DISPLAY_TYPE_STATE_GRID'] = $type == 'grid' ? ' active' : null;
770
+					$tpl_component['DISPLAY_TYPE_STATE_LIST'] = $type == 'list' ? ' active' : null;
771
+					$sorting = wpshop_display::display_template_element($template_part, $tpl_component);
772
+				} else if (!empty($atts['sorting']) && ($atts['sorting'] == 'no')) {
773
+					$sub_template_part = 'product_listing_sorting_criteria_hidden';
774
+					$sub_tpl_component = array();
775
+					$sub_tpl_component['CRITERIA_DEFAULT'] = !empty($sorting_criteria[0]['code']) ? $sorting_criteria[0]['code'] : 'title';
776
+					$tpl_component['SORTING_CRITERIA'] = wpshop_display::display_template_element($sub_template_part, $sub_tpl_component, array(), 'admin');
777
+					unset($sub_tpl_component);
778
+
779
+					$template_part = 'product_listing_sorting_hidden';
780
+					$sorting = wpshop_display::display_template_element($template_part, $tpl_component, array(), 'admin');
781
+				}
782
+			}
783
+			unset($tpl_component);
784
+
785
+			if (!empty($atts) && !empty($atts['container']) && $atts['container'] == 'no') {
786
+				$string = $sorting . '<div data-nonce="' . wp_create_nonce('products_by_criteria') . '" class="wps-catalog-container wps-bloc-loader">' . $string . '</div>';
787
+			} else {
788
+				$string = '<div class="wpshop_products_block">' . $sorting . '<div data-nonce="' . wp_create_nonce('products_by_criteria') . '" class="wps-catalog-container wps-bloc-loader">' . $string . '</div></div>';
789
+			}
790
+		} else if (empty($atts['no_result_message']) || ($atts['no_result_message'] != 'no')) {
791
+			$string = __('There is nothing to output here', 'wpshop');
792
+		}
793
+
794
+		return do_shortcode($string);
795
+	}
796
+
797
+	public static function wpshop_get_product_by_criteria($criteria = null, $cid = 0, $pid = 0, $display_type, $order = 'ASC', $page_number, $products_per_page = 0, $nb_of_product_limit = 0, $grid_element_nb_per_line = WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE, $display_pagination = true)
798
+	{
799
+		global $wpdb;
800
+
801
+		$string = '<span id="wpshop_loading">&nbsp;</span>';
802
+		$have_results = false;
803
+		$display_type = (!empty($display_type) && in_array($display_type, array('grid', 'list'))) ? $display_type : 'grid';
804
+
805
+		/** Check if Discount are activated */
806
+		$discount_option = get_option('wpshop_catalog_product_option');
807
+
808
+		if ($criteria == 'product_price' && !empty($discount_option) && !empty($discount_option['discount']) && $discount_option['discount'] == 'on') {
809
+			$criteria = 'wpshop_displayed_price';
810
+		}
811
+
812
+		$query = array(
813
+			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
814
+			'order' => $order,
815
+			'posts_per_page' => $products_per_page,
816
+			'paged' => $page_number,
817
+			'post_status' => 'publish',
818
+		);
819
+
820
+		// If the limit is greater than zero, hide pagination and change posts_per_page var
821
+		if ($nb_of_product_limit > 0) {
822
+			$query['posts_per_page'] = $nb_of_product_limit;
823
+			unset($query['paged']);
824
+		}
825
+		if (!empty($pid)) {
826
+			if (!is_array($pid)) {
827
+				$pid = explode(',', $pid);
828
+			}
829
+
830
+			$query['post__in'] = $pid;
831
+		}
832
+		if (!empty($cid)) {
833
+			$cid = explode(',', $cid);
834
+			$query['tax_query'] = array(array(
835
+				'taxonomy' => WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES,
836
+				'field' => 'id',
837
+				'terms' => $cid,
838
+				'operator' => 'IN',
839
+			));
840
+		}
841
+		if ($criteria != null) {
842
+			switch ($criteria) {
843
+				case 'creator':
844
+				case 'author':
845
+					$query['author'] = get_current_user_id();
846
+					break;
847
+				case 'title':
848
+				case 'date':
849
+				case 'modified':
850
+				case 'rand':
851
+					$query['orderby'] = $criteria;
852
+					break;
853
+				default:
854
+					if (!empty($pid)) {
855
+						$post_meta = get_post_meta($pid, '_' . $criteria, true);
856
+					} else {
857
+						$check_meta = $wpdb->prepare("SELECT COUNT(meta_id) as meta_criteria FROM " . $wpdb->postmeta . " WHERE meta_key = %s", '_' . $criteria);
858
+						$post_meta = $wpdb->get_var($check_meta);
859
+					}
860
+					if (!empty($post_meta)) {
861
+						if (in_array($criteria, array('wpshop_displayed_price', 'product_price'))) {
862
+							$query['orderby'] = 'meta_value_num';
863
+						} else {
864
+							$query['orderby'] = 'meta_value';
865
+						}
866
+						$query['meta_key'] = '_' . $criteria;
867
+					}
868
+					break;
869
+			}
870
+		} else {
871
+			$query['orderby'] = 'menu_order ID';
872
+		}
873
+		$post_per_page = $query['posts_per_page'];
874
+		$total_products = (!empty($query['post__in'])) ? $query['post__in'] : 0;
875
+		if (!empty($pid) && !empty($query['post__in']) && count($query['post__in']) > $query['posts_per_page']) {
876
+			$tmp_array = array();
877
+
878
+			if (empty($page_number) || $page_number == 1) {
879
+				for ($i = 0; $i < $query['posts_per_page']; $i++) {
880
+					$tmp_array[] = $query['post__in'][$i];
881
+				}
882
+			} else {
883
+				$begin_number = (($page_number - 1) * $query['posts_per_page']);
884
+				for ($i = $begin_number; $i < $query['posts_per_page'] + $begin_number; $i++) {
885
+					if (!empty($query['post__in'][$i])) {
886
+						$tmp_array[] = $query['post__in'][$i];
887
+					}
888
+				}
889
+			}
890
+			unset($query['post__in']);
891
+			$query['post__in'] = $tmp_array;
892
+			$query['posts_per_page'] = -1;
893
+		}
894
+
895
+		$query['post_status'] = 'publish';
896
+
897
+		$custom_query = new WP_Query($query);
898
+
899
+		if ($custom_query->have_posts()) {
900
+			$have_results = true;
901
+
902
+			// ---------------- //
903
+			// Products listing //
904
+			// ---------------- //
905
+			$current_position = 1;
906
+			$product_list = '';
907
+			while ($custom_query->have_posts()): $custom_query->the_post();
908
+				$cats = get_the_terms(get_the_ID(), WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
909
+				$cats = !empty($cats) && is_array($cats) ? array_values($cats) : array();
910
+				$cat_id = empty($cats) ? 0 : $cats[0]->term_id;
911
+				$product_list .= self::product_mini_output(get_the_ID(), $cat_id, $display_type, $current_position, $grid_element_nb_per_line);
912
+				$current_position++;
913
+			endwhile;
914
+			$tpl_component = array();
915
+			$tpl_component['PRODUCT_CONTAINER_TYPE_CLASS'] = ($display_type == 'grid' ? ' ' . $display_type . '_' . $grid_element_nb_per_line : '') . ' ' . $display_type . '_mode';
916
+			$tpl_component['PRODUCT_LIST_DISPLAY_TYPE'] = $display_type;
917
+			$tpl_component['PRODUCT_LIST_PER_LINE'] = $grid_element_nb_per_line;
918
+			$tpl_component['PRODUCT_LIST'] = $product_list;
919
+			$tpl_component['CROSSED_OUT_PRICE'] = '';
920
+			$tpl_component['LOW_STOCK_ALERT_MESSAGE'] = '';
921
+			$string = wpshop_display::display_template_element('product_list_container', $tpl_component);
922
+
923
+			// --------------------- //
924
+			// Pagination management //
925
+			// --------------------- //
926
+			if ($display_pagination) {
927
+				if ($nb_of_product_limit == 0) {
928
+
929
+					$paginate = paginate_links(array(
930
+						'base' => '%_%',
931
+						'format' => '/?page_product=%#%',
932
+						'current' => $page_number,
933
+						'total' => $custom_query->max_num_pages,
934
+						'type' => 'array',
935
+						'prev_next' => false,
936
+					));
937
+					if (!empty($paginate)) {
938
+						$string .= '<ul class="pagination wps-pagination">';
939
+						foreach ($paginate as $p) {
940
+							$string .= '<li>' . $p . '</li>';
941
+						}
942
+						$string .= '</ul>';
943
+					}
944
+				}
945
+
946
+				if (!empty($pid) && !empty($query['post__in']) && count($total_products) > $post_per_page) {
947
+					$paginate = paginate_links(array(
948
+						'base' => '%_%',
949
+						'format' => '/?page_product=%#%',
950
+						'current' => $page_number,
951
+						'total' => ceil(count($total_products) / $post_per_page),
952
+						'type' => 'array',
953
+						'prev_next' => false,
954
+					));
955
+					if (!empty($paginate)) {
956
+						$string .= '<ul class="pagination wps-pagination">';
957
+						foreach ($paginate as $p) {
958
+							$string .= '<li>' . $p . '</li>';
959
+						}
960
+						$string .= '</ul>';
961
+					}
962
+				}
963
+			}
964
+
965
+		}
966
+		wp_reset_query(); // important
967
+
968
+		return array($have_results, $string);
969
+	}
970
+
971
+	/**
972
+	 * Update quantity for a product
973
+	 * @param integer $product_id The product we want to update quantity for
974
+	 * @param decimal $qty The new quantity
975
+	 */
976
+	public static function reduce_product_stock_qty($product_id, $qty, $variation_id = '')
977
+	{
978
+		global $wpdb;
979
+
980
+		$product = self::get_product_data($product_id);
981
+		/** Check if there is variation ***/
982
+		if (!empty($variation_id) && $variation_id != $product_id) {
983
+			$variation_post_type = get_post_type($variation_id);
984
+			if ($variation_post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
985
+				/** Check if variation def is combined **/
986
+				$variations = get_post_meta($product_id, '_wpshop_variation_defining', true);
987
+				if (!empty($variations) && !empty($variations['options']) && !empty($variations['options']['priority']) && in_array('combined', $variations['options']['priority'])) {
988
+					/** Get post meta of variation */
989
+					$variation_metadata = get_post_meta($variation_id, '_wpshop_product_metadata', true);
990
+					if (!empty($variation_metadata) && isset($variation_metadata['product_stock'])) {
991
+						$product = self::get_product_data($variation_id);
992
+						$product_id = $variation_id;
993
+					}
994
+				}
995
+			}
996
+		}
997
+
998
+		if (!empty($product)) {
999
+			$newQty = $product['product_stock'] - $qty;
1000
+			$value_id = 0;
1001
+			if ($newQty >= 0) {
1002
+				$query = '
1003 1003
 					SELECT wp_wpshop__attribute_value_decimal.value_id
1004 1004
 					FROM wp_wpshop__attribute_value_decimal
1005 1005
 					LEFT JOIN wp_wpshop__attribute ON wp_wpshop__attribute_value_decimal.attribute_id = wp_wpshop__attribute.id
1006 1006
 					WHERE wp_wpshop__attribute_value_decimal.entity_id=' . $product_id . ' AND wp_wpshop__attribute.code="product_stock"
1007 1007
 					LIMIT 1
1008 1008
 				';
1009
-                $value_id = $wpdb->get_var($query);
1010
-                $update = $wpdb->update('wp_wpshop__attribute_value_decimal', array('value' => wpshop_tools::wpshop_clean($newQty)), array('value_id' => $value_id));
1011
-            }
1012
-            // Historic
1013
-            $attribute_histo_content = array();
1014
-            $attribute_histo_content['status'] = 'valid';
1015
-            $attribute_histo_content['creation_date'] = current_time('mysql', 0);
1016
-            $attribute_histo_content['creation_date_value'] = (!empty($attribute_histo[0]->creation_date_value)) ? $attribute_histo[0]->creation_date_value : current_time('mysql', 0);
1017
-            $attribute_histo_content['original_value_id'] = $value_id;
1018
-            $attribute_histo_content['entity_type_id'] = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
1019
-            $stock_atribute = wpshop_attributes::getElement('product_stock', "'valid'", 'code');
1020
-            $attribute_histo_content['attribute_id'] = $stock_atribute->id;
1021
-            $attribute_histo_content['entity_id'] = $product_id;
1022
-            $attribute_histo_content['value'] = $product['product_stock'];
1023
-            $attribute_histo_content['value_type'] = 'wp_wpshop__attribute_value_decimal';
1024
-            $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_HISTO, $attribute_histo_content);
1025
-
1026
-            $product_meta = get_post_meta($product_id, '_wpshop_product_metadata', true);
1027
-            $product_meta['product_stock'] = $newQty;
1028
-            update_post_meta($product_id, '_wpshop_product_metadata', $product_meta);
1029
-        }
1030
-    }
1031
-
1032
-    /**
1033
-     * Retrieve an array with complete information about a given product
1034
-     * @param integer $product_id
1035
-     * @param boolean $for_cart_storage
1036
-     * @return array Information about the product defined by first parameter
1037
-     */
1038
-    public static function get_product_data($product_id, $for_cart_storage = false, $post_status = '"publish"')
1039
-    {
1040
-        global $wpdb;
1041
-        $query = $wpdb->prepare('
1009
+				$value_id = $wpdb->get_var($query);
1010
+				$update = $wpdb->update('wp_wpshop__attribute_value_decimal', array('value' => wpshop_tools::wpshop_clean($newQty)), array('value_id' => $value_id));
1011
+			}
1012
+			// Historic
1013
+			$attribute_histo_content = array();
1014
+			$attribute_histo_content['status'] = 'valid';
1015
+			$attribute_histo_content['creation_date'] = current_time('mysql', 0);
1016
+			$attribute_histo_content['creation_date_value'] = (!empty($attribute_histo[0]->creation_date_value)) ? $attribute_histo[0]->creation_date_value : current_time('mysql', 0);
1017
+			$attribute_histo_content['original_value_id'] = $value_id;
1018
+			$attribute_histo_content['entity_type_id'] = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
1019
+			$stock_atribute = wpshop_attributes::getElement('product_stock', "'valid'", 'code');
1020
+			$attribute_histo_content['attribute_id'] = $stock_atribute->id;
1021
+			$attribute_histo_content['entity_id'] = $product_id;
1022
+			$attribute_histo_content['value'] = $product['product_stock'];
1023
+			$attribute_histo_content['value_type'] = 'wp_wpshop__attribute_value_decimal';
1024
+			$wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_HISTO, $attribute_histo_content);
1025
+
1026
+			$product_meta = get_post_meta($product_id, '_wpshop_product_metadata', true);
1027
+			$product_meta['product_stock'] = $newQty;
1028
+			update_post_meta($product_id, '_wpshop_product_metadata', $product_meta);
1029
+		}
1030
+	}
1031
+
1032
+	/**
1033
+	 * Retrieve an array with complete information about a given product
1034
+	 * @param integer $product_id
1035
+	 * @param boolean $for_cart_storage
1036
+	 * @return array Information about the product defined by first parameter
1037
+	 */
1038
+	public static function get_product_data($product_id, $for_cart_storage = false, $post_status = '"publish"')
1039
+	{
1040
+		global $wpdb;
1041
+		$query = $wpdb->prepare('
1042 1042
 			SELECT P.*, PM.meta_value AS attribute_set_id
1043 1043
 			FROM ' . $wpdb->posts . ' AS P
1044 1044
 				INNER JOIN ' . $wpdb->postmeta . ' AS PM ON (PM.post_id=P.ID)
@@ -1050,398 +1050,398 @@  discard block
 block discarded – undo
1050 1050
 			LIMIT 1
1051 1051
 		', $product_id);
1052 1052
 
1053
-        $product = $wpdb->get_row($query);
1054
-
1055
-        $product_data = array();
1056
-        $product_meta = array();
1057
-
1058
-        if (!empty($product)) {
1059
-            $product_data['product_id'] = (!empty($product->ID)) ? $product->ID : '';
1060
-            $product_data['post_name'] = (!empty($product->post_name)) ? $product->post_name : '';
1061
-            $product_data['product_name'] = (!empty($product->post_title)) ? $product->post_title : '';
1062
-            $product_data['post_title'] = (!empty($product->post_title)) ? $product->post_title : '';
1063
-
1064
-            $product_data['product_author_id'] = (!empty($product->post_author)) ? $product->post_author : '';
1065
-            $product_data['product_date'] = (!empty($product->post_date)) ? $product->post_date : '';
1066
-            $product_data['product_content'] = (!empty($product->post_content)) ? $product->post_content : '';
1067
-            $product_data['product_excerpt'] = (!empty($product->post_excerpt)) ? $product->post_excerpt : wp_trim_words($product_data['product_content'], apply_filters('excerpt_length', 50), '...');
1068
-
1069
-            $product_data['product_meta_attribute_set_id'] = (!empty($product->attribute_set_id)) ? $product->attribute_set_id : '';
1070
-
1071
-            $data = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT), $product->ID, WPSHOP_CURRENT_LOCALE, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
1072
-            if (!empty($data)) {
1073
-                foreach ($data as $attribute) {
1074
-                    $data_type = 'attribute_value_' . $attribute->data_type;
1075
-                    $value = $attribute->$data_type;
1076
-                    if (in_array($attribute->backend_input, array('select', 'multiple-select', 'radio', 'checkbox'))) {
1077
-                        $value = wpshop_attributes::get_attribute_type_select_option_info($value, 'value');
1078
-                    }
1079
-
1080
-                    /** Special traitment regarding attribute_code    */
1081
-                    switch ($attribute->attribute_code) {
1082
-                        case 'product_weight':
1083
-                            $default_weight_unity = get_option('wpshop_shop_default_weight_unity');
1084
-                            if (!empty($default_weight_unity)) {
1085
-                                $query = $wpdb->prepare('SELECT unit FROM ' . WPSHOP_DBT_ATTRIBUTE_UNIT . ' WHERE id = %d', $default_weight_unity);
1086
-                                $unity = $wpdb->get_var($query);
1087
-                                if ($unity == 'kg') {
1088
-                                    $value *= 1000;
1089
-                                }
1090
-
1091
-                            }
1092
-                            break;
1093
-                        default:
1094
-                            $value = !empty($value) ? $value : 0;
1095
-                            break;
1096
-                    }
1097
-                    $product_data[$attribute->attribute_code] = $value;
1098
-
1099
-                    if (!$for_cart_storage or $for_cart_storage && $attribute->is_recordable_in_cart_meta == 'yes') {
1100
-                        $meta = get_post_meta($product->ID, 'attribute_option_' . $attribute->attribute_code, true);
1101
-                        if (!empty($meta)) {
1102
-                            $product_meta[$attribute->attribute_code] = $meta;
1103
-                        }
1104
-                    }
1105
-
1106
-                    if (($attribute->is_visible_in_front == 'yes') && (!in_array($attribute->attribute_code, unserialize(WPSHOP_ATTRIBUTE_PRICES)))) {
1107
-                        $product_meta['attribute_visible'][$attribute->attribute_code] = $value;
1108
-                    }
1109
-                    if (($attribute->is_visible_in_front_listing == 'yes') && (!in_array($attribute->attribute_code, unserialize(WPSHOP_ATTRIBUTE_PRICES)))) {
1110
-                        $product_meta['attribute_visible_listing'][$attribute->attribute_code] = $value;
1111
-                    }
1112
-                }
1113
-            } else {
1114
-
1115
-            }
1116
-
1117
-            /**    Get datas about product options    */
1118
-            if ($product->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
1119
-                $variation_details = get_post_meta($product->ID, '_wpshop_variations_attribute_def', true);
1120
-
1121
-                foreach ($variation_details as $attribute_code => $attribute_value) {
1122
-                    $variation_id = $attribute_value;
1123
-                    $attribute_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
1124
-
1125
-                    $product_meta['variation_definition'][$attribute_code]['UNSTYLED_VALUE'] = stripslashes(wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', $attribute_definition->data_type_to_use, true));
1126
-                    $product_meta['variation_definition'][$attribute_code]['NAME'] = $attribute_definition->frontend_label;
1127
-                    switch ($attribute_definition->backend_input) {
1128
-                        case 'select':
1129
-                        case 'multiple-select':
1130
-                        case 'radio':
1131
-                        case 'checkbox':
1132
-                            $attribute_value = wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', $attribute_definition->data_type_to_use, true);
1133
-                            break;
1134
-                    }
1135
-                    $product_meta['variation_definition'][$attribute_code]['VALUE'] = $attribute_value;
1136
-                    $product_meta['variation_definition'][$attribute_code]['ID'] = $variation_id;
1137
-                }
1138
-            }
1139
-
1140
-            $product_data['item_meta'] = !empty($product_meta) ? $product_meta : array();
1141
-
1142
-            /** Get the display definition for the current product for checking custom display    */
1143
-            $product_data['custom_display'] = get_post_meta($product_id, WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, true);
1144
-        }
1145
-
1146
-        return $product_data;
1147
-    }
1148
-
1149
-    /**
1150
-     * Add a product into the db. This function is used for the EDI
1151
-     * @param $name Name of the product
1152
-     * @param $description Description of the product
1153
-     * @param $attrs List of the attributes and values of the product
1154
-     * @return boolean
1155
-     */
1156
-    public function addProduct($name, $description, $attrs = array())
1157
-    {
1158
-        $new_product = wpshop_entities::create_new_entity(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, $name, $description, $attrs);
1159
-        return $new_product[0];
1160
-    }
1161
-
1162
-    /**
1163
-     * Retrieve a product listing
1164
-     * @param boolean $formated If the output have to be formated or not
1165
-     * @param string $product_search Optionnal Define a search term for request
1166
-     * @return object|string If $formated is set to true will display an html output with all product. Else return a wordpress database object with the product list
1167
-     */
1168
-    public static function product_list($formated = false, $product_search = null)
1169
-    {
1170
-        global $wpdb;
1171
-
1172
-        $query_extra_params = $query_extra_params_value = '';
1173
-        if (!empty($product_search)) {
1174
-            $query_extra_params = " AND post_title LIKE '%%" . $product_search . "%%'";
1175
-            if (is_array($product_search)) {
1176
-                $query_extra_params = " AND ID IN (%s)";
1177
-                $query_extra_params_value = implode(",", $product_search);
1178
-            }
1179
-        }
1180
-
1181
-        $query = $wpdb->prepare("SELECT ID, post_title FROM " . $wpdb->posts . " WHERE post_type=%s AND post_status=%s" . $query_extra_params, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'publish', $query_extra_params_value);
1182
-        $data = $wpdb->get_results($query);
1183
-
1184
-        /*
1053
+		$product = $wpdb->get_row($query);
1054
+
1055
+		$product_data = array();
1056
+		$product_meta = array();
1057
+
1058
+		if (!empty($product)) {
1059
+			$product_data['product_id'] = (!empty($product->ID)) ? $product->ID : '';
1060
+			$product_data['post_name'] = (!empty($product->post_name)) ? $product->post_name : '';
1061
+			$product_data['product_name'] = (!empty($product->post_title)) ? $product->post_title : '';
1062
+			$product_data['post_title'] = (!empty($product->post_title)) ? $product->post_title : '';
1063
+
1064
+			$product_data['product_author_id'] = (!empty($product->post_author)) ? $product->post_author : '';
1065
+			$product_data['product_date'] = (!empty($product->post_date)) ? $product->post_date : '';
1066
+			$product_data['product_content'] = (!empty($product->post_content)) ? $product->post_content : '';
1067
+			$product_data['product_excerpt'] = (!empty($product->post_excerpt)) ? $product->post_excerpt : wp_trim_words($product_data['product_content'], apply_filters('excerpt_length', 50), '...');
1068
+
1069
+			$product_data['product_meta_attribute_set_id'] = (!empty($product->attribute_set_id)) ? $product->attribute_set_id : '';
1070
+
1071
+			$data = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT), $product->ID, WPSHOP_CURRENT_LOCALE, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
1072
+			if (!empty($data)) {
1073
+				foreach ($data as $attribute) {
1074
+					$data_type = 'attribute_value_' . $attribute->data_type;
1075
+					$value = $attribute->$data_type;
1076
+					if (in_array($attribute->backend_input, array('select', 'multiple-select', 'radio', 'checkbox'))) {
1077
+						$value = wpshop_attributes::get_attribute_type_select_option_info($value, 'value');
1078
+					}
1079
+
1080
+					/** Special traitment regarding attribute_code    */
1081
+					switch ($attribute->attribute_code) {
1082
+						case 'product_weight':
1083
+							$default_weight_unity = get_option('wpshop_shop_default_weight_unity');
1084
+							if (!empty($default_weight_unity)) {
1085
+								$query = $wpdb->prepare('SELECT unit FROM ' . WPSHOP_DBT_ATTRIBUTE_UNIT . ' WHERE id = %d', $default_weight_unity);
1086
+								$unity = $wpdb->get_var($query);
1087
+								if ($unity == 'kg') {
1088
+									$value *= 1000;
1089
+								}
1090
+
1091
+							}
1092
+							break;
1093
+						default:
1094
+							$value = !empty($value) ? $value : 0;
1095
+							break;
1096
+					}
1097
+					$product_data[$attribute->attribute_code] = $value;
1098
+
1099
+					if (!$for_cart_storage or $for_cart_storage && $attribute->is_recordable_in_cart_meta == 'yes') {
1100
+						$meta = get_post_meta($product->ID, 'attribute_option_' . $attribute->attribute_code, true);
1101
+						if (!empty($meta)) {
1102
+							$product_meta[$attribute->attribute_code] = $meta;
1103
+						}
1104
+					}
1105
+
1106
+					if (($attribute->is_visible_in_front == 'yes') && (!in_array($attribute->attribute_code, unserialize(WPSHOP_ATTRIBUTE_PRICES)))) {
1107
+						$product_meta['attribute_visible'][$attribute->attribute_code] = $value;
1108
+					}
1109
+					if (($attribute->is_visible_in_front_listing == 'yes') && (!in_array($attribute->attribute_code, unserialize(WPSHOP_ATTRIBUTE_PRICES)))) {
1110
+						$product_meta['attribute_visible_listing'][$attribute->attribute_code] = $value;
1111
+					}
1112
+				}
1113
+			} else {
1114
+
1115
+			}
1116
+
1117
+			/**    Get datas about product options    */
1118
+			if ($product->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
1119
+				$variation_details = get_post_meta($product->ID, '_wpshop_variations_attribute_def', true);
1120
+
1121
+				foreach ($variation_details as $attribute_code => $attribute_value) {
1122
+					$variation_id = $attribute_value;
1123
+					$attribute_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
1124
+
1125
+					$product_meta['variation_definition'][$attribute_code]['UNSTYLED_VALUE'] = stripslashes(wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', $attribute_definition->data_type_to_use, true));
1126
+					$product_meta['variation_definition'][$attribute_code]['NAME'] = $attribute_definition->frontend_label;
1127
+					switch ($attribute_definition->backend_input) {
1128
+						case 'select':
1129
+						case 'multiple-select':
1130
+						case 'radio':
1131
+						case 'checkbox':
1132
+							$attribute_value = wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', $attribute_definition->data_type_to_use, true);
1133
+							break;
1134
+					}
1135
+					$product_meta['variation_definition'][$attribute_code]['VALUE'] = $attribute_value;
1136
+					$product_meta['variation_definition'][$attribute_code]['ID'] = $variation_id;
1137
+				}
1138
+			}
1139
+
1140
+			$product_data['item_meta'] = !empty($product_meta) ? $product_meta : array();
1141
+
1142
+			/** Get the display definition for the current product for checking custom display    */
1143
+			$product_data['custom_display'] = get_post_meta($product_id, WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, true);
1144
+		}
1145
+
1146
+		return $product_data;
1147
+	}
1148
+
1149
+	/**
1150
+	 * Add a product into the db. This function is used for the EDI
1151
+	 * @param $name Name of the product
1152
+	 * @param $description Description of the product
1153
+	 * @param $attrs List of the attributes and values of the product
1154
+	 * @return boolean
1155
+	 */
1156
+	public function addProduct($name, $description, $attrs = array())
1157
+	{
1158
+		$new_product = wpshop_entities::create_new_entity(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, $name, $description, $attrs);
1159
+		return $new_product[0];
1160
+	}
1161
+
1162
+	/**
1163
+	 * Retrieve a product listing
1164
+	 * @param boolean $formated If the output have to be formated or not
1165
+	 * @param string $product_search Optionnal Define a search term for request
1166
+	 * @return object|string If $formated is set to true will display an html output with all product. Else return a wordpress database object with the product list
1167
+	 */
1168
+	public static function product_list($formated = false, $product_search = null)
1169
+	{
1170
+		global $wpdb;
1171
+
1172
+		$query_extra_params = $query_extra_params_value = '';
1173
+		if (!empty($product_search)) {
1174
+			$query_extra_params = " AND post_title LIKE '%%" . $product_search . "%%'";
1175
+			if (is_array($product_search)) {
1176
+				$query_extra_params = " AND ID IN (%s)";
1177
+				$query_extra_params_value = implode(",", $product_search);
1178
+			}
1179
+		}
1180
+
1181
+		$query = $wpdb->prepare("SELECT ID, post_title FROM " . $wpdb->posts . " WHERE post_type=%s AND post_status=%s" . $query_extra_params, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'publish', $query_extra_params_value);
1182
+		$data = $wpdb->get_results($query);
1183
+
1184
+		/*
1185 1185
          * Make some arangement on output if parameter is given
1186 1186
          */
1187
-        if ($formated) {
1188
-            $product_string = '';
1189
-            foreach ($data as $d) {
1190
-                $product_string .= '
1187
+		if ($formated) {
1188
+			$product_string = '';
1189
+			foreach ($data as $d) {
1190
+				$product_string .= '
1191 1191
 					<li class="wpshop_shortcode_element_container wpshop_shortcode_element_container_product" >
1192 1192
 						<input type="checkbox" class="wpshop_shortcode_element wpshop_shortcode_element_product" value="' . $d->ID . '" id="' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '-' . $d->ID . '" name="products[]" /><label for="' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '-' . $d->ID . '" > ' . $d->post_title . '</label>
1193 1193
 					</li>';
1194
-            }
1195
-        }
1196
-
1197
-        return $formated ? $product_string : $data;
1198
-    }
1199
-
1200
-    /**
1201
-     * Enregistrement des données pour le produit
1202
-     */
1203
-    public function save_product_custom_informations($post_id, $data_to_save = array())
1204
-    {
1205
-        global $wpdb;
1206
-
1207
-        $data_to_save = (!empty($data_to_save)) ? $data_to_save : (array) $_REQUEST;
1208
-        // Apply a filter to extra actions
1209
-        $data_to_save = apply_filters('wps_save_product_extra_filter', $data_to_save);
1210
-
1211
-        if (!empty($data_to_save['post_ID']) && ((get_post_type($data_to_save['post_ID']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) || (get_post_type($data_to_save['post_ID']) == "free_product"))) {
1212
-            if (!empty($data_to_save[wpshop_products::currentPageCode . '_attribute'])) {
1213
-                /*    Fill the product reference automatically if nothing is sent    */
1214
-                if (empty($data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'])) {
1215
-                    $query = "SELECT MAX(ID) AS PDCT_ID FROM {$wpdb->posts}";
1216
-                    $last_ref = $wpdb->get_var($query);
1217
-                    $data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'] = WPSHOP_PRODUCT_REFERENCE_PREFIX . str_repeat(0, WPSHOP_PRODUCT_REFERENCE_PREFIX_NB_FILL) . $last_ref;
1218
-                } else {
1219
-                    /* Check if the product reference existing in the database */
1220
-                    $ref = $data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'];
1221
-                    $query = $wpdb->prepare(
1222
-                        "SELECT value_id
1194
+			}
1195
+		}
1196
+
1197
+		return $formated ? $product_string : $data;
1198
+	}
1199
+
1200
+	/**
1201
+	 * Enregistrement des données pour le produit
1202
+	 */
1203
+	public function save_product_custom_informations($post_id, $data_to_save = array())
1204
+	{
1205
+		global $wpdb;
1206
+
1207
+		$data_to_save = (!empty($data_to_save)) ? $data_to_save : (array) $_REQUEST;
1208
+		// Apply a filter to extra actions
1209
+		$data_to_save = apply_filters('wps_save_product_extra_filter', $data_to_save);
1210
+
1211
+		if (!empty($data_to_save['post_ID']) && ((get_post_type($data_to_save['post_ID']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) || (get_post_type($data_to_save['post_ID']) == "free_product"))) {
1212
+			if (!empty($data_to_save[wpshop_products::currentPageCode . '_attribute'])) {
1213
+				/*    Fill the product reference automatically if nothing is sent    */
1214
+				if (empty($data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'])) {
1215
+					$query = "SELECT MAX(ID) AS PDCT_ID FROM {$wpdb->posts}";
1216
+					$last_ref = $wpdb->get_var($query);
1217
+					$data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'] = WPSHOP_PRODUCT_REFERENCE_PREFIX . str_repeat(0, WPSHOP_PRODUCT_REFERENCE_PREFIX_NB_FILL) . $last_ref;
1218
+				} else {
1219
+					/* Check if the product reference existing in the database */
1220
+					$ref = $data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'];
1221
+					$query = $wpdb->prepare(
1222
+						"SELECT value_id
1223 1223
 						FROM " . WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR . "
1224 1224
 							INNER JOIN {$wpdb->posts} ON ( ID = entity_id )
1225 1225
 						WHERE value = %s
1226 1226
 							AND entity_id != %d
1227 1227
 							AND entity_type_id = %d"
1228
-                        , $ref, $data_to_save['post_ID'], wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT));
1229
-                    $existing_reference = $wpdb->get_var($query);
1230
-
1231
-                    /* If this product reference exist -> Create a new product reference */
1232
-                    if ($wpdb->num_rows > 0) {
1233
-                        $query = "SELECT MAX(ID) AS PDCT_ID FROM " . $wpdb->posts;
1234
-                        $last_ref = $wpdb->get_var($query);
1235
-                        $data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'] = WPSHOP_PRODUCT_REFERENCE_PREFIX . str_repeat(0, WPSHOP_PRODUCT_REFERENCE_PREFIX_NB_FILL) . $last_ref;
1236
-                    }
1237
-                }
1238
-
1239
-                /*    Save the attributes values into wpshop eav database    */
1240
-                $update_from = !empty($data_to_save[wpshop_products::currentPageCode . '_provenance']) ? $data_to_save[wpshop_products::currentPageCode . '_provenance'] : '';
1241
-                $lang = WPSHOP_CURRENT_LOCALE;
1242
-                if (!empty($data_to_save['icl_post_language'])) {
1243
-                    $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $data_to_save['icl_post_language']);
1244
-                    $lang = $wpdb->get_var($query);
1245
-                }
1246
-                wpshop_attributes::saveAttributeForEntity($data_to_save[wpshop_products::currentPageCode . '_attribute'], wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $data_to_save['post_ID'], $lang, $update_from);
1247
-
1248
-                /*    Update product price looking for shop parameters    */
1249
-                wpshop_products::calculate_price($data_to_save['post_ID']);
1250
-
1251
-                /*    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
1252
-                $productMetaDatas = array();
1253
-                foreach ($data_to_save[wpshop_products::currentPageCode . '_attribute'] as $attributeType => $attributeValues) {
1254
-                    foreach ($attributeValues as $attributeCode => $attributeValue) {
1255
-                        if ($attributeCode == 'product_attribute_set_id') {
1256
-                            /*    Update the attribute set id for the current product    */
1257
-                            update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, $attributeValue);
1258
-                        }
1259
-                        if ($attributeType == 'decimal') {
1260
-                            $attributeValue = str_replace(',', '.', $attributeValue);
1261
-                        }
1262
-                        if (($attributeType == 'integer') && !is_array($attributeValue)) {
1263
-                            $attributeValue = (int) $attributeValue;
1264
-                        }
1265
-                        $productMetaDatas[$attributeCode] = $attributeValue;
1266
-                    }
1267
-                }
1268
-                update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
1269
-            }
1270
-
1271
-            if (!empty($data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']) && empty($data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']['default_config'])) {
1272
-                update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, $data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']);
1273
-            } else if ($data_to_save['action'] != 'autosave') {
1274
-                delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_FRONT_DISPLAY_CONF);
1275
-            }
1276
-
1277
-            $product = wpshop_products::get_product_data($data_to_save['post_ID'], false, '"publish"');
1278
-            if (empty($product['product_id'])) {
1279
-                $product['product_id'] = $data_to_save['post_ID'];
1280
-            }
1281
-            $price = wpshop_prices::get_product_price($product, 'just_price_infos', array('mini_output', 'grid'));
1282
-            update_post_meta($data_to_save['post_ID'], '_wps_price_infos', $price);
1283
-
1284
-            /**    Save product variation    */
1285
-            if (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION])) {
1286
-                foreach ($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION] as $variation_id => $variation_definition) {
1287
-
1288
-                    foreach (unserialize(WPSHOP_ATTRIBUTE_PRICES) as $price_attribute_code) {
1289
-                        $price_attr_def = wpshop_attributes::getElement($price_attribute_code, "'valid'", 'code');
1290
-                        if (!empty($variation_definition) && !empty($variation_definition['attribute']) && is_object($price_attr_def) && !empty($variation_definition['attribute'][$price_attr_def->data_type]) && is_array($variation_definition['attribute'][$price_attr_def->data_type]) && !array_key_exists($price_attribute_code, $variation_definition['attribute'][$price_attr_def->data_type])) {
1291
-                            $variation_definition['attribute'][$price_attr_def->data_type][$price_attribute_code] = !empty($data_to_save[wpshop_products::currentPageCode . '_attribute'][$price_attr_def->data_type][$price_attribute_code]) ? $data_to_save[wpshop_products::currentPageCode . '_attribute'][$price_attr_def->data_type][$price_attribute_code] : 0;
1292
-                        }
1293
-                    }
1294
-                    $lang = WPSHOP_CURRENT_LOCALE;
1295
-                    if (!empty($data_to_save['icl_post_language'])) {
1296
-                        $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $data_to_save['icl_post_language']);
1297
-                        $lang = $wpdb->get_var($query);
1298
-                    }
1299
-                    wpshop_attributes::saveAttributeForEntity($variation_definition['attribute'], wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $variation_id, $lang);
1300
-
1301
-                    /**    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
1302
-                    $variation_metadata = get_post_meta($variation_id, '_wpshop_product_metadata', true);
1303
-                    if (!empty($variation_metadata)) {
1304
-                        $attributes_list = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION), $variation_id);
1305
-                        if (!empty($attributes_list)) {
1306
-                            foreach ($attributes_list as $attribute) {
1307
-                                $value_key = 'attribute_value_' . $attribute->data_type;
1308
-                                $attributeValue = $attribute->$value_key;
1309
-                                if ($attribute->data_type == 'decimal') {
1310
-                                    $attributeValue = str_replace(',', '.', $attribute->$value_key);
1311
-                                }
1312
-                                if (($attribute->data_type == 'integer') && !is_array($attributeValue)) {
1313
-                                    $attributeValue = (int) $attribute->$value_key;
1314
-                                }
1315
-                                $variation_metadata[$attribute->code] = $attribute->$value_key;
1316
-                            }
1317
-                        }
1318
-                    }
1319
-
1320
-                    foreach ($variation_definition['attribute'] as $attributeType => $attributeValues) {
1321
-                        foreach ($attributeValues as $attributeCode => $attributeValue) {
1322
-                            if ($attributeType == 'decimal') {
1323
-                                $attributeValue = str_replace(',', '.', $attributeValue);
1324
-                            }
1325
-                            if (($attributeType == 'integer') && !is_array($attributeValue)) {
1326
-                                $attributeValue = (int) $attributeValue;
1327
-                            }
1328
-                            $variation_metadata[$attributeCode] = $attributeValue;
1329
-                        }
1330
-                    }
1331
-                    update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $variation_metadata);
1332
-
1333
-                    /** Save attached picture id **/
1334
-                    $attached_picture_id = (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION][$variation_id]['wps_attached_picture_id'])) ? intval($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION][$variation_id]['wps_attached_picture_id']) : '';
1335
-                    $selected_picture = update_post_meta($variation_id, '_wps_variation_attached_picture', $attached_picture_id);
1336
-
1337
-                    /*    Update product price looking for shop parameters    */
1338
-                    wpshop_products::calculate_price($variation_id);
1339
-                }
1340
-            }
1341
-
1342
-            /*    Update the related products list*/
1343
-            if (!empty($data_to_save['related_products_list'])) {
1344
-                update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_RELATED_PRODUCTS, $data_to_save['related_products_list']);
1345
-            } else if ($data_to_save['action'] != 'autosave') {
1346
-                delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_RELATED_PRODUCTS);
1347
-            }
1348
-            /*    Update the provider list*/
1349
-            if (!empty($data_to_save['provider_list'])) {
1350
-                update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_PROVIDER, $data_to_save['provider_list']);
1351
-            } else if ($data_to_save['action'] != 'autosave') {
1352
-                delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_PROVIDER);
1353
-            }
1354
-
1355
-            /*    Update product options    */
1356
-            if (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT]['options'])) {
1357
-                update_post_meta($data_to_save['post_ID'], '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options', $data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT]['options']);
1358
-            } else if ($data_to_save['action'] != 'autosave') {
1359
-                delete_post_meta($data_to_save['post_ID'], '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options');
1360
-            }
1361
-
1362
-            /** Add flag on variations to check which type of variation **/
1363
-            $check_product_have_variations = self::get_variation($post_id);
1364
-            if (!empty($check_product_have_variations)) {
1365
-                $variation_flag = self::check_variation_type($post_id);
1366
-                $variation_defining = get_post_meta($post_id, '_wpshop_variation_defining', true);
1367
-                $variation_defining['variation_type'] = $variation_flag;
1368
-                update_post_meta($post_id, '_wpshop_variation_defining', $variation_defining);
1369
-            }
1370
-            add_post_meta($post_id, '_wpshop_variation_defining', array('follow_general_config' => true, 'options' => array('priority' => array('combined'), 'price_behaviour' => array('replacement'), 'price_display' => (!empty($catalog_product_option) && !empty($catalog_product_option['price_display'])) ? $catalog_product_option['price_display'] : array('text_from' => 'on', 'lower_price' => 'on'))), true);
1371
-        }
1372
-
1373
-        flush_rewrite_rules();
1374
-    }
1375
-
1376
-    /**
1377
-     * Return the variation type
1378
-     * @param integer $post_id
1379
-     * @return string
1380
-     */
1381
-    public function check_variation_type($post_id)
1382
-    {
1383
-        $variation_type = 'single';
1384
-        if (!empty($variation_type)) {
1385
-            $variations = self::get_variation($post_id);
1386
-            if (!empty($variations)) {
1387
-                foreach ($variations as $variation_id => $variation_data) {
1388
-                    if (!empty($variation_data) && !empty($variation_data['variation_def']) && count($variation_data['variation_def']) == 1) {
1389
-                        return 'single';
1390
-                    } elseif (!empty($variation_data) && !empty($variation_data['variation_def']) && count($variation_data['variation_def']) > 1) {
1391
-                        $variation_type = 'combined';
1392
-                    }
1393
-                }
1394
-            }
1395
-        }
1396
-        return $variation_type;
1397
-    }
1398
-
1399
-    /**
1400
-     * Allows to define a specific permalink for each product by checking the parent categories
1401
-     *
1402
-     * @param mixed $permalink The actual permalink of the element
1403
-     * @param object $post The post we want to set the permalink for
1404
-     * @param void
1405
-     *
1406
-     * @return mixed The new permalink for the current element
1407
-     */
1408
-    public static function set_product_permalink($permalink, $post, $unknown)
1409
-    {
1410
-        $options = get_option('wpshop_catalog_product_option', array());
1411
-
1412
-        if ($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT/* && 'publish' == $post->post_status */) {
1413
-            if (!isset($options['wpshop_catalog_product_slug_with_category']) || $options['wpshop_catalog_product_slug_with_category'] != 'yes') {
1414
-                return $permalink;
1415
-            } else {
1416
-                $options_cats = get_option('wpshop_catalog_categories_option', array());
1417
-                $product_categories = wp_get_object_terms($post->ID, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
1418
-                if (empty($product_categories)) {
1419
-                    $product_category_slug = $options_cats['wpshop_catalog_no_category_slug'];
1420
-                } else {
1421
-                    $product_category_slug = $product_categories[0]->slug;
1422
-                }
1423
-                $permalink = str_replace('%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%', $product_category_slug, $permalink);
1424
-            }
1425
-        }
1426
-
1427
-        return $permalink;
1428
-
1429
-        //echo '<pre>'; print_r($permalink); echo '</pre>'; exit();
1430
-
1431
-        //wpshop_catalog_categories_slug wpshop_catalog_no_category_slug
1432
-
1433
-        /*if(false) {
1228
+						, $ref, $data_to_save['post_ID'], wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT));
1229
+					$existing_reference = $wpdb->get_var($query);
1230
+
1231
+					/* If this product reference exist -> Create a new product reference */
1232
+					if ($wpdb->num_rows > 0) {
1233
+						$query = "SELECT MAX(ID) AS PDCT_ID FROM " . $wpdb->posts;
1234
+						$last_ref = $wpdb->get_var($query);
1235
+						$data_to_save[wpshop_products::currentPageCode . '_attribute']['varchar']['product_reference'] = WPSHOP_PRODUCT_REFERENCE_PREFIX . str_repeat(0, WPSHOP_PRODUCT_REFERENCE_PREFIX_NB_FILL) . $last_ref;
1236
+					}
1237
+				}
1238
+
1239
+				/*    Save the attributes values into wpshop eav database    */
1240
+				$update_from = !empty($data_to_save[wpshop_products::currentPageCode . '_provenance']) ? $data_to_save[wpshop_products::currentPageCode . '_provenance'] : '';
1241
+				$lang = WPSHOP_CURRENT_LOCALE;
1242
+				if (!empty($data_to_save['icl_post_language'])) {
1243
+					$query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $data_to_save['icl_post_language']);
1244
+					$lang = $wpdb->get_var($query);
1245
+				}
1246
+				wpshop_attributes::saveAttributeForEntity($data_to_save[wpshop_products::currentPageCode . '_attribute'], wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $data_to_save['post_ID'], $lang, $update_from);
1247
+
1248
+				/*    Update product price looking for shop parameters    */
1249
+				wpshop_products::calculate_price($data_to_save['post_ID']);
1250
+
1251
+				/*    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
1252
+				$productMetaDatas = array();
1253
+				foreach ($data_to_save[wpshop_products::currentPageCode . '_attribute'] as $attributeType => $attributeValues) {
1254
+					foreach ($attributeValues as $attributeCode => $attributeValue) {
1255
+						if ($attributeCode == 'product_attribute_set_id') {
1256
+							/*    Update the attribute set id for the current product    */
1257
+							update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, $attributeValue);
1258
+						}
1259
+						if ($attributeType == 'decimal') {
1260
+							$attributeValue = str_replace(',', '.', $attributeValue);
1261
+						}
1262
+						if (($attributeType == 'integer') && !is_array($attributeValue)) {
1263
+							$attributeValue = (int) $attributeValue;
1264
+						}
1265
+						$productMetaDatas[$attributeCode] = $attributeValue;
1266
+					}
1267
+				}
1268
+				update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
1269
+			}
1270
+
1271
+			if (!empty($data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']) && empty($data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']['default_config'])) {
1272
+				update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_FRONT_DISPLAY_CONF, $data_to_save[wpshop_products::currentPageCode . '_attr_frontend_display']);
1273
+			} else if ($data_to_save['action'] != 'autosave') {
1274
+				delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_FRONT_DISPLAY_CONF);
1275
+			}
1276
+
1277
+			$product = wpshop_products::get_product_data($data_to_save['post_ID'], false, '"publish"');
1278
+			if (empty($product['product_id'])) {
1279
+				$product['product_id'] = $data_to_save['post_ID'];
1280
+			}
1281
+			$price = wpshop_prices::get_product_price($product, 'just_price_infos', array('mini_output', 'grid'));
1282
+			update_post_meta($data_to_save['post_ID'], '_wps_price_infos', $price);
1283
+
1284
+			/**    Save product variation    */
1285
+			if (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION])) {
1286
+				foreach ($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION] as $variation_id => $variation_definition) {
1287
+
1288
+					foreach (unserialize(WPSHOP_ATTRIBUTE_PRICES) as $price_attribute_code) {
1289
+						$price_attr_def = wpshop_attributes::getElement($price_attribute_code, "'valid'", 'code');
1290
+						if (!empty($variation_definition) && !empty($variation_definition['attribute']) && is_object($price_attr_def) && !empty($variation_definition['attribute'][$price_attr_def->data_type]) && is_array($variation_definition['attribute'][$price_attr_def->data_type]) && !array_key_exists($price_attribute_code, $variation_definition['attribute'][$price_attr_def->data_type])) {
1291
+							$variation_definition['attribute'][$price_attr_def->data_type][$price_attribute_code] = !empty($data_to_save[wpshop_products::currentPageCode . '_attribute'][$price_attr_def->data_type][$price_attribute_code]) ? $data_to_save[wpshop_products::currentPageCode . '_attribute'][$price_attr_def->data_type][$price_attribute_code] : 0;
1292
+						}
1293
+					}
1294
+					$lang = WPSHOP_CURRENT_LOCALE;
1295
+					if (!empty($data_to_save['icl_post_language'])) {
1296
+						$query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $data_to_save['icl_post_language']);
1297
+						$lang = $wpdb->get_var($query);
1298
+					}
1299
+					wpshop_attributes::saveAttributeForEntity($variation_definition['attribute'], wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $variation_id, $lang);
1300
+
1301
+					/**    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
1302
+					$variation_metadata = get_post_meta($variation_id, '_wpshop_product_metadata', true);
1303
+					if (!empty($variation_metadata)) {
1304
+						$attributes_list = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION), $variation_id);
1305
+						if (!empty($attributes_list)) {
1306
+							foreach ($attributes_list as $attribute) {
1307
+								$value_key = 'attribute_value_' . $attribute->data_type;
1308
+								$attributeValue = $attribute->$value_key;
1309
+								if ($attribute->data_type == 'decimal') {
1310
+									$attributeValue = str_replace(',', '.', $attribute->$value_key);
1311
+								}
1312
+								if (($attribute->data_type == 'integer') && !is_array($attributeValue)) {
1313
+									$attributeValue = (int) $attribute->$value_key;
1314
+								}
1315
+								$variation_metadata[$attribute->code] = $attribute->$value_key;
1316
+							}
1317
+						}
1318
+					}
1319
+
1320
+					foreach ($variation_definition['attribute'] as $attributeType => $attributeValues) {
1321
+						foreach ($attributeValues as $attributeCode => $attributeValue) {
1322
+							if ($attributeType == 'decimal') {
1323
+								$attributeValue = str_replace(',', '.', $attributeValue);
1324
+							}
1325
+							if (($attributeType == 'integer') && !is_array($attributeValue)) {
1326
+								$attributeValue = (int) $attributeValue;
1327
+							}
1328
+							$variation_metadata[$attributeCode] = $attributeValue;
1329
+						}
1330
+					}
1331
+					update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $variation_metadata);
1332
+
1333
+					/** Save attached picture id **/
1334
+					$attached_picture_id = (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION][$variation_id]['wps_attached_picture_id'])) ? intval($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION][$variation_id]['wps_attached_picture_id']) : '';
1335
+					$selected_picture = update_post_meta($variation_id, '_wps_variation_attached_picture', $attached_picture_id);
1336
+
1337
+					/*    Update product price looking for shop parameters    */
1338
+					wpshop_products::calculate_price($variation_id);
1339
+				}
1340
+			}
1341
+
1342
+			/*    Update the related products list*/
1343
+			if (!empty($data_to_save['related_products_list'])) {
1344
+				update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_RELATED_PRODUCTS, $data_to_save['related_products_list']);
1345
+			} else if ($data_to_save['action'] != 'autosave') {
1346
+				delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_RELATED_PRODUCTS);
1347
+			}
1348
+			/*    Update the provider list*/
1349
+			if (!empty($data_to_save['provider_list'])) {
1350
+				update_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_PROVIDER, $data_to_save['provider_list']);
1351
+			} else if ($data_to_save['action'] != 'autosave') {
1352
+				delete_post_meta($data_to_save['post_ID'], WPSHOP_PRODUCT_PROVIDER);
1353
+			}
1354
+
1355
+			/*    Update product options    */
1356
+			if (!empty($data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT]['options'])) {
1357
+				update_post_meta($data_to_save['post_ID'], '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options', $data_to_save[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT]['options']);
1358
+			} else if ($data_to_save['action'] != 'autosave') {
1359
+				delete_post_meta($data_to_save['post_ID'], '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options');
1360
+			}
1361
+
1362
+			/** Add flag on variations to check which type of variation **/
1363
+			$check_product_have_variations = self::get_variation($post_id);
1364
+			if (!empty($check_product_have_variations)) {
1365
+				$variation_flag = self::check_variation_type($post_id);
1366
+				$variation_defining = get_post_meta($post_id, '_wpshop_variation_defining', true);
1367
+				$variation_defining['variation_type'] = $variation_flag;
1368
+				update_post_meta($post_id, '_wpshop_variation_defining', $variation_defining);
1369
+			}
1370
+			add_post_meta($post_id, '_wpshop_variation_defining', array('follow_general_config' => true, 'options' => array('priority' => array('combined'), 'price_behaviour' => array('replacement'), 'price_display' => (!empty($catalog_product_option) && !empty($catalog_product_option['price_display'])) ? $catalog_product_option['price_display'] : array('text_from' => 'on', 'lower_price' => 'on'))), true);
1371
+		}
1372
+
1373
+		flush_rewrite_rules();
1374
+	}
1375
+
1376
+	/**
1377
+	 * Return the variation type
1378
+	 * @param integer $post_id
1379
+	 * @return string
1380
+	 */
1381
+	public function check_variation_type($post_id)
1382
+	{
1383
+		$variation_type = 'single';
1384
+		if (!empty($variation_type)) {
1385
+			$variations = self::get_variation($post_id);
1386
+			if (!empty($variations)) {
1387
+				foreach ($variations as $variation_id => $variation_data) {
1388
+					if (!empty($variation_data) && !empty($variation_data['variation_def']) && count($variation_data['variation_def']) == 1) {
1389
+						return 'single';
1390
+					} elseif (!empty($variation_data) && !empty($variation_data['variation_def']) && count($variation_data['variation_def']) > 1) {
1391
+						$variation_type = 'combined';
1392
+					}
1393
+				}
1394
+			}
1395
+		}
1396
+		return $variation_type;
1397
+	}
1398
+
1399
+	/**
1400
+	 * Allows to define a specific permalink for each product by checking the parent categories
1401
+	 *
1402
+	 * @param mixed $permalink The actual permalink of the element
1403
+	 * @param object $post The post we want to set the permalink for
1404
+	 * @param void
1405
+	 *
1406
+	 * @return mixed The new permalink for the current element
1407
+	 */
1408
+	public static function set_product_permalink($permalink, $post, $unknown)
1409
+	{
1410
+		$options = get_option('wpshop_catalog_product_option', array());
1411
+
1412
+		if ($post->post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT/* && 'publish' == $post->post_status */) {
1413
+			if (!isset($options['wpshop_catalog_product_slug_with_category']) || $options['wpshop_catalog_product_slug_with_category'] != 'yes') {
1414
+				return $permalink;
1415
+			} else {
1416
+				$options_cats = get_option('wpshop_catalog_categories_option', array());
1417
+				$product_categories = wp_get_object_terms($post->ID, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
1418
+				if (empty($product_categories)) {
1419
+					$product_category_slug = $options_cats['wpshop_catalog_no_category_slug'];
1420
+				} else {
1421
+					$product_category_slug = $product_categories[0]->slug;
1422
+				}
1423
+				$permalink = str_replace('%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%', $product_category_slug, $permalink);
1424
+			}
1425
+		}
1426
+
1427
+		return $permalink;
1428
+
1429
+		//echo '<pre>'; print_r($permalink); echo '</pre>'; exit();
1430
+
1431
+		//wpshop_catalog_categories_slug wpshop_catalog_no_category_slug
1432
+
1433
+		/*if(false) {
1434 1434
         global $wp_query;
1435 1435
         $product_categories = wp_get_object_terms( $post->ID, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES );
1436 1436
 
1437 1437
         if(count($product_categories) == 0){            /*    Product has only one category we get the only available slug    */
1438
-        /*        $product_category_slug = WPSHOP_UNCATEGORIZED_PRODUCT_SLUG;
1438
+		/*        $product_category_slug = WPSHOP_UNCATEGORIZED_PRODUCT_SLUG;
1439 1439
         }
1440 1440
         elseif(count($product_categories) == 1){    /*    Product has only one category we get the only available slug    */
1441
-        /*        $product_category_slug = $product_categories[0]->slug;
1441
+		/*        $product_category_slug = $product_categories[0]->slug;
1442 1442
         }
1443 1443
         else{                                                                            /*    Product has several categories choose the slug of the we want    */
1444
-        /*        $product_category_slugs = array();
1444
+		/*        $product_category_slugs = array();
1445 1445
     foreach($product_categories as $product_category){
1446 1446
     $product_category_slugs[] = $product_category->slug;
1447 1447
     }
@@ -1451,452 +1451,452 @@  discard block
 block discarded – undo
1451 1451
     $permalink = str_replace('%' . WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '%', $product_category_slug, $permalink);
1452 1452
     return apply_filters('wpshop_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_permalink', $permalink, $post->ID );
1453 1453
     }*/
1454
-    }
1455
-
1456
-    public static function set_product_request_by_name($query)
1457
-    {
1458
-        $options = get_option('wpshop_catalog_product_option', array());
1459
-
1460
-        if ($query->is_main_query() && (2 == count($query->query) || isset($query->query['page'])) && !empty($query->query['name']) && empty($options['wpshop_catalog_product_slug']));
1461
-        //    $query->set( 'post_type', array( 'post', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'page' ) );
1462
-    }
1463
-
1464
-    /**
1465
-     *    Define output for product
1466
-     *
1467
-     *    @param mixed $initialContent The initial product content defined into wordpress basic admin interface
1468
-     *    @param integer $product_id The product identifier we want to get and output attribute for
1469
-     *
1470
-     *    @return mixed $content The content to add or to modify the product output in frontend
1471
-     */
1472
-    public static function product_complete_sheet_output($initialContent, $product_id)
1473
-    {
1474
-        $content = $attributeContentOutput = '';
1475
-        $wps_product_ctr = new wps_product_ctr();
1476
-        /** Log number of view for the current product    */
1477
-        $product_view_number = get_post_meta($product_id, WPSHOP_PRODUCT_VIEW_NB, true);
1478
-        $product_view_number++;
1479
-        update_post_meta($product_id, WPSHOP_PRODUCT_VIEW_NB, $product_view_number);
1480
-
1481
-        /** Get product definition    */
1482
-
1483
-        $product = self::get_product_data($product_id);
1484
-        wp_cache_set('wpshop_product_data_' . $product_id, $product);
1485
-
1486
-        /** Get the product thumbnail    */
1487
-        $productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1488
-        if (has_post_thumbnail($product_id)) {
1489
-            $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
1490
-            $tpl_component = array();
1491
-            $tpl_component['PRODUCT_THUMBNAIL_URL'] = $thumbnail_url[0];
1492
-            $tpl_component['PRODUCT_THUMBNAIL'] = get_the_post_thumbnail($product_id, 'wpshop-product-galery');
1493
-            $tpl_component['PRODUCT_THUMBNAIL_FULL'] = get_the_post_thumbnail($product_id, 'full');
1494
-            $image_attributes = wp_get_attachment_metadata(get_post_thumbnail_id());
1495
-            if (!empty($image_attributes) && !empty($image_attributes['sizes']) && is_array($image_attributes['sizes'])) {
1496
-                foreach ($image_attributes['sizes'] as $size_name => $size_def) {
1497
-                    $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = wp_get_attachment_image(get_post_thumbnail_id(), $size_name);
1498
-                    $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = (!empty($tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)])) ? $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1499
-                }
1500
-            }
1501
-            $tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] = (!empty($tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'])) ? $tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1502
-            $productThumbnail = wpshop_display::display_template_element('product_thumbnail', $tpl_component);
1503
-            unset($tpl_component);
1504
-        }
1505
-
1506
-        $product_document_galery = wps_media_manager_frontend_ctr::get_product_complete_sheet_attachments($product_id);
1507
-
1508
-        /**    Retrieve product attributes for output    */
1509
-        $attributeContentOutput = wpshop_attributes::attribute_of_entity_to_tab(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $product_id, $product);
1510
-
1511
-        /** Retrieve product price */
1512
-        $price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
1513
-        $price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front, $product['custom_display'], 'attribute', 'product_price', 'complete_sheet');
1514
-        $catalog_options = get_option('wpshop_catalog_main_option', array());
1515
-        $productPrice = '';
1516
-        $wpshop_price_piloting_option = get_option('wpshop_shop_price_piloting');
1517
-        $check_product_price = wpshop_prices::check_product_price($product);
1518
-        $result_price_piloting = (!empty($wpshop_price_piloting_option) && $wpshop_price_piloting_option == 'HT') ? $check_product_price['et'] : $check_product_price['ati'];
1519
-        if ($price_display && !(!empty($catalog_options) && (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes') && $result_price_piloting == 0)) {
1520
-            $productPrice = wpshop_prices::get_product_price($product, 'price_display', 'complete_sheet');
1521
-        }
1522
-
1523
-        /** Check if there is at less 1 product in stock    */
1524
-        $productStock = $wps_product_ctr->check_stock($product_id, 1);
1525
-        $productStock = ($productStock === true) ? 1 : null;
1526
-
1527
-        /** if !product stock check product have variation with stock **/
1454
+	}
1455
+
1456
+	public static function set_product_request_by_name($query)
1457
+	{
1458
+		$options = get_option('wpshop_catalog_product_option', array());
1459
+
1460
+		if ($query->is_main_query() && (2 == count($query->query) || isset($query->query['page'])) && !empty($query->query['name']) && empty($options['wpshop_catalog_product_slug']));
1461
+		//    $query->set( 'post_type', array( 'post', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'page' ) );
1462
+	}
1463
+
1464
+	/**
1465
+	 *    Define output for product
1466
+	 *
1467
+	 *    @param mixed $initialContent The initial product content defined into wordpress basic admin interface
1468
+	 *    @param integer $product_id The product identifier we want to get and output attribute for
1469
+	 *
1470
+	 *    @return mixed $content The content to add or to modify the product output in frontend
1471
+	 */
1472
+	public static function product_complete_sheet_output($initialContent, $product_id)
1473
+	{
1474
+		$content = $attributeContentOutput = '';
1475
+		$wps_product_ctr = new wps_product_ctr();
1476
+		/** Log number of view for the current product    */
1477
+		$product_view_number = get_post_meta($product_id, WPSHOP_PRODUCT_VIEW_NB, true);
1478
+		$product_view_number++;
1479
+		update_post_meta($product_id, WPSHOP_PRODUCT_VIEW_NB, $product_view_number);
1480
+
1481
+		/** Get product definition    */
1482
+
1483
+		$product = self::get_product_data($product_id);
1484
+		wp_cache_set('wpshop_product_data_' . $product_id, $product);
1485
+
1486
+		/** Get the product thumbnail    */
1487
+		$productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1488
+		if (has_post_thumbnail($product_id)) {
1489
+			$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
1490
+			$tpl_component = array();
1491
+			$tpl_component['PRODUCT_THUMBNAIL_URL'] = $thumbnail_url[0];
1492
+			$tpl_component['PRODUCT_THUMBNAIL'] = get_the_post_thumbnail($product_id, 'wpshop-product-galery');
1493
+			$tpl_component['PRODUCT_THUMBNAIL_FULL'] = get_the_post_thumbnail($product_id, 'full');
1494
+			$image_attributes = wp_get_attachment_metadata(get_post_thumbnail_id());
1495
+			if (!empty($image_attributes) && !empty($image_attributes['sizes']) && is_array($image_attributes['sizes'])) {
1496
+				foreach ($image_attributes['sizes'] as $size_name => $size_def) {
1497
+					$tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = wp_get_attachment_image(get_post_thumbnail_id(), $size_name);
1498
+					$tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = (!empty($tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)])) ? $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1499
+				}
1500
+			}
1501
+			$tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] = (!empty($tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'])) ? $tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1502
+			$productThumbnail = wpshop_display::display_template_element('product_thumbnail', $tpl_component);
1503
+			unset($tpl_component);
1504
+		}
1505
+
1506
+		$product_document_galery = wps_media_manager_frontend_ctr::get_product_complete_sheet_attachments($product_id);
1507
+
1508
+		/**    Retrieve product attributes for output    */
1509
+		$attributeContentOutput = wpshop_attributes::attribute_of_entity_to_tab(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $product_id, $product);
1510
+
1511
+		/** Retrieve product price */
1512
+		$price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
1513
+		$price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front, $product['custom_display'], 'attribute', 'product_price', 'complete_sheet');
1514
+		$catalog_options = get_option('wpshop_catalog_main_option', array());
1515
+		$productPrice = '';
1516
+		$wpshop_price_piloting_option = get_option('wpshop_shop_price_piloting');
1517
+		$check_product_price = wpshop_prices::check_product_price($product);
1518
+		$result_price_piloting = (!empty($wpshop_price_piloting_option) && $wpshop_price_piloting_option == 'HT') ? $check_product_price['et'] : $check_product_price['ati'];
1519
+		if ($price_display && !(!empty($catalog_options) && (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes') && $result_price_piloting == 0)) {
1520
+			$productPrice = wpshop_prices::get_product_price($product, 'price_display', 'complete_sheet');
1521
+		}
1522
+
1523
+		/** Check if there is at less 1 product in stock    */
1524
+		$productStock = $wps_product_ctr->check_stock($product_id, 1);
1525
+		$productStock = ($productStock === true) ? 1 : null;
1526
+
1527
+		/** if !product stock check product have variation with stock **/
1528 1528
 //         if ( empty($productStock) ) {
1529
-        $product_variation_meta = get_post_meta($product_id, '_wpshop_variation_defining', true);
1530
-        if (!empty($product_variation_meta) && !empty($product_variation_meta['options']) && !empty($product_variation_meta['options']['priority']) && in_array('combined', $product_variation_meta['options']['priority'])) {
1531
-            $variations = wpshop_products::get_variation($product_id);
1532
-            if (!empty($variations)) {
1533
-                foreach ($variations as $variation) {
1534
-                    if (!empty($variation) && !empty($variation['variation_dif']) && array_key_exists('product_stock', $variation['variation_dif']) && round($variation['variation_dif']['product_stock']) > 0) {
1535
-                        $productStock = 1;
1536
-                    }
1537
-                }
1538
-            }
1539
-        }
1529
+		$product_variation_meta = get_post_meta($product_id, '_wpshop_variation_defining', true);
1530
+		if (!empty($product_variation_meta) && !empty($product_variation_meta['options']) && !empty($product_variation_meta['options']['priority']) && in_array('combined', $product_variation_meta['options']['priority'])) {
1531
+			$variations = wpshop_products::get_variation($product_id);
1532
+			if (!empty($variations)) {
1533
+				foreach ($variations as $variation) {
1534
+					if (!empty($variation) && !empty($variation['variation_dif']) && array_key_exists('product_stock', $variation['variation_dif']) && round($variation['variation_dif']['product_stock']) > 0) {
1535
+						$productStock = 1;
1536
+					}
1537
+				}
1538
+			}
1539
+		}
1540 1540
 //         }
1541 1541
 
1542
-        /** Define "Add to cart" button     */
1543
-        $add_to_cart_button_display_state = wpshop_attributes::check_attribute_display(((WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no'), $product['custom_display'], 'product_action_button', 'add_to_cart', 'complete_sheet');
1544
-        $display_price_state_when_price_is_empty = true;
1545
-        if (empty($productPrice) && (!empty($catalog_options) || (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes'))) {
1546
-            $display_price_state_when_price_is_empty = false;
1547
-        } else if (!empty($productPrice)) {
1548
-            $display_price_state_when_price_is_empty = true;
1549
-        }
1550
-        $add_to_cart_button = (true === $add_to_cart_button_display_state) && (true === $display_price_state_when_price_is_empty) ? self::display_add_to_cart_button($product_id, $productStock, 'complete') : '';
1551
-        $product_quantity_chooser_input = (true === $add_to_cart_button_display_state) ? wpshop_display::display_template_element('product_complete_sheet_quantity_chooser', array('PRODUCT_ID' => $product_id)) : '';
1552
-
1553
-        /** Define "Ask a quotation" button    */
1554
-        $quotation_button = self::display_quotation_button($product_id, (!empty($product['quotation_allowed']) ? $product['quotation_allowed'] : null), 'complete');
1555
-
1556
-        /** Template parameters    */
1557
-        $template_part = 'product_complete_tpl';
1558
-        $tpl_component = array();
1559
-
1560
-        $tpl_component['PRODUCT_VARIATIONS'] = wpshop_products::wpshop_variation($product_id);
1561
-        $tpl_component['PRODUCT_ID'] = $product_id;
1562
-        $tpl_component['PRODUCT_TITLE'] = $product['post_title'];
1563
-        $tpl_component['PRODUCT_THUMBNAIL'] = $productThumbnail;
1564
-        $tpl_component['PRODUCT_GALERY_PICS'] = '';
1565
-        $tpl_component['PRODUCT_PRICE'] = $productPrice;
1566
-        $modules_option = get_option('wpshop_modules');
1567
-        $tpl_component['LOW_STOCK_ALERT_MESSAGE'] = '';
1568
-        if (!empty($modules_option) && !empty($modules_option['wpshop_low_stock_alert']) && $modules_option['wpshop_low_stock_alert']['activated'] == 'on') {
1569
-            $wps_marketing_tools = new wps_marketing_tools_ctr();
1570
-            $tpl_component['LOW_STOCK_ALERT_MESSAGE'] = $wps_marketing_tools->display_alert_stock_message(array('id' => $product_id));
1571
-        }
1572
-
1573
-        /** Gallery **/
1574
-        $tpl_component['PRODUCT_COMPLETE_SHEET_GALLERY'] = wps_media_manager_frontend_ctr::get_product_complete_sheet_galery($product_id);
1575
-
1576
-        $product_new_def = self::display_product_special_state('declare_new', 'complete', (!empty($product['declare_new']) ? $product['declare_new'] : 'no'), (!empty($product['set_new_from']) ? $product['set_new_from'] : ''), (!empty($product['set_new_to']) ? $product['set_new_to'] : ''));
1577
-
1578
-        $product_new = $product_new_def['output'];
1579
-        $product_class = $product_new_def['class'];
1580
-
1581
-        $product_featured_def = self::display_product_special_state('highlight_product', 'complete', (!empty($product['highlight_product']) ? $product['highlight_product'] : 'no'), (!empty($product['highlight_from']) ? $product['highlight_from'] : ''), (!empty($product['highlight_to']) ? $product['highlight_to'] : ''));
1582
-        $product_featured = $product_featured_def['output'];
1583
-        $product_class .= $product_featured_def['class'];
1584
-
1585
-        $tpl_component['PRODUCT_IS_NEW'] = $product_new;
1586
-        $tpl_component['PRODUCT_IS_FEATURED'] = $product_featured;
1587
-        $tpl_component['PRODUCT_EXTRA_STATE'] = $tpl_component['PRODUCT_IS_NEW'] . $tpl_component['PRODUCT_IS_FEATURED'];
1588
-
1589
-        $tpl_component['PRODUCT_INITIAL_CONTENT'] = $initialContent;
1590
-        $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] = $add_to_cart_button;
1591
-        $tpl_component['PRODUCT_BUTTON_QUOTATION'] = $quotation_button;
1592
-        $tpl_component['PRODUCT_QUANTITY_CHOOSER'] = $product_quantity_chooser_input;
1593
-        $tpl_component['PRODUCT_BUTTONS'] = $tpl_component['PRODUCT_BUTTON_QUOTATION'] . $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'];
1594
-        $tpl_component['PRODUCT_GALERY_DOCS'] = $product_document_galery;
1595
-        $tpl_component['PRODUCT_FEATURES'] = $attributeContentOutput;
1596
-        $tpl_component = apply_filters('wps-filter-product-complete-sheet-output', $tpl_component, $product_id);
1597
-
1598
-        /** Build template    */
1599
-        $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1600
-        if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1601
-            /*    Include the old way template part    */
1602
-            ob_start();
1603
-            require wpshop_display::get_template_file($tpl_way_to_take[1]);
1604
-            $content = ob_get_contents();
1605
-            ob_end_clean();
1606
-        } else {
1607
-            $content = wpshop_display::display_template_element($template_part, $tpl_component);
1608
-        }
1609
-        unset($tpl_component);
1610
-        return $content;
1611
-    }
1612
-
1613
-    public static function product_mini_output($product_id, $category_id, $output_type = 'list', $current_item_position = 1, $grid_element_nb_per_line = WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE)
1614
-    {
1615
-        $content = '';
1616
-        $product_information = $product_class = '';
1617
-
1618
-        /** Get the product thumbnail    */
1619
-        $productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1620
-        if (has_post_thumbnail($product_id)) {
1621
-            $productThumbnail = get_the_post_thumbnail($product_id, 'thumbnail');
1622
-        }
1623
-
1624
-        $product = self::get_product_data($product_id);
1625
-
1626
-        /**    Get the product information for output    */
1627
-        if (!empty($product)) {
1628
-
1629
-            $product_title = $product['post_title'];
1630
-            $product_name = $product['post_name'];
1631
-            $product_link = get_permalink($product_id);
1632
-            $product_more_informations = $product['product_content'];
1633
-            $product_excerpt = $product['product_excerpt'];
1634
-
1635
-            if (strpos($product['product_content'], '<!--more-->')) {
1636
-                $post_content = explode('<!--more-->', $product['product_content']);
1637
-                $product_more_informations = $post_content[0];
1638
-            }
1639
-
1640
-        } else {
1641
-            $productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1642
-            $product_title = '<i>' . __('This product does not exist', 'wpshop') . '</i>';
1643
-            $product_link = '';
1644
-            $product_more_informations = '';
1645
-            $product_excerpt = '';
1646
-        }
1647
-
1648
-        /** Retrieve product price    */
1649
-        $price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
1650
-        $price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front_listing, $product['custom_display'], 'attribute', 'product_price', 'mini_output');
1651
-        $catalog_options = get_option('wpshop_catalog_main_option', array());
1652
-        $productPrice = '';
1653
-        $wpshop_price_piloting_option = get_option('wpshop_shop_price_piloting');
1654
-        $check_product_price = wpshop_prices::check_product_price($product);
1655
-        $result_price_piloting = (!empty($wpshop_price_piloting_option) && $wpshop_price_piloting_option == 'HT') ? $check_product_price['et'] : $check_product_price['ati'];
1656
-        if ($price_display && !(!empty($catalog_options) && (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes') && $result_price_piloting == 0)) {
1657
-            $product_price_infos = wpshop_prices::get_product_price($product, 'just_price_infos', 'mini_output');
1658
-
1659
-            if (!empty($product_price_infos)) {
1660
-                $tpl_component_price = array();
1661
-                /** Price piloting **/
1662
-                $price_ploting = get_option('wpshop_shop_price_piloting');
1663
-                $tpl_component_price['CROSSED_OUT_PRICE'] = (!empty($product_price_infos['CROSSED_OUT_PRICE'])) ? ((!empty($product_price_infos['PRICE_FROM'])) ? __('Price from', 'wpshop') . ' ' : '') . wpshop_display::display_template_element('product_price_template_crossed_out_price', array('CROSSED_OUT_PRICE_VALUE' => $product_price_infos['CROSSED_OUT_PRICE'])) : '';
1664
-                $variations = wpshop_products::get_variation($product_id);
1665
-                $tpl_component_price['PRODUCT_PRICE'] = (empty($product_price_infos['CROSSED_OUT_PRICE']) && (!empty($variations) && !empty($product_price_infos['PRICE_FROM']))) ? __('Price from', 'wpshop') . ' ' . $product_price_infos['PRODUCT_PRICE'] : $product_price_infos['PRODUCT_PRICE'];
1666
-                $tpl_component_price['MESSAGE_SAVE_MONEY'] = $product_price_infos['MESSAGE_SAVE_MONEY'];
1667
-                $tpl_component_price['TAX_PILOTING'] = (!empty($price_ploting) && $price_ploting == 'HT') ? __('ET', 'wpshop') : '';
1668
-                $product_price_infos['MESSAGE_SAVE_MONEY'];
1669
-                $productPrice = wpshop_display::display_template_element('product_price_template_mini_output', $tpl_component_price);
1670
-            } else {
1671
-                $productPrice = wpshop_prices::get_product_price($product, 'price_display', array('mini_output', $output_type));
1672
-            }
1673
-        }
1674
-
1675
-        /** Check if there is at less 1 product in stock    */
1676
-        $wps_product_ctr = new wps_product_ctr();
1677
-        $productStock = $wps_product_ctr->check_stock($product_id, 1);
1678
-        $productStock = $productStock === true ? 1 : null;
1679
-
1680
-        /** Define "Add to cart" button    */
1681
-        $add_to_cart_button_display_state = wpshop_attributes::check_attribute_display(((WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no'), $product['custom_display'], 'product_action_button', 'add_to_cart', 'mini_output');
1682
-        $display_price_state_when_price_is_empty = false;
1683
-        if (empty($productPrice) && (!empty($catalog_options) || (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes'))) {
1684
-            $display_price_state_when_price_is_empty = false;
1685
-        } else if (!empty($productPrice)) {
1686
-            $display_price_state_when_price_is_empty = true;
1687
-        }
1688
-        $add_to_cart_button = (true === $add_to_cart_button_display_state) && (true === $display_price_state_when_price_is_empty) ? self::display_add_to_cart_button($product_id, $productStock, 'mini') : '';
1689
-        $product_quantity_chooser_input = (true === $add_to_cart_button_display_state) ? wpshop_display::display_template_element('product_complete_sheet_quantity_chooser', array('PRODUCT_ID' => $product_id)) : '';
1690
-
1691
-        /** Define "Ask a quotation" button    */
1692
-        $quotation_button = self::display_quotation_button($product_id, (!empty($product['quotation_allowed']) ? $product['quotation_allowed'] : null));
1693
-        $product_new_def = self::display_product_special_state('declare_new', $output_type, (!empty($product['declare_new']) ? $product['declare_new'] : 'no'), (!empty($product['set_new_from']) ? $product['set_new_from'] : ''), (!empty($product['set_new_to']) ? $product['set_new_to'] : ''));
1694
-
1695
-        $product_new = $product_new_def['output'];
1696
-        $product_class .= $product_new_def['class'];
1697
-
1698
-        $product_featured_def = self::display_product_special_state('highlight_product', $output_type, (!empty($product['highlight_product']) ? $product['highlight_product'] : 'no'), (!empty($product['highlight_from']) ? $product['highlight_from'] : ''), (!empty($product['highlight_to']) ? $product['highlight_to'] : ''));
1699
-        $product_featured = $product_featured_def['output'];
1700
-        $product_class .= $product_featured_def['class'];
1701
-
1702
-        if (!($current_item_position % $grid_element_nb_per_line)) {
1703
-            $product_class .= ' wpshop_last_product_of_line';
1704
-        }
1705
-
1706
-        if (!empty($product['product_id'])) {
1707
-            /** Template parameters    */
1708
-            $template_part = 'product_mini_' . $output_type;
1709
-            $tpl_component = array();
1710
-            $tpl_component['PRODUCT_THUMBNAIL'] = wpshop_display::display_template_element('product_thumbnail_default', array());
1711
-            $tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] = wpshop_display::display_template_element('product_thumbnail_default', array());
1712
-            $tpl_component['PRODUCT_ID'] = $product_id;
1713
-            $tpl_component['PRODUCT_CLASS'] = $product_class;
1714
-            $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] = $add_to_cart_button;
1715
-            $tpl_component['PRODUCT_BUTTON_QUOTATION'] = $quotation_button;
1716
-            $tpl_component['PRODUCT_QUANTITY_CHOOSER'] = $product_quantity_chooser_input;
1717
-            $tpl_component['PRODUCT_BUTTONS'] = $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] . $tpl_component['PRODUCT_BUTTON_QUOTATION'];
1718
-            $tpl_component['PRODUCT_PRICE'] = $productPrice;
1719
-            $tpl_component['PRODUCT_PERMALINK'] = $product_link;
1720
-            $tpl_component['PRODUCT_TITLE'] = (!empty($product_title)) ? $product_title : '';
1721
-            $tpl_component['PRODUCT_NAME'] = $product_name;
1722
-            $tpl_component['PRODUCT_DESCRIPTION'] = $product_more_informations;
1723
-            $tpl_component['PRODUCT_IS_NEW'] = $product_new;
1724
-            $tpl_component['PRODUCT_IS_FEATURED'] = $product_featured;
1725
-            $tpl_component['PRODUCT_EXTRA_STATE'] = $tpl_component['PRODUCT_IS_NEW'] . $tpl_component['PRODUCT_IS_FEATURED'];
1726
-            $tpl_component['PRODUCT_THUMBNAIL'] = $productThumbnail;
1727
-
1728
-            if (has_post_thumbnail($product_id)) {
1729
-                $image_attributes = wp_get_attachment_metadata(get_post_thumbnail_id($product_id));
1730
-                if (!empty($image_attributes) && !empty($image_attributes['sizes']) && is_array($image_attributes['sizes'])) {
1731
-                    $existing_image_sizes = get_intermediate_image_sizes();
1732
-                    foreach ($existing_image_sizes as $size_name) {
1733
-                        $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = wp_get_attachment_image(get_post_thumbnail_id($product_id), $size_name);
1734
-                        $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = (!empty($tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)])) ? $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1735
-                    }
1736
-                }
1737
-            }
1738
-
1739
-            $tpl_component['PRODUCT_EXCERPT'] = $product_excerpt;
1740
-            $tpl_component['PRODUCT_OUTPUT_TYPE'] = $output_type;
1741
-            $tpl_component = apply_filters('wps-filter-product-mini-output', $tpl_component, $product_id);
1742
-
1743
-            /** Build template    */
1744
-            $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1745
-            if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1746
-                /**    Include the old way template part    */
1747
-                ob_start();
1748
-                require wpshop_display::get_template_file($tpl_way_to_take[1]);
1749
-                $content = ob_get_contents();
1750
-                ob_end_clean();
1751
-            } else {
1752
-                $content = wpshop_display::display_template_element($template_part, $tpl_component);
1753
-            }
1754
-            unset($tpl_component);
1755
-
1756
-        }
1757
-
1758
-        return $content;
1759
-    }
1760
-
1761
-    /**
1762
-     *    Get the products (post) of a given category
1763
-     *
1764
-     *    @param string $category_slug The category slug we want to get the product list for
1765
-     *
1766
-     *    @return mixed $widget_content The output for the product list
1767
-     */
1768
-    public static function get_product_of_category($category_slug, $category_id)
1769
-    {
1770
-        global $top_categories;
1771
-        $widget_content = '';
1772
-        $products = wp_cache_get('wpshop_products_category_' . $category_id);
1773
-        if (false == $products) {
1774
-            $args = array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES => $category_slug);
1775
-            $products = get_posts($args);
1776
-            wp_cache_set('wpshop_products_category_' . $category_id, $products);
1777
-        }
1778
-        if (is_array($products) && (count($products) > 0)) {
1779
-            foreach ($products as $product) {
1780
-                if ($product->post_status == "publish") {
1781
-                    ob_start();
1782
-                    require wpshop_display::get_template_file('categories_products-widget.tpl.php');
1783
-                    $widget_content .= ob_get_contents();
1784
-                    ob_end_clean();
1785
-                }
1786
-            }
1787
-        }
1788
-
1789
-        echo $widget_content;
1790
-    }
1791
-
1792
-    /**
1793
-     *
1794
-     * @param unknown_type $selected_product
1795
-     * @return string
1796
-     */
1797
-    public function custom_product_list($selected_product = array())
1798
-    {
1799
-        global $wpdb;
1800
-
1801
-        /*    Start the table definition    */
1802
-        $tableId = 'wpshop_product_list';
1803
-        $tableTitles = array();
1804
-        $tableTitles[] = '';
1805
-        $tableTitles[] = __('Id', 'wpshop');
1806
-        $tableTitles[] = __('Quantity', 'wpshop');
1807
-        $tableTitles[] = __('Reference', 'wpshop');
1808
-        $tableTitles[] = __('Product name', 'wpshop');
1809
-        $tableTitles[] = __('Actions', 'wpshop');
1810
-        $tableTitles[] = __('Price', 'wpshop');
1811
-        $tableClasses = array();
1812
-        $tableClasses[] = 'wpshop_product_selector_column';
1813
-        $tableClasses[] = 'wpshop_product_identifier_column';
1814
-        $tableClasses[] = 'wpshop_product_quantity_column';
1815
-        $tableClasses[] = 'wpshop_product_sku_column';
1816
-        $tableClasses[] = 'wpshop_product_name_column';
1817
-        $tableClasses[] = 'wpshop_product_link_column';
1818
-        $tableClasses[] = 'wpshop_product_price_column';
1819
-
1820
-        /*    Get post list    */
1821
-        $has_result = false;
1822
-        $current_line_index = 0;
1823
-        $posts = query_posts(array(
1824
-            'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'posts_per_page' => -1,
1825
-        ));
1826
-        if (!empty($posts)) {
1827
-            $has_result = true;
1828
-            foreach ($posts as $post) {
1829
-                $tableRowsId[$current_line_index] = 'product_' . $post->ID;
1830
-
1831
-                $post_info = get_post_meta($post->ID, '_wpshop_product_metadata', true);
1832
-
1833
-                unset($tableRowValue);
1834
-                $tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '<input type="checkbox" name="wp_list_product[]" value="' . $post->ID . '" class="wpshop_product_cb_dialog" id="wpshop_product_cb_dialog_' . $post->ID . '" />');
1835
-                $tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '<label for="wpshop_product_cb_dialog_' . $post->ID . '" >' . WPSHOP_IDENTIFIER_PRODUCT . $post->ID . '</label>');
1836
-                $tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '<a href="#" class="order_product_action_button qty_change">-</a><input type="text" name="wpshop_pdt_qty[' . $post->ID . ']" value="1" class="wpshop_order_product_qty" /><a href="#" class="order_product_action_button qty_change">+</a>');
1837
-                $tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => (!empty($post_info['product_reference'])) ? $post_info['product_reference'] : '');
1838
-                $tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $post->post_title);
1839
-                $tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '<a href="' . $post->guid . '" target="wpshop_product_view_product" target="wpshop_view_product" >' . __('View product', 'wpshop') . '</a><br/>
1542
+		/** Define "Add to cart" button     */
1543
+		$add_to_cart_button_display_state = wpshop_attributes::check_attribute_display(((WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no'), $product['custom_display'], 'product_action_button', 'add_to_cart', 'complete_sheet');
1544
+		$display_price_state_when_price_is_empty = true;
1545
+		if (empty($productPrice) && (!empty($catalog_options) || (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes'))) {
1546
+			$display_price_state_when_price_is_empty = false;
1547
+		} else if (!empty($productPrice)) {
1548
+			$display_price_state_when_price_is_empty = true;
1549
+		}
1550
+		$add_to_cart_button = (true === $add_to_cart_button_display_state) && (true === $display_price_state_when_price_is_empty) ? self::display_add_to_cart_button($product_id, $productStock, 'complete') : '';
1551
+		$product_quantity_chooser_input = (true === $add_to_cart_button_display_state) ? wpshop_display::display_template_element('product_complete_sheet_quantity_chooser', array('PRODUCT_ID' => $product_id)) : '';
1552
+
1553
+		/** Define "Ask a quotation" button    */
1554
+		$quotation_button = self::display_quotation_button($product_id, (!empty($product['quotation_allowed']) ? $product['quotation_allowed'] : null), 'complete');
1555
+
1556
+		/** Template parameters    */
1557
+		$template_part = 'product_complete_tpl';
1558
+		$tpl_component = array();
1559
+
1560
+		$tpl_component['PRODUCT_VARIATIONS'] = wpshop_products::wpshop_variation($product_id);
1561
+		$tpl_component['PRODUCT_ID'] = $product_id;
1562
+		$tpl_component['PRODUCT_TITLE'] = $product['post_title'];
1563
+		$tpl_component['PRODUCT_THUMBNAIL'] = $productThumbnail;
1564
+		$tpl_component['PRODUCT_GALERY_PICS'] = '';
1565
+		$tpl_component['PRODUCT_PRICE'] = $productPrice;
1566
+		$modules_option = get_option('wpshop_modules');
1567
+		$tpl_component['LOW_STOCK_ALERT_MESSAGE'] = '';
1568
+		if (!empty($modules_option) && !empty($modules_option['wpshop_low_stock_alert']) && $modules_option['wpshop_low_stock_alert']['activated'] == 'on') {
1569
+			$wps_marketing_tools = new wps_marketing_tools_ctr();
1570
+			$tpl_component['LOW_STOCK_ALERT_MESSAGE'] = $wps_marketing_tools->display_alert_stock_message(array('id' => $product_id));
1571
+		}
1572
+
1573
+		/** Gallery **/
1574
+		$tpl_component['PRODUCT_COMPLETE_SHEET_GALLERY'] = wps_media_manager_frontend_ctr::get_product_complete_sheet_galery($product_id);
1575
+
1576
+		$product_new_def = self::display_product_special_state('declare_new', 'complete', (!empty($product['declare_new']) ? $product['declare_new'] : 'no'), (!empty($product['set_new_from']) ? $product['set_new_from'] : ''), (!empty($product['set_new_to']) ? $product['set_new_to'] : ''));
1577
+
1578
+		$product_new = $product_new_def['output'];
1579
+		$product_class = $product_new_def['class'];
1580
+
1581
+		$product_featured_def = self::display_product_special_state('highlight_product', 'complete', (!empty($product['highlight_product']) ? $product['highlight_product'] : 'no'), (!empty($product['highlight_from']) ? $product['highlight_from'] : ''), (!empty($product['highlight_to']) ? $product['highlight_to'] : ''));
1582
+		$product_featured = $product_featured_def['output'];
1583
+		$product_class .= $product_featured_def['class'];
1584
+
1585
+		$tpl_component['PRODUCT_IS_NEW'] = $product_new;
1586
+		$tpl_component['PRODUCT_IS_FEATURED'] = $product_featured;
1587
+		$tpl_component['PRODUCT_EXTRA_STATE'] = $tpl_component['PRODUCT_IS_NEW'] . $tpl_component['PRODUCT_IS_FEATURED'];
1588
+
1589
+		$tpl_component['PRODUCT_INITIAL_CONTENT'] = $initialContent;
1590
+		$tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] = $add_to_cart_button;
1591
+		$tpl_component['PRODUCT_BUTTON_QUOTATION'] = $quotation_button;
1592
+		$tpl_component['PRODUCT_QUANTITY_CHOOSER'] = $product_quantity_chooser_input;
1593
+		$tpl_component['PRODUCT_BUTTONS'] = $tpl_component['PRODUCT_BUTTON_QUOTATION'] . $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'];
1594
+		$tpl_component['PRODUCT_GALERY_DOCS'] = $product_document_galery;
1595
+		$tpl_component['PRODUCT_FEATURES'] = $attributeContentOutput;
1596
+		$tpl_component = apply_filters('wps-filter-product-complete-sheet-output', $tpl_component, $product_id);
1597
+
1598
+		/** Build template    */
1599
+		$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1600
+		if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1601
+			/*    Include the old way template part    */
1602
+			ob_start();
1603
+			require wpshop_display::get_template_file($tpl_way_to_take[1]);
1604
+			$content = ob_get_contents();
1605
+			ob_end_clean();
1606
+		} else {
1607
+			$content = wpshop_display::display_template_element($template_part, $tpl_component);
1608
+		}
1609
+		unset($tpl_component);
1610
+		return $content;
1611
+	}
1612
+
1613
+	public static function product_mini_output($product_id, $category_id, $output_type = 'list', $current_item_position = 1, $grid_element_nb_per_line = WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE)
1614
+	{
1615
+		$content = '';
1616
+		$product_information = $product_class = '';
1617
+
1618
+		/** Get the product thumbnail    */
1619
+		$productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1620
+		if (has_post_thumbnail($product_id)) {
1621
+			$productThumbnail = get_the_post_thumbnail($product_id, 'thumbnail');
1622
+		}
1623
+
1624
+		$product = self::get_product_data($product_id);
1625
+
1626
+		/**    Get the product information for output    */
1627
+		if (!empty($product)) {
1628
+
1629
+			$product_title = $product['post_title'];
1630
+			$product_name = $product['post_name'];
1631
+			$product_link = get_permalink($product_id);
1632
+			$product_more_informations = $product['product_content'];
1633
+			$product_excerpt = $product['product_excerpt'];
1634
+
1635
+			if (strpos($product['product_content'], '<!--more-->')) {
1636
+				$post_content = explode('<!--more-->', $product['product_content']);
1637
+				$product_more_informations = $post_content[0];
1638
+			}
1639
+
1640
+		} else {
1641
+			$productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
1642
+			$product_title = '<i>' . __('This product does not exist', 'wpshop') . '</i>';
1643
+			$product_link = '';
1644
+			$product_more_informations = '';
1645
+			$product_excerpt = '';
1646
+		}
1647
+
1648
+		/** Retrieve product price    */
1649
+		$price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
1650
+		$price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front_listing, $product['custom_display'], 'attribute', 'product_price', 'mini_output');
1651
+		$catalog_options = get_option('wpshop_catalog_main_option', array());
1652
+		$productPrice = '';
1653
+		$wpshop_price_piloting_option = get_option('wpshop_shop_price_piloting');
1654
+		$check_product_price = wpshop_prices::check_product_price($product);
1655
+		$result_price_piloting = (!empty($wpshop_price_piloting_option) && $wpshop_price_piloting_option == 'HT') ? $check_product_price['et'] : $check_product_price['ati'];
1656
+		if ($price_display && !(!empty($catalog_options) && (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes') && $result_price_piloting == 0)) {
1657
+			$product_price_infos = wpshop_prices::get_product_price($product, 'just_price_infos', 'mini_output');
1658
+
1659
+			if (!empty($product_price_infos)) {
1660
+				$tpl_component_price = array();
1661
+				/** Price piloting **/
1662
+				$price_ploting = get_option('wpshop_shop_price_piloting');
1663
+				$tpl_component_price['CROSSED_OUT_PRICE'] = (!empty($product_price_infos['CROSSED_OUT_PRICE'])) ? ((!empty($product_price_infos['PRICE_FROM'])) ? __('Price from', 'wpshop') . ' ' : '') . wpshop_display::display_template_element('product_price_template_crossed_out_price', array('CROSSED_OUT_PRICE_VALUE' => $product_price_infos['CROSSED_OUT_PRICE'])) : '';
1664
+				$variations = wpshop_products::get_variation($product_id);
1665
+				$tpl_component_price['PRODUCT_PRICE'] = (empty($product_price_infos['CROSSED_OUT_PRICE']) && (!empty($variations) && !empty($product_price_infos['PRICE_FROM']))) ? __('Price from', 'wpshop') . ' ' . $product_price_infos['PRODUCT_PRICE'] : $product_price_infos['PRODUCT_PRICE'];
1666
+				$tpl_component_price['MESSAGE_SAVE_MONEY'] = $product_price_infos['MESSAGE_SAVE_MONEY'];
1667
+				$tpl_component_price['TAX_PILOTING'] = (!empty($price_ploting) && $price_ploting == 'HT') ? __('ET', 'wpshop') : '';
1668
+				$product_price_infos['MESSAGE_SAVE_MONEY'];
1669
+				$productPrice = wpshop_display::display_template_element('product_price_template_mini_output', $tpl_component_price);
1670
+			} else {
1671
+				$productPrice = wpshop_prices::get_product_price($product, 'price_display', array('mini_output', $output_type));
1672
+			}
1673
+		}
1674
+
1675
+		/** Check if there is at less 1 product in stock    */
1676
+		$wps_product_ctr = new wps_product_ctr();
1677
+		$productStock = $wps_product_ctr->check_stock($product_id, 1);
1678
+		$productStock = $productStock === true ? 1 : null;
1679
+
1680
+		/** Define "Add to cart" button    */
1681
+		$add_to_cart_button_display_state = wpshop_attributes::check_attribute_display(((WPSHOP_DEFINED_SHOP_TYPE == 'sale') ? 'yes' : 'no'), $product['custom_display'], 'product_action_button', 'add_to_cart', 'mini_output');
1682
+		$display_price_state_when_price_is_empty = false;
1683
+		if (empty($productPrice) && (!empty($catalog_options) || (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes'))) {
1684
+			$display_price_state_when_price_is_empty = false;
1685
+		} else if (!empty($productPrice)) {
1686
+			$display_price_state_when_price_is_empty = true;
1687
+		}
1688
+		$add_to_cart_button = (true === $add_to_cart_button_display_state) && (true === $display_price_state_when_price_is_empty) ? self::display_add_to_cart_button($product_id, $productStock, 'mini') : '';
1689
+		$product_quantity_chooser_input = (true === $add_to_cart_button_display_state) ? wpshop_display::display_template_element('product_complete_sheet_quantity_chooser', array('PRODUCT_ID' => $product_id)) : '';
1690
+
1691
+		/** Define "Ask a quotation" button    */
1692
+		$quotation_button = self::display_quotation_button($product_id, (!empty($product['quotation_allowed']) ? $product['quotation_allowed'] : null));
1693
+		$product_new_def = self::display_product_special_state('declare_new', $output_type, (!empty($product['declare_new']) ? $product['declare_new'] : 'no'), (!empty($product['set_new_from']) ? $product['set_new_from'] : ''), (!empty($product['set_new_to']) ? $product['set_new_to'] : ''));
1694
+
1695
+		$product_new = $product_new_def['output'];
1696
+		$product_class .= $product_new_def['class'];
1697
+
1698
+		$product_featured_def = self::display_product_special_state('highlight_product', $output_type, (!empty($product['highlight_product']) ? $product['highlight_product'] : 'no'), (!empty($product['highlight_from']) ? $product['highlight_from'] : ''), (!empty($product['highlight_to']) ? $product['highlight_to'] : ''));
1699
+		$product_featured = $product_featured_def['output'];
1700
+		$product_class .= $product_featured_def['class'];
1701
+
1702
+		if (!($current_item_position % $grid_element_nb_per_line)) {
1703
+			$product_class .= ' wpshop_last_product_of_line';
1704
+		}
1705
+
1706
+		if (!empty($product['product_id'])) {
1707
+			/** Template parameters    */
1708
+			$template_part = 'product_mini_' . $output_type;
1709
+			$tpl_component = array();
1710
+			$tpl_component['PRODUCT_THUMBNAIL'] = wpshop_display::display_template_element('product_thumbnail_default', array());
1711
+			$tpl_component['PRODUCT_THUMBNAIL_WPSHOP-PRODUCT-GALERY'] = wpshop_display::display_template_element('product_thumbnail_default', array());
1712
+			$tpl_component['PRODUCT_ID'] = $product_id;
1713
+			$tpl_component['PRODUCT_CLASS'] = $product_class;
1714
+			$tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] = $add_to_cart_button;
1715
+			$tpl_component['PRODUCT_BUTTON_QUOTATION'] = $quotation_button;
1716
+			$tpl_component['PRODUCT_QUANTITY_CHOOSER'] = $product_quantity_chooser_input;
1717
+			$tpl_component['PRODUCT_BUTTONS'] = $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] . $tpl_component['PRODUCT_BUTTON_QUOTATION'];
1718
+			$tpl_component['PRODUCT_PRICE'] = $productPrice;
1719
+			$tpl_component['PRODUCT_PERMALINK'] = $product_link;
1720
+			$tpl_component['PRODUCT_TITLE'] = (!empty($product_title)) ? $product_title : '';
1721
+			$tpl_component['PRODUCT_NAME'] = $product_name;
1722
+			$tpl_component['PRODUCT_DESCRIPTION'] = $product_more_informations;
1723
+			$tpl_component['PRODUCT_IS_NEW'] = $product_new;
1724
+			$tpl_component['PRODUCT_IS_FEATURED'] = $product_featured;
1725
+			$tpl_component['PRODUCT_EXTRA_STATE'] = $tpl_component['PRODUCT_IS_NEW'] . $tpl_component['PRODUCT_IS_FEATURED'];
1726
+			$tpl_component['PRODUCT_THUMBNAIL'] = $productThumbnail;
1727
+
1728
+			if (has_post_thumbnail($product_id)) {
1729
+				$image_attributes = wp_get_attachment_metadata(get_post_thumbnail_id($product_id));
1730
+				if (!empty($image_attributes) && !empty($image_attributes['sizes']) && is_array($image_attributes['sizes'])) {
1731
+					$existing_image_sizes = get_intermediate_image_sizes();
1732
+					foreach ($existing_image_sizes as $size_name) {
1733
+						$tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = wp_get_attachment_image(get_post_thumbnail_id($product_id), $size_name);
1734
+						$tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = (!empty($tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)])) ? $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
1735
+					}
1736
+				}
1737
+			}
1738
+
1739
+			$tpl_component['PRODUCT_EXCERPT'] = $product_excerpt;
1740
+			$tpl_component['PRODUCT_OUTPUT_TYPE'] = $output_type;
1741
+			$tpl_component = apply_filters('wps-filter-product-mini-output', $tpl_component, $product_id);
1742
+
1743
+			/** Build template    */
1744
+			$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1745
+			if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1746
+				/**    Include the old way template part    */
1747
+				ob_start();
1748
+				require wpshop_display::get_template_file($tpl_way_to_take[1]);
1749
+				$content = ob_get_contents();
1750
+				ob_end_clean();
1751
+			} else {
1752
+				$content = wpshop_display::display_template_element($template_part, $tpl_component);
1753
+			}
1754
+			unset($tpl_component);
1755
+
1756
+		}
1757
+
1758
+		return $content;
1759
+	}
1760
+
1761
+	/**
1762
+	 *    Get the products (post) of a given category
1763
+	 *
1764
+	 *    @param string $category_slug The category slug we want to get the product list for
1765
+	 *
1766
+	 *    @return mixed $widget_content The output for the product list
1767
+	 */
1768
+	public static function get_product_of_category($category_slug, $category_id)
1769
+	{
1770
+		global $top_categories;
1771
+		$widget_content = '';
1772
+		$products = wp_cache_get('wpshop_products_category_' . $category_id);
1773
+		if (false == $products) {
1774
+			$args = array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES => $category_slug);
1775
+			$products = get_posts($args);
1776
+			wp_cache_set('wpshop_products_category_' . $category_id, $products);
1777
+		}
1778
+		if (is_array($products) && (count($products) > 0)) {
1779
+			foreach ($products as $product) {
1780
+				if ($product->post_status == "publish") {
1781
+					ob_start();
1782
+					require wpshop_display::get_template_file('categories_products-widget.tpl.php');
1783
+					$widget_content .= ob_get_contents();
1784
+					ob_end_clean();
1785
+				}
1786
+			}
1787
+		}
1788
+
1789
+		echo $widget_content;
1790
+	}
1791
+
1792
+	/**
1793
+	 *
1794
+	 * @param unknown_type $selected_product
1795
+	 * @return string
1796
+	 */
1797
+	public function custom_product_list($selected_product = array())
1798
+	{
1799
+		global $wpdb;
1800
+
1801
+		/*    Start the table definition    */
1802
+		$tableId = 'wpshop_product_list';
1803
+		$tableTitles = array();
1804
+		$tableTitles[] = '';
1805
+		$tableTitles[] = __('Id', 'wpshop');
1806
+		$tableTitles[] = __('Quantity', 'wpshop');
1807
+		$tableTitles[] = __('Reference', 'wpshop');
1808
+		$tableTitles[] = __('Product name', 'wpshop');
1809
+		$tableTitles[] = __('Actions', 'wpshop');
1810
+		$tableTitles[] = __('Price', 'wpshop');
1811
+		$tableClasses = array();
1812
+		$tableClasses[] = 'wpshop_product_selector_column';
1813
+		$tableClasses[] = 'wpshop_product_identifier_column';
1814
+		$tableClasses[] = 'wpshop_product_quantity_column';
1815
+		$tableClasses[] = 'wpshop_product_sku_column';
1816
+		$tableClasses[] = 'wpshop_product_name_column';
1817
+		$tableClasses[] = 'wpshop_product_link_column';
1818
+		$tableClasses[] = 'wpshop_product_price_column';
1819
+
1820
+		/*    Get post list    */
1821
+		$has_result = false;
1822
+		$current_line_index = 0;
1823
+		$posts = query_posts(array(
1824
+			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'posts_per_page' => -1,
1825
+		));
1826
+		if (!empty($posts)) {
1827
+			$has_result = true;
1828
+			foreach ($posts as $post) {
1829
+				$tableRowsId[$current_line_index] = 'product_' . $post->ID;
1830
+
1831
+				$post_info = get_post_meta($post->ID, '_wpshop_product_metadata', true);
1832
+
1833
+				unset($tableRowValue);
1834
+				$tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '<input type="checkbox" name="wp_list_product[]" value="' . $post->ID . '" class="wpshop_product_cb_dialog" id="wpshop_product_cb_dialog_' . $post->ID . '" />');
1835
+				$tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '<label for="wpshop_product_cb_dialog_' . $post->ID . '" >' . WPSHOP_IDENTIFIER_PRODUCT . $post->ID . '</label>');
1836
+				$tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '<a href="#" class="order_product_action_button qty_change">-</a><input type="text" name="wpshop_pdt_qty[' . $post->ID . ']" value="1" class="wpshop_order_product_qty" /><a href="#" class="order_product_action_button qty_change">+</a>');
1837
+				$tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => (!empty($post_info['product_reference'])) ? $post_info['product_reference'] : '');
1838
+				$tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $post->post_title);
1839
+				$tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '<a href="' . $post->guid . '" target="wpshop_product_view_product" target="wpshop_view_product" >' . __('View product', 'wpshop') . '</a><br/>
1840 1840
 				<a href="' . admin_url('post.php?post=' . $post->ID . '&action=edit') . '" target="wpshop_edit_product" >' . __('Edit product', 'wpshop') . '</a>');
1841
-                $tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => __('Price ET', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_HT])) ? round($post_info[WPSHOP_PRODUCT_PRICE_HT], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : '') . '<br/>' . __('Price ATI', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_TTC])) ? round($post_info[WPSHOP_PRODUCT_PRICE_TTC], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : ''));
1842
-                $tableRows[] = $tableRowValue;
1843
-
1844
-                $current_line_index++;
1845
-            }
1846
-            wp_reset_query();
1847
-        }
1848
-        $posts = query_posts(array(
1849
-            'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'posts_per_page' => -1,
1850
-        ));
1851
-        if (!empty($posts)) {
1852
-            $has_result = true;
1853
-            foreach ($posts as $post) {
1854
-                $tableRowsId[$current_line_index] = 'product_' . $post->ID;
1855
-
1856
-                $post_info = get_post_meta($post->ID, '_wpshop_product_metadata', true);
1857
-
1858
-                unset($tableRowValue);
1859
-                $tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '<input type="checkbox" name="wp_list_product[]" value="' . $post->ID . '" class="wpshop_product_cb_dialog" id="wpshop_product_cb_dialog_' . $post->ID . '" />');
1860
-                $tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '<label for="wpshop_product_cb_dialog_' . $post->ID . '" >' . WPSHOP_IDENTIFIER_PRODUCT . $post->ID . '</label>');
1861
-                $tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '<a href="#" class="order_product_action_button qty_change">-</a><input type="text" name="wpshop_pdt_qty[' . $post->ID . ']" value="1" class="wpshop_order_product_qty" /><a href="#" class="order_product_action_button qty_change">+</a>');
1862
-                $tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => (!empty($post_info['product_reference'])) ? $post_info['product_reference'] : '');
1863
-                $parent_product = wpshop_products::get_parent_variation($post->ID);
1864
-                if (!empty($parent_product) && !empty($parent_product['parent_post'])) {
1865
-                    $product_variations_postmeta = get_post_meta($post->ID, '_wpshop_variations_attribute_def', true);
1866
-                    $query = $wpdb->prepare('SELECT frontend_label FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s', key($product_variations_postmeta));
1867
-                    $option_name = $wpdb->get_var($query);
1868
-                    $query = $wpdb->prepare('SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $product_variations_postmeta[key($product_variations_postmeta)]);
1869
-                    $option_value = $wpdb->get_var($query);
1870
-                    $parent_post = $parent_product['parent_post'];
1871
-                    $tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $parent_post->post_title . ' <br/>(' . $option_name . ' : ' . $option_value . ')');
1872
-                } else {
1873
-                    $tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $post->post_title);
1874
-                }
1875
-
1876
-                $tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '<a href="' . $post->guid . '" target="wpshop_product_view_product" target="wpshop_view_product" >' . __('View product', 'wpshop') . '</a><br/>
1841
+				$tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => __('Price ET', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_HT])) ? round($post_info[WPSHOP_PRODUCT_PRICE_HT], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : '') . '<br/>' . __('Price ATI', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_TTC])) ? round($post_info[WPSHOP_PRODUCT_PRICE_TTC], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : ''));
1842
+				$tableRows[] = $tableRowValue;
1843
+
1844
+				$current_line_index++;
1845
+			}
1846
+			wp_reset_query();
1847
+		}
1848
+		$posts = query_posts(array(
1849
+			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'posts_per_page' => -1,
1850
+		));
1851
+		if (!empty($posts)) {
1852
+			$has_result = true;
1853
+			foreach ($posts as $post) {
1854
+				$tableRowsId[$current_line_index] = 'product_' . $post->ID;
1855
+
1856
+				$post_info = get_post_meta($post->ID, '_wpshop_product_metadata', true);
1857
+
1858
+				unset($tableRowValue);
1859
+				$tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '<input type="checkbox" name="wp_list_product[]" value="' . $post->ID . '" class="wpshop_product_cb_dialog" id="wpshop_product_cb_dialog_' . $post->ID . '" />');
1860
+				$tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '<label for="wpshop_product_cb_dialog_' . $post->ID . '" >' . WPSHOP_IDENTIFIER_PRODUCT . $post->ID . '</label>');
1861
+				$tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '<a href="#" class="order_product_action_button qty_change">-</a><input type="text" name="wpshop_pdt_qty[' . $post->ID . ']" value="1" class="wpshop_order_product_qty" /><a href="#" class="order_product_action_button qty_change">+</a>');
1862
+				$tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => (!empty($post_info['product_reference'])) ? $post_info['product_reference'] : '');
1863
+				$parent_product = wpshop_products::get_parent_variation($post->ID);
1864
+				if (!empty($parent_product) && !empty($parent_product['parent_post'])) {
1865
+					$product_variations_postmeta = get_post_meta($post->ID, '_wpshop_variations_attribute_def', true);
1866
+					$query = $wpdb->prepare('SELECT frontend_label FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s', key($product_variations_postmeta));
1867
+					$option_name = $wpdb->get_var($query);
1868
+					$query = $wpdb->prepare('SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $product_variations_postmeta[key($product_variations_postmeta)]);
1869
+					$option_value = $wpdb->get_var($query);
1870
+					$parent_post = $parent_product['parent_post'];
1871
+					$tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $parent_post->post_title . ' <br/>(' . $option_name . ' : ' . $option_value . ')');
1872
+				} else {
1873
+					$tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => $post->post_title);
1874
+				}
1875
+
1876
+				$tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '<a href="' . $post->guid . '" target="wpshop_product_view_product" target="wpshop_view_product" >' . __('View product', 'wpshop') . '</a><br/>
1877 1877
 				<a href="' . admin_url('post.php?post=' . $post->ID . '&action=edit') . '" target="wpshop_edit_product" >' . __('Edit product', 'wpshop') . '</a>');
1878
-                $tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => __('Price ET', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_HT])) ? round($post_info[WPSHOP_PRODUCT_PRICE_HT], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : '') . '<br/>' . __('Price ATI', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_TTC])) ? round($post_info[WPSHOP_PRODUCT_PRICE_TTC], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : ''));
1879
-                $tableRows[] = $tableRowValue;
1880
-
1881
-                $current_line_index++;
1882
-            }
1883
-            wp_reset_query();
1884
-        }
1885
-
1886
-        if (!$has_result) {
1887
-            $tableRowsId[] = 'no_product_found';
1888
-            unset($tableRowValue);
1889
-            $tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '');
1890
-            $tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '');
1891
-            $tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '');
1892
-            $tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => __('No element to ouput here', 'wpshop'));
1893
-            $tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => '');
1894
-            $tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '');
1895
-            $tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => '');
1896
-            $tableRows[] = $tableRowValue;
1897
-        }
1898
-
1899
-        return wpshop_display::getTable($tableId, $tableTitles, $tableRows, $tableClasses, $tableRowsId, '', false) . '
1878
+				$tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => __('Price ET', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_HT])) ? round($post_info[WPSHOP_PRODUCT_PRICE_HT], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : '') . '<br/>' . __('Price ATI', 'wpshop') . '&nbsp;:&nbsp;' . ((!empty($post_info[WPSHOP_PRODUCT_PRICE_TTC])) ? round($post_info[WPSHOP_PRODUCT_PRICE_TTC], 2) . '&nbsp;' . wpshop_tools::wpshop_get_currency() : ''));
1879
+				$tableRows[] = $tableRowValue;
1880
+
1881
+				$current_line_index++;
1882
+			}
1883
+			wp_reset_query();
1884
+		}
1885
+
1886
+		if (!$has_result) {
1887
+			$tableRowsId[] = 'no_product_found';
1888
+			unset($tableRowValue);
1889
+			$tableRowValue[] = array('class' => 'wpshop_product_selector_cell', 'value' => '');
1890
+			$tableRowValue[] = array('class' => 'wpshop_product_identifier_cell', 'value' => '');
1891
+			$tableRowValue[] = array('class' => 'wpshop_product_quantity_cell', 'value' => '');
1892
+			$tableRowValue[] = array('class' => 'wpshop_product_sku_cell', 'value' => __('No element to ouput here', 'wpshop'));
1893
+			$tableRowValue[] = array('class' => 'wpshop_product_name_cell', 'value' => '');
1894
+			$tableRowValue[] = array('class' => 'wpshop_product_link_cell', 'value' => '');
1895
+			$tableRowValue[] = array('class' => 'wpshop_product_price_cell', 'value' => '');
1896
+			$tableRows[] = $tableRowValue;
1897
+		}
1898
+
1899
+		return wpshop_display::getTable($tableId, $tableTitles, $tableRows, $tableClasses, $tableRowsId, '', false) . '
1900 1900
 <script type="text/javascript" >
1901 1901
 	wpshop(document).ready(function(){
1902 1902
 		jQuery("#' . $tableId . '").dataTable( {
@@ -1905,1530 +1905,1530 @@  discard block
 block discarded – undo
1905 1905
 		});
1906 1906
 	});
1907 1907
 </script>';
1908
-    }
1909
-
1910
-    /**
1911
-     * Allows to manage output for special state for a product (New product/highlight product)
1912
-     *
1913
-     * @param string $special The type of special type we want to output
1914
-     * @param string $output_type The current display type (used for product listing)
1915
-     * @param string $special_state_def The value allowing to test if we have to display a special state for the product
1916
-     * @param datetime $special_state_start The start date if applicable for the special state
1917
-     * @param datetime $special_state_end The end date if applicable for the special state
1918
-     *
1919
-     * @return array $product_special_state The product special state
1920
-     */
1921
-    public static function display_product_special_state($special, $output_type, $special_state_def, $special_state_start, $special_state_end)
1922
-    {
1923
-        $product_special_state = array();
1924
-        $product_special_state['output'] = $product_special_state['class'] = '';
1925
-
1926
-        /** Get product special state definition    */
1927
-        $special_state_def = !empty($special_state_def) ? $special_state_def : 'No';
1928
-        $special_state_start = !empty($special_state_start) ? substr($special_state_start, 0, 10) : null;
1929
-        $special_state_end = !empty($special_state_end) ? substr($special_state_end, 0, 10) : null;
1930
-
1931
-        /** Get current time    */
1932
-        $current_time = substr(current_time('mysql', 0), 0, 10);
1933
-
1934
-        /** PRODUCT MARK AS NEW */
1935
-        $show_product_special_state = false;
1936
-        if ((strtolower(__($special_state_def, 'wpshop')) === strtolower(__('Yes', 'wpshop'))) &&
1937
-            (empty($special_state_start) || ($special_state_start == '0000-00-00') || ($special_state_start >= $current_time)) &&
1938
-            (empty($special_state_end) || ($special_state_end == '0000-00-00') || ($special_state_end <= $current_time))) {
1939
-            $show_product_special_state = true;
1940
-        }
1941
-
1942
-        if ($show_product_special_state) {
1943
-            /** Check the type of special output needed    */
1944
-            switch ($special) {
1945
-                case 'declare_new':
1946
-                    $product_special_state['class'] = ' wpshop_product_is_new_' . $output_type;
1947
-                    $template_part = 'product_is_new_sticker';
1948
-                    break;
1949
-
1950
-                case 'highlight_product':
1951
-                    $product_special_state['class'] = ' wpshop_product_featured_' . $output_type;
1952
-                    $template_part = 'product_is_featured_sticker';
1953
-                    break;
1954
-            }
1955
-
1956
-            /** Template parameters    */
1957
-            $tpl_component = array();
1958
-
1959
-            /** Build template        */
1960
-            $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1961
-            if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1962
-                /**    Include the old way template part    */
1963
-                ob_start();
1964
-                require wpshop_display::get_template_file($tpl_way_to_take[1]);
1965
-                $product_special_state['output'] = ob_get_contents();
1966
-                ob_end_clean();
1967
-            } else {
1968
-                $product_special_state['output'] = wpshop_display::display_template_element($template_part, $tpl_component);
1969
-            }
1970
-            unset($tpl_component);
1971
-        }
1972
-
1973
-        return $product_special_state;
1974
-    }
1975
-
1976
-    /**
1977
-     * Prepare product price for saving and easier read later
1978
-     *
1979
-     * @param integer $element_id Identifier of current product
1980
-     */
1981
-    public static function calculate_price($element_id)
1982
-    {
1983
-        global $wpdb;
1984
-
1985
-        /**
1986
-         * Récupères les attributs suivants : product_price, price_ht et tva
1987
-         * par rapport à $element_id / Get the next attributes : product_price,
1988
-         * price_ht and tva
1989
-         */
1990
-        $query = $wpdb->prepare(
1991
-            "SELECT ATTR.code, ATTR_VAL.value_id AS id, ATTR_VAL.value, ATTR.id AS attribute_id
1908
+	}
1909
+
1910
+	/**
1911
+	 * Allows to manage output for special state for a product (New product/highlight product)
1912
+	 *
1913
+	 * @param string $special The type of special type we want to output
1914
+	 * @param string $output_type The current display type (used for product listing)
1915
+	 * @param string $special_state_def The value allowing to test if we have to display a special state for the product
1916
+	 * @param datetime $special_state_start The start date if applicable for the special state
1917
+	 * @param datetime $special_state_end The end date if applicable for the special state
1918
+	 *
1919
+	 * @return array $product_special_state The product special state
1920
+	 */
1921
+	public static function display_product_special_state($special, $output_type, $special_state_def, $special_state_start, $special_state_end)
1922
+	{
1923
+		$product_special_state = array();
1924
+		$product_special_state['output'] = $product_special_state['class'] = '';
1925
+
1926
+		/** Get product special state definition    */
1927
+		$special_state_def = !empty($special_state_def) ? $special_state_def : 'No';
1928
+		$special_state_start = !empty($special_state_start) ? substr($special_state_start, 0, 10) : null;
1929
+		$special_state_end = !empty($special_state_end) ? substr($special_state_end, 0, 10) : null;
1930
+
1931
+		/** Get current time    */
1932
+		$current_time = substr(current_time('mysql', 0), 0, 10);
1933
+
1934
+		/** PRODUCT MARK AS NEW */
1935
+		$show_product_special_state = false;
1936
+		if ((strtolower(__($special_state_def, 'wpshop')) === strtolower(__('Yes', 'wpshop'))) &&
1937
+			(empty($special_state_start) || ($special_state_start == '0000-00-00') || ($special_state_start >= $current_time)) &&
1938
+			(empty($special_state_end) || ($special_state_end == '0000-00-00') || ($special_state_end <= $current_time))) {
1939
+			$show_product_special_state = true;
1940
+		}
1941
+
1942
+		if ($show_product_special_state) {
1943
+			/** Check the type of special output needed    */
1944
+			switch ($special) {
1945
+				case 'declare_new':
1946
+					$product_special_state['class'] = ' wpshop_product_is_new_' . $output_type;
1947
+					$template_part = 'product_is_new_sticker';
1948
+					break;
1949
+
1950
+				case 'highlight_product':
1951
+					$product_special_state['class'] = ' wpshop_product_featured_' . $output_type;
1952
+					$template_part = 'product_is_featured_sticker';
1953
+					break;
1954
+			}
1955
+
1956
+			/** Template parameters    */
1957
+			$tpl_component = array();
1958
+
1959
+			/** Build template        */
1960
+			$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
1961
+			if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
1962
+				/**    Include the old way template part    */
1963
+				ob_start();
1964
+				require wpshop_display::get_template_file($tpl_way_to_take[1]);
1965
+				$product_special_state['output'] = ob_get_contents();
1966
+				ob_end_clean();
1967
+			} else {
1968
+				$product_special_state['output'] = wpshop_display::display_template_element($template_part, $tpl_component);
1969
+			}
1970
+			unset($tpl_component);
1971
+		}
1972
+
1973
+		return $product_special_state;
1974
+	}
1975
+
1976
+	/**
1977
+	 * Prepare product price for saving and easier read later
1978
+	 *
1979
+	 * @param integer $element_id Identifier of current product
1980
+	 */
1981
+	public static function calculate_price($element_id)
1982
+	{
1983
+		global $wpdb;
1984
+
1985
+		/**
1986
+		 * Récupères les attributs suivants : product_price, price_ht et tva
1987
+		 * par rapport à $element_id / Get the next attributes : product_price,
1988
+		 * price_ht and tva
1989
+		 */
1990
+		$query = $wpdb->prepare(
1991
+			"SELECT ATTR.code, ATTR_VAL.value_id AS id, ATTR_VAL.value, ATTR.id AS attribute_id
1992 1992
 			FROM " . WPSHOP_DBT_ATTRIBUTE . " AS ATTR
1993 1993
 				RIGHT JOIN " . WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL . " AS ATTR_VAL ON ((ATTR_VAL.attribute_id = ATTR.id) AND (ATTR_VAL.entity_id = %d))
1994 1994
 			WHERE ATTR.code IN ('" . implode("', '", unserialize(WPSHOP_ATTRIBUTE_PRICES)) . "')",
1995
-            $element_id
1996
-        );
1997
-        $prices_attribute = $wpdb->get_results($query, OBJECT_K);
1998
-
1999
-        /** Si aucun prix trouvé on stop la méthode / If not found price, stop the method */
2000
-        if (empty($prices_attribute)) {
2001
-            return false;
2002
-        } else {
2003
-            /**
2004
-             * Récupère le prix de base selon le pilotage de prix de la boutique / Get
2005
-             * the base amount according on the shop price control
2006
-             */
2007
-            $base_price = $prices_attribute[constant('WPSHOP_PRODUCT_PRICE_' . WPSHOP_PRODUCT_PRICE_PILOT)]->value;
2008
-            $rate_vat = wpshop_prices::get_rate_vat($element_id);
2009
-            $divider_price_ttc = 1 + ($rate_vat->value / 100);
2010
-
2011
-            /**
2012
-             * Informations nécessaire pour crée un attribut / Information needed to
2013
-             * create an attribute
2014
-             */
2015
-            $entity_type_id = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
2016
-            $language = WPSHOP_CURRENT_LOCALE;
2017
-            $icl_post_language = !empty($_REQUEST['icl_post_language']) ? sanitize_text_field($_REQUEST['icl_post_language']) : '';
2018
-
2019
-            if (!empty($icl_post_language)) {
2020
-                $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $icl_post_language);
2021
-                $language = $wpdb->get_var($query);
2022
-            }
2023
-
2024
-            /**
2025
-             * Vérifie la configuration pour savoir comment calculer les prix pour le produit /
2026
-             * Check configuration to know how to make the calcul for the product
2027
-             */
2028
-            if (WPSHOP_PRODUCT_PRICE_PILOT == 'HT') {
2029
-                $price_with_vat = ($base_price * $divider_price_ttc);
2030
-                $price_no_vat = $base_price;
2031
-            } else if (WPSHOP_PRODUCT_PRICE_PILOT == 'TTC') {
2032
-                $price_with_vat = $base_price;
2033
-                $price_no_vat = ($price_with_vat / $divider_price_ttc);
2034
-            }
2035
-
2036
-            /**
2037
-             * Le dernier paramètre permet de ne pas supprimer les attributs du même
2038
-             * type que celui-ci / The last parameter allows not to delete the
2039
-             * attributes of the same type as this one
2040
-             */
2041
-            wpshop_attributes::saveAttributeForEntity(array('decimal' => array('product_price' => $price_with_vat)), $entity_type_id, $element_id, $language, 'wpshop_products');
2042
-            wpshop_attributes::saveAttributeForEntity(array('decimal' => array('price_ht' => $price_no_vat)), $entity_type_id, $element_id, $language, 'wpshop_product');
2043
-
2044
-            /** Ajout ou met à jour de l'attribut tva / Add or update the attribute vat */
2045
-            $vat_amount = $price_with_vat - $price_no_vat;
2046
-            wpshop_attributes::saveAttributeForEntity(array('decimal' => array('tva' => $vat_amount)), $entity_type_id, $element_id, $language, 'wpshop_product');
2047
-
2048
-            /**    Update the product meta information with the calculated prices    */
2049
-            $product_postmeta = get_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, true);
2050
-            $product_postmeta[WPSHOP_PRODUCT_PRICE_TTC] = number_format(round($price_with_vat, 5), 5, '.', '');
2051
-            $product_postmeta[WPSHOP_PRODUCT_PRICE_HT] = number_format(round($price_no_vat, 5), 5, '.', '');
2052
-            $product_postmeta[WPSHOP_PRODUCT_PRICE_TAX_AMOUNT] = number_format(round($vat_amount, 5), 5, '.', '');
2053
-            $product_postmeta[WPSHOP_PRODUCT_PRICE_TAX] = $rate_vat->id;
2054
-
2055
-            update_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $product_postmeta);
2056
-
2057
-            /** Met à jour la meta _wps_price_infos */
2058
-            $p = wpshop_products::get_product_data($element_id);
2059
-            $price = wpshop_prices::get_product_price($p, 'just_price_infos', array('mini_output', 'grid'));
2060
-            update_post_meta($element_id, '_wps_price_infos', $price);
2061
-
2062
-            /** Met à jour la meta _wpshop_displayed_price */
2063
-            wps_filter_search::save_displayed_price_meta($element_id);
2064
-        }
2065
-
2066
-        return true;
2067
-    }
2068
-
2069
-    /**
2070
-     * Allows to get the good button for adding product to cart
2071
-     *
2072
-     * @param integer $product_id The product identifier
2073
-     * @param boolean $productStock If there is the possibility to add the given product to the cart
2074
-     *
2075
-     * @return string $button The html output for the button
2076
-     */
2077
-    public static function display_add_to_cart_button($product_id, $productStock, $output_type = 'mini')
2078
-    {
2079
-        $button = '';
1995
+			$element_id
1996
+		);
1997
+		$prices_attribute = $wpdb->get_results($query, OBJECT_K);
1998
+
1999
+		/** Si aucun prix trouvé on stop la méthode / If not found price, stop the method */
2000
+		if (empty($prices_attribute)) {
2001
+			return false;
2002
+		} else {
2003
+			/**
2004
+			 * Récupère le prix de base selon le pilotage de prix de la boutique / Get
2005
+			 * the base amount according on the shop price control
2006
+			 */
2007
+			$base_price = $prices_attribute[constant('WPSHOP_PRODUCT_PRICE_' . WPSHOP_PRODUCT_PRICE_PILOT)]->value;
2008
+			$rate_vat = wpshop_prices::get_rate_vat($element_id);
2009
+			$divider_price_ttc = 1 + ($rate_vat->value / 100);
2010
+
2011
+			/**
2012
+			 * Informations nécessaire pour crée un attribut / Information needed to
2013
+			 * create an attribute
2014
+			 */
2015
+			$entity_type_id = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
2016
+			$language = WPSHOP_CURRENT_LOCALE;
2017
+			$icl_post_language = !empty($_REQUEST['icl_post_language']) ? sanitize_text_field($_REQUEST['icl_post_language']) : '';
2018
+
2019
+			if (!empty($icl_post_language)) {
2020
+				$query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $icl_post_language);
2021
+				$language = $wpdb->get_var($query);
2022
+			}
2023
+
2024
+			/**
2025
+			 * Vérifie la configuration pour savoir comment calculer les prix pour le produit /
2026
+			 * Check configuration to know how to make the calcul for the product
2027
+			 */
2028
+			if (WPSHOP_PRODUCT_PRICE_PILOT == 'HT') {
2029
+				$price_with_vat = ($base_price * $divider_price_ttc);
2030
+				$price_no_vat = $base_price;
2031
+			} else if (WPSHOP_PRODUCT_PRICE_PILOT == 'TTC') {
2032
+				$price_with_vat = $base_price;
2033
+				$price_no_vat = ($price_with_vat / $divider_price_ttc);
2034
+			}
2035
+
2036
+			/**
2037
+			 * Le dernier paramètre permet de ne pas supprimer les attributs du même
2038
+			 * type que celui-ci / The last parameter allows not to delete the
2039
+			 * attributes of the same type as this one
2040
+			 */
2041
+			wpshop_attributes::saveAttributeForEntity(array('decimal' => array('product_price' => $price_with_vat)), $entity_type_id, $element_id, $language, 'wpshop_products');
2042
+			wpshop_attributes::saveAttributeForEntity(array('decimal' => array('price_ht' => $price_no_vat)), $entity_type_id, $element_id, $language, 'wpshop_product');
2043
+
2044
+			/** Ajout ou met à jour de l'attribut tva / Add or update the attribute vat */
2045
+			$vat_amount = $price_with_vat - $price_no_vat;
2046
+			wpshop_attributes::saveAttributeForEntity(array('decimal' => array('tva' => $vat_amount)), $entity_type_id, $element_id, $language, 'wpshop_product');
2047
+
2048
+			/**    Update the product meta information with the calculated prices    */
2049
+			$product_postmeta = get_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, true);
2050
+			$product_postmeta[WPSHOP_PRODUCT_PRICE_TTC] = number_format(round($price_with_vat, 5), 5, '.', '');
2051
+			$product_postmeta[WPSHOP_PRODUCT_PRICE_HT] = number_format(round($price_no_vat, 5), 5, '.', '');
2052
+			$product_postmeta[WPSHOP_PRODUCT_PRICE_TAX_AMOUNT] = number_format(round($vat_amount, 5), 5, '.', '');
2053
+			$product_postmeta[WPSHOP_PRODUCT_PRICE_TAX] = $rate_vat->id;
2054
+
2055
+			update_post_meta($element_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $product_postmeta);
2056
+
2057
+			/** Met à jour la meta _wps_price_infos */
2058
+			$p = wpshop_products::get_product_data($element_id);
2059
+			$price = wpshop_prices::get_product_price($p, 'just_price_infos', array('mini_output', 'grid'));
2060
+			update_post_meta($element_id, '_wps_price_infos', $price);
2061
+
2062
+			/** Met à jour la meta _wpshop_displayed_price */
2063
+			wps_filter_search::save_displayed_price_meta($element_id);
2064
+		}
2065
+
2066
+		return true;
2067
+	}
2068
+
2069
+	/**
2070
+	 * Allows to get the good button for adding product to cart
2071
+	 *
2072
+	 * @param integer $product_id The product identifier
2073
+	 * @param boolean $productStock If there is the possibility to add the given product to the cart
2074
+	 *
2075
+	 * @return string $button The html output for the button
2076
+	 */
2077
+	public static function display_add_to_cart_button($product_id, $productStock, $output_type = 'mini')
2078
+	{
2079
+		$button = '';
2080 2080
 //         $attributes_frontend_display = get_post_meta( $product_id, '_wpshop_product_attributes_frontend_display', true );
2081 2081
 
2082
-        if (WPSHOP_DEFINED_SHOP_TYPE == 'sale' /*&& ( empty($attributes_frontend_display) || ( !empty($attributes_frontend_display) && !empty($attributes_frontend_display['product_action_button']) && !empty($attributes_frontend_display['product_action_button']['mini_output']) && $output_type == 'mini') || ( !empty($attributes_frontend_display) && !empty($attributes_frontend_display['product_action_button']) && !empty($attributes_frontend_display['product_action_button']['complete_sheet']) && $output_type == 'complete') ) */) {
2083
-            /*
2082
+		if (WPSHOP_DEFINED_SHOP_TYPE == 'sale' /*&& ( empty($attributes_frontend_display) || ( !empty($attributes_frontend_display) && !empty($attributes_frontend_display['product_action_button']) && !empty($attributes_frontend_display['product_action_button']['mini_output']) && $output_type == 'mini') || ( !empty($attributes_frontend_display) && !empty($attributes_frontend_display['product_action_button']) && !empty($attributes_frontend_display['product_action_button']['complete_sheet']) && $output_type == 'complete') ) */) {
2083
+			/*
2084 2084
              * Check if current product has variation for button display
2085 2085
              */
2086
-            $variations_exists = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'post_parent' => $product_id));
2087
-            $variations_list = (!empty($variations_exists) && is_array(wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode)))) ? true : false;
2086
+			$variations_exists = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, 'post_parent' => $product_id));
2087
+			$variations_list = (!empty($variations_exists) && is_array(wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode)))) ? true : false;
2088 2088
 
2089
-            /*
2089
+			/*
2090 2090
              * Template parameters
2091 2091
              */
2092 2092
 
2093
-            if (get_post_type($product_id) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) {
2094
-                /** Check variation stock **/
2095
-                $variations = self::get_variation($product_id);
2096
-                if (!empty($variations)) {
2097
-                    foreach ($variations as $variation) {
2098
-                        if (!empty($variation) && !empty($variation['variation_dif']) && !empty($variation['variation_dif']['product_stock'])) {
2099
-                            $productStock += $variation['variation_dif']['product_stock'];
2100
-                        }
2101
-                    }
2102
-                }
2103
-            }
2104
-
2105
-            $template_part = ($variations_list && ($output_type == 'mini')) ? 'configure_product_button' : (!empty($productStock) ? 'add_to_cart_button' : 'unavailable_product_button');
2093
+			if (get_post_type($product_id) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) {
2094
+				/** Check variation stock **/
2095
+				$variations = self::get_variation($product_id);
2096
+				if (!empty($variations)) {
2097
+					foreach ($variations as $variation) {
2098
+						if (!empty($variation) && !empty($variation['variation_dif']) && !empty($variation['variation_dif']['product_stock'])) {
2099
+							$productStock += $variation['variation_dif']['product_stock'];
2100
+						}
2101
+					}
2102
+				}
2103
+			}
2104
+
2105
+			$template_part = ($variations_list && ($output_type == 'mini')) ? 'configure_product_button' : (!empty($productStock) ? 'add_to_cart_button' : 'unavailable_product_button');
2106 2106
 						$template_part = apply_filters( 'wps_custom_add_to_cart_button', $template_part, $product_id );
2107 2107
 
2108
-            $tpl_component = array();
2109
-            $tpl_component['PRODUCT_ID'] = $product_id;
2110
-            $tpl_component['PRODUCT_PERMALINK'] = get_permalink($product_id);
2111
-            $tpl_component['PRODUCT_TITLE'] = get_the_title($product_id);
2112
-
2113
-            /*
2114
-             * Build template
2115
-             */
2116
-            $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2117
-            if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2118
-                /*    Include the old way template part    */
2119
-                ob_start();
2120
-                require wpshop_display::get_template_file($tpl_way_to_take[1]);
2121
-                $button = ob_get_contents();
2122
-                ob_end_clean();
2123
-            } else {
2124
-                $button = wpshop_display::display_template_element($template_part, $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $product_id, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . 'output_type' => $output_type));
2125
-            }
2126
-            unset($tpl_component);
2127
-        }
2128
-        return $button;
2129
-    }
2130
-
2131
-    /**
2132
-     * Allows to get the good button for adding product to a quotation
2133
-     *
2134
-     * @param integer $product_id The product identifier
2135
-     * @param boolean $product_quotation_state The state of the quotation addons
2136
-     *
2137
-     * @return string $button The html output for the button
2138
-     */
2139
-    public static function display_quotation_button($product_id, $product_quotation_state, $output_type = 'mini')
2140
-    {
2141
-        $quotation_button = '';
2142
-
2143
-        if (WPSHOP_ADDONS_QUOTATION && (!empty($product_quotation_state) && strtolower(__($product_quotation_state, 'wpshop')) == strtolower(__('Yes', 'wpshop'))) && (empty($_SESSION['cart']['cart_type']) || ($_SESSION['cart']['cart_type'] == 'quotation'))) {
2144
-            $variations_list = (is_array(wpshop_products::get_variation($product_id)) && is_array(wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode)))) ? array_merge(wpshop_products::get_variation($product_id), wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode))) : array();
2145
-            /**
2146
-             * Template parameters
2147
-             */
2148
-            $template_part = (!empty($variations_list) && ($output_type == 'mini')) ? 'configure_quotation_button' : 'ask_quotation_button';
2149
-            $tpl_component = array();
2150
-            $tpl_component['PRODUCT_ID'] = $product_id;
2151
-            $tpl_component['PRODUCT_PERMALINK'] = get_permalink($product_id);
2152
-            $tpl_component['PRODUCT_TITLE'] = get_the_title($product_id);
2108
+			$tpl_component = array();
2109
+			$tpl_component['PRODUCT_ID'] = $product_id;
2110
+			$tpl_component['PRODUCT_PERMALINK'] = get_permalink($product_id);
2111
+			$tpl_component['PRODUCT_TITLE'] = get_the_title($product_id);
2153 2112
 
2154
-            /**
2113
+			/*
2155 2114
              * Build template
2156 2115
              */
2157
-            $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2158
-            if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2159
-                /*    Include the old way template part    */
2160
-                ob_start();
2161
-                require wpshop_display::get_template_file($tpl_way_to_take[1]);
2162
-                $quotation_button = ob_get_contents();
2163
-                ob_end_clean();
2164
-            } else {
2165
-                $quotation_button = wpshop_display::display_template_element($template_part, $tpl_component);
2166
-            }
2167
-            unset($tpl_component);
2168
-        }
2169
-
2170
-        return $quotation_button;
2171
-    }
2172
-
2173
-    /**
2174
-     * Return the output for a product attachement gallery (picture or document)
2175
-     *
2176
-     * @param string $attachement_type The type of attachement to output. allows to define with type of template to take
2177
-     * @param string $content The gallery content build previously
2178
-     *
2179
-     * @return string The attachement gallery output
2180
-     */
2181
-    public static function display_attachment_gallery($attachement_type, $content)
2182
-    {
2183
-        $galery_output = '';
2184
-
2185
-        /*
2116
+			$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2117
+			if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2118
+				/*    Include the old way template part    */
2119
+				ob_start();
2120
+				require wpshop_display::get_template_file($tpl_way_to_take[1]);
2121
+				$button = ob_get_contents();
2122
+				ob_end_clean();
2123
+			} else {
2124
+				$button = wpshop_display::display_template_element($template_part, $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $product_id, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . 'output_type' => $output_type));
2125
+			}
2126
+			unset($tpl_component);
2127
+		}
2128
+		return $button;
2129
+	}
2130
+
2131
+	/**
2132
+	 * Allows to get the good button for adding product to a quotation
2133
+	 *
2134
+	 * @param integer $product_id The product identifier
2135
+	 * @param boolean $product_quotation_state The state of the quotation addons
2136
+	 *
2137
+	 * @return string $button The html output for the button
2138
+	 */
2139
+	public static function display_quotation_button($product_id, $product_quotation_state, $output_type = 'mini')
2140
+	{
2141
+		$quotation_button = '';
2142
+
2143
+		if (WPSHOP_ADDONS_QUOTATION && (!empty($product_quotation_state) && strtolower(__($product_quotation_state, 'wpshop')) == strtolower(__('Yes', 'wpshop'))) && (empty($_SESSION['cart']['cart_type']) || ($_SESSION['cart']['cart_type'] == 'quotation'))) {
2144
+			$variations_list = (is_array(wpshop_products::get_variation($product_id)) && is_array(wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode)))) ? array_merge(wpshop_products::get_variation($product_id), wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode))) : array();
2145
+			/**
2146
+			 * Template parameters
2147
+			 */
2148
+			$template_part = (!empty($variations_list) && ($output_type == 'mini')) ? 'configure_quotation_button' : 'ask_quotation_button';
2149
+			$tpl_component = array();
2150
+			$tpl_component['PRODUCT_ID'] = $product_id;
2151
+			$tpl_component['PRODUCT_PERMALINK'] = get_permalink($product_id);
2152
+			$tpl_component['PRODUCT_TITLE'] = get_the_title($product_id);
2153
+
2154
+			/**
2155
+			 * Build template
2156
+			 */
2157
+			$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2158
+			if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2159
+				/*    Include the old way template part    */
2160
+				ob_start();
2161
+				require wpshop_display::get_template_file($tpl_way_to_take[1]);
2162
+				$quotation_button = ob_get_contents();
2163
+				ob_end_clean();
2164
+			} else {
2165
+				$quotation_button = wpshop_display::display_template_element($template_part, $tpl_component);
2166
+			}
2167
+			unset($tpl_component);
2168
+		}
2169
+
2170
+		return $quotation_button;
2171
+	}
2172
+
2173
+	/**
2174
+	 * Return the output for a product attachement gallery (picture or document)
2175
+	 *
2176
+	 * @param string $attachement_type The type of attachement to output. allows to define with type of template to take
2177
+	 * @param string $content The gallery content build previously
2178
+	 *
2179
+	 * @return string The attachement gallery output
2180
+	 */
2181
+	public static function display_attachment_gallery($attachement_type, $content)
2182
+	{
2183
+		$galery_output = '';
2184
+
2185
+		/*
2186 2186
          * Get the template part for given galery type
2187 2187
          */
2188
-        switch ($attachement_type) {
2189
-            case 'picture':
2190
-                $template_part = 'product_attachment_picture_galery';
2191
-                break;
2192
-            case 'document':
2193
-                $template_part = 'product_attachment_galery';
2194
-                break;
2195
-        }
2196
-
2197
-        /*
2188
+		switch ($attachement_type) {
2189
+			case 'picture':
2190
+				$template_part = 'product_attachment_picture_galery';
2191
+				break;
2192
+			case 'document':
2193
+				$template_part = 'product_attachment_galery';
2194
+				break;
2195
+		}
2196
+
2197
+		/*
2198 2198
          * Template parameters
2199 2199
          */
2200
-        $tpl_component = array();
2201
-        $tpl_component['PRODUCT_ATTACHMENT_OUTPUT_CONTENT'] = $content;
2202
-        $tpl_component['ATTACHMENT_ITEM_TYPE'] = $attachement_type;
2200
+		$tpl_component = array();
2201
+		$tpl_component['PRODUCT_ATTACHMENT_OUTPUT_CONTENT'] = $content;
2202
+		$tpl_component['ATTACHMENT_ITEM_TYPE'] = $attachement_type;
2203 2203
 
2204
-        /*
2204
+		/*
2205 2205
          * Build template
2206 2206
          */
2207
-        $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2208
-        if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2209
-            /*    Include the old way template part    */
2210
-            ob_start();
2211
-            require wpshop_display::get_template_file($tpl_way_to_take[1]);
2212
-            $galery_output = ob_get_contents();
2213
-            ob_end_clean();
2214
-        } else {
2215
-            $galery_output = wpshop_display::display_template_element($template_part, $tpl_component);
2216
-        }
2217
-        unset($tpl_component);
2218
-
2219
-        return $galery_output;
2220
-    }
2221
-
2222
-    /**
2223
-     * Define the metabox to display in product edition page in backend
2224
-     * @param object $post The current element displayed for edition
2225
-     */
2226
-    public static function meta_box_variations($post)
2227
-    {
2228
-        $output = '';
2229
-        /*    Variations container    */
2230
-        $tpl_component = array();
2231
-        $tpl_component['ADMIN_VARIATION_CONTAINER'] = self::display_variation_admin($post->ID);
2232
-        $tpl_component['LINK_NEW_INTERFACE'] = wp_nonce_url(get_edit_post_link($post->ID) . '&wps_variation_interface=true', 'wps_remove_variation_interface');
2233
-        $output .= wpshop_display::display_template_element('wpshop_admin_variation_metabox', $tpl_component, array(), 'admin');
2234
-        echo '<span class="wpshop_loading_ wpshopHide" ><img src="' . admin_url('images/loading.gif') . '" alt="loading picture" /></span>' . $output . '<div class="wpshop_cls" ></div>';
2235
-    }
2236
-
2237
-    /**
2238
-     * Call variation creation function with a list of defined variation
2239
-     *
2240
-     * @param array $possible_variations A list of variation to create for the current element
2241
-     * @param integer $element_id The product we want to create variation for
2242
-     *
2243
-     * @return mixed The last created variation identifier
2244
-     */
2245
-    public static function creation_variation_callback($possible_variations, $element_id)
2246
-    {
2247
-        /** Get existing variation    */
2248
-        $existing_variations_in_db = wpshop_products::get_variation($element_id);
2249
-        $existing_variations = array();
2250
-        if (!empty($existing_variations_in_db)) {
2251
-            foreach ($existing_variations_in_db as $variations_def) {
2252
-                $existing_variations[] = $variations_def['variation_def'];
2253
-            }
2254
-        }
2255
-        /** New variation definition    */
2256
-        $attribute_defining_variation = get_post_meta($element_id, '_wpshop_variation_defining', true);
2257
-
2258
-        /**    Read possible values    */
2259
-        foreach ($possible_variations as $variation_definition) {
2260
-            if (in_array($variation_definition, $existing_variations)) {
2261
-                continue;
2262
-            }
2263
-
2264
-            $attribute_to_set = array();
2265
-            foreach ($variation_definition as $attribute_code => $attribute_selected_value) {
2266
-                $attribute = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2267
-                $attribute_to_set[$attribute->data_type][$attribute_code] = $attribute_selected_value;
2268
-                if (empty($attribute_defining_variation['attributes']) || (!in_array($attribute_code, $attribute_defining_variation['attributes']))) {
2269
-                    $attribute_defining_variation['attributes'][] = $attribute_code;
2270
-                }
2271
-            }
2272
-            $variation_id = wpshop_products::create_variation($element_id, $attribute_to_set);
2273
-        }
2274
-        update_post_meta($element_id, '_wpshop_variation_defining', $attribute_defining_variation);
2275
-
2276
-        return !empty($variation_id) ? $variation_id : 0;
2277
-    }
2278
-
2279
-    /**
2280
-     * Create a new variation for product
2281
-     *
2282
-     * @param integer $head_product The product identifier to create the new variation for
2283
-     * @param array $variation_attributes Attribute list for the variation
2284
-     *
2285
-     * @return mixed <number, WP_Error> The variation identifier or an error in case the creation was not succesfull
2286
-     */
2287
-    public static function create_variation($head_product, $variation_attributes)
2288
-    {
2289
-        /** Create custom title */
2290
-        $title_variation = "";
2291
-        if (!empty($variation_attributes)) {
2292
-            foreach ($variation_attributes as $type => $array) {
2293
-                if (!empty($array)) {
2294
-                    foreach ($array as $code => $value) {
2295
-                        $title_variation .= " " . $code . " " . $value;
2296
-                    }
2297
-                }
2298
-            }
2299
-        }
2300
-
2301
-        $variation = array(
2302
-            'post_title' => sprintf(__('Product %s variation %s', 'wpshop'), $head_product, get_the_title($head_product)) . $title_variation,
2303
-            'post_content' => '',
2304
-            'post_status' => 'publish',
2305
-            'post_author' => get_current_user_id(),
2306
-            'post_parent' => $head_product,
2307
-            'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
2308
-        );
2309
-        $variation_id = wp_insert_post($variation);
2310
-
2311
-        wpshop_attributes::saveAttributeForEntity($variation_attributes, wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $variation_id, WPSHOP_CURRENT_LOCALE, '');
2312
-
2313
-        /*    Update product price looking for shop parameters    */
2314
-        wpshop_products::calculate_price($variation_id);
2315
-
2316
-        /*    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
2317
-        $productMetaDatas = array();
2318
-        foreach ($variation_attributes as $attributeType => $attributeValues) {
2319
-            foreach ($attributeValues as $attributeCode => $attributeValue) {
2320
-                if (!empty($attributeValue)) {
2321
-                    $productMetaDatas[$attributeCode] = $attributeValue;
2322
-                }
2323
-            }
2324
-        }
2325
-        update_post_meta($variation_id, '_wpshop_variations_attribute_def', $productMetaDatas);
2326
-        update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
2327
-        update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, get_post_meta($head_product, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
2328
-
2329
-        return $variation_id;
2330
-    }
2331
-
2332
-    /**
2333
-     * Get variation list for a given product
2334
-     *
2335
-     * @param integer $head_product The product identifier to get the variation for
2336
-     * @return object The variation list
2337
-     */
2338
-    public static function get_variation($head_product)
2339
-    {
2340
-        global $wpdb;
2341
-        $variations_output = null;
2342
-        $args = array(
2343
-            'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
2344
-            'post_parent' => $head_product,
2345
-            'orderby' => 'ID',
2346
-            'order' => 'ASC',
2347
-            'posts_per_page' => -1,
2348
-            'post_status' => 'any',
2349
-        );
2350
-        $variations = get_posts($args);
2351
-
2352
-        if (!empty($variations)) {
2353
-            $head_wpshop_variation_definition = get_post_meta($head_product, '_wpshop_variation_defining', true);
2354
-
2355
-            foreach ($variations as $post_def) {
2356
-                $data = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $post_def->ID, WPSHOP_CURRENT_LOCALE, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, "'valid', 'deleted'");
2357
-                foreach ($data as $content) {
2358
-                    $attribute_value = 'attribute_value_' . $content->data_type;
2359
-                    if (!empty($content->$attribute_value)) {
2360
-                        if (!empty($head_wpshop_variation_definition['attributes']) && in_array($content->code, $head_wpshop_variation_definition['attributes'])) {
2361
-                            $variations_output[$post_def->ID]['variation_def'][$content->code] = $content->$attribute_value;
2362
-                        } else {
2363
-                            $variations_output[$post_def->ID]['variation_dif'][$content->code] = $content->$attribute_value;
2364
-                        }
2365
-                    }
2366
-                }
2367
-                $variations_output[$post_def->ID]['post'] = $post_def;
2368
-            }
2369
-        }
2370
-        wp_reset_query();
2371
-        return $variations_output;
2372
-    }
2373
-
2374
-    /**
2375
-     * Affichage des variations d'un produit dans l'administration
2376
-     *
2377
-     * @param integer $head_product L'identifiant du produit dont on veut afficher les variations
2378
-     * @return string Le code html permettant l'affichage des variations dans l'interface d'édition du produit
2379
-     */
2380
-    public static function display_variation_admin($head_product)
2381
-    {
2382
-        $output = '';
2383
-        $productCurrency = wpshop_tools::wpshop_get_currency();
2384
-        /*    Récupération de la liste des variations pour le produit en cours d'édition    */
2385
-        $variations = self::get_variation($head_product);
2386
-        $price_piloting = get_option('wpshop_shop_price_piloting');
2387
-
2388
-        /*    Affichage de la liste des variations pour le produit en cours d'édition    */
2389
-        if (!empty($variations) && is_array($variations)) {
2390
-            $existing_variation_list = wpshop_display::display_template_element('wpshop_admin_existing_variation_controller', array(), array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product), 'admin');
2391
-
2392
-            foreach ($variations as $variation) {
2393
-                $tpl_component = array();
2394
-
2395
-                $tpl_component['ADMIN_EXISTING_VARIATIONS_CLASS'] = ' wpshop_variation_' . self::currentPageCode;
2396
-                $tpl_component['VARIATION_IDENTIFIER'] = $variation['post']->ID;
2397
-                $tpl_component['VARIATION_DETAIL'] = '  ';
2398
-                $p = (!empty($variation['variation_dif']['product_price']) || !empty($variation['variation_dif']['price_ht'])) ? ((!empty($price_piloting) && $price_piloting == 'HT' && !empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : (!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0) : 0;
2399
-                $tpl_component['VARIATION_DETAIL_PRICE'] = number_format($p, 2, '.', '') . ' ' . $productCurrency . ' ' . ((!empty($price_piloting) && $price_piloting == 'HT') ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
2400
-                if (!empty($price_piloting) && $price_piloting == 'HT') {
2401
-
2402
-                } else {
2403
-
2404
-                }
2405
-                $post_obj = $variation['post'];
2406
-
2407
-                $parent_product_infos = wpshop_products::get_parent_variation($post_obj->ID);
2408
-                if (!empty($parent_product_infos)) {
2409
-                    $parent_post = $parent_product_infos['parent_post'];
2410
-                    $product_option_postmeta = get_post_meta($parent_post->ID, '_wpshop_variation_defining', true);
2411
-                    if (!empty($product_option_postmeta['options']['price_behaviour']) && !empty($product_option_postmeta['options']['price_behaviour'][0]) && $product_option_postmeta['options']['price_behaviour'][0] == 'addition') {
2412
-                        if (!empty($price_piloting) && $price_piloting == 'HT') {
2413
-                            $product_price = ((!empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : 0) + $parent_product_infos['parent_post_meta']['price_ht'];
2414
-                        } else {
2415
-                            $product_price = ((!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0) + $parent_product_infos['parent_post_meta']['product_price'];
2416
-                        }
2417
-                        $tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Variation price combined with the parent product price', 'wpshop');
2418
-                    } else {
2419
-                        if (!empty($price_piloting) && $price_piloting == 'HT') {
2420
-                            $product_price = (!empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : 0;
2421
-                        } else {
2422
-                            $product_price = (!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0;
2423
-                        }
2424
-                        $tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Only variation\'s price is used', 'wpshop');
2425
-                    }
2426
-                    $product_price = number_format(str_replace(',', '.', $product_price), 2, '.', '') . ' ' . $productCurrency . ' ' . ((!empty($price_piloting) && $price_piloting == 'HT') ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
2427
-                    $tpl_component['VARIATION_DETAIL_SALE_PRICE'] = $product_price;
2428
-                }
2429
-
2430
-                if (!empty($variation['variation_def'])) {
2431
-                    foreach ($variation['variation_def'] as $variation_key => $variation_value) {
2432
-                        if (!empty($variation_value)) {
2433
-                            $attribute_def_for_variation = wpshop_attributes::getElement($variation_key, "'valid'", 'code');
2434
-                            $tpl_component['VARIATION_DETAIL'] .= '<input type="hidden" name="' . self::current_page_variation_code . '[' . $variation['post']->ID . '][attribute][' . $attribute_def_for_variation->data_type . '][' . $variation_key . ']" value="' . $variation_value . '" />' . wpshop_display::display_template_element('wpshop_admin_variation_item_def_header', array('VARIATION_ATTRIBUTE_CODE' => $attribute_def_for_variation->frontend_label, 'VARIATION_ATTRIBUTE_CODE_VALUE' => stripslashes(wpshop_attributes::get_attribute_type_select_option_info($variation_value, 'label', $attribute_def_for_variation->data_type_to_use, true))), array(), 'admin');
2435
-                            $tpl_component['VARIATION_IMAGE_CHOICE'] = '';
2436
-
2437
-                            /** Define Link image to variation interface **/
2438
-                            if (!empty($product_option_postmeta) && !empty($product_option_postmeta['attributes']) && !empty($product_option_postmeta['variation_type']) && ((count($product_option_postmeta['attributes']) == 1 && $product_option_postmeta['variation_type'] == 'single') || ($product_option_postmeta['variation_type'] == 'combined'))) {
2439
-                                $pictures = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $head_product));
2440
-
2441
-                                $media_id_data = get_post_meta($head_product, '_wps_product_media', true);
2442
-                                if (!empty($media_id_data)) {
2443
-                                    $medias_ids = explode(',', $media_id_data);
2444
-
2445
-                                    if (!empty($medias_ids)) {
2446
-                                        foreach ($medias_ids as $media_id) {
2447
-                                            if (!empty($media_id)) {
2448
-                                                $pictures[] = get_post($media_id);
2449
-                                            }
2450
-                                        }
2451
-                                    }
2452
-                                }
2453
-
2454
-                                $pictures_data = '';
2455
-                                if (!empty($pictures)) {
2456
-                                    $selected_picture = get_post_meta($variation['post']->ID, '_wps_variation_attached_picture', true);
2457
-
2458
-                                    $done_picture = array();
2459
-                                    foreach ($pictures as $picture) {
2460
-                                        if (!in_array($picture->ID, $done_picture)) {
2461
-                                            if (wp_attachment_is_image($picture->ID)) {
2462
-                                                $checked = ((!empty($selected_picture) && $selected_picture == $picture->ID) ? 'checked="checked"' : '');
2463
-                                                $pictures_data .= wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_element', array('PICTURE_CHOICE_VARIATION_ID' => $picture->ID, 'PRODUCT_VARIATION_ID' => $variation['post']->ID, 'PICTURE_CHOICE_SELECTED' => $checked, 'PICTURE_CHOICE_VARIATION_IMG' => wp_get_attachment_image($picture->ID, 'thumbnail')), array(), 'admin');
2464
-
2465
-                                                $done_picture[] = $picture->ID;
2466
-                                            }
2467
-                                        }
2468
-                                    }
2469
-                                }
2470
-
2471
-                                $tpl_component['VARIATION_IMAGE_CHOICE'] = wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_container', array('PICTURE_CHOICE_CONTAINER_CONTENT' => $pictures_data), array(), 'admin');
2472
-                            }
2473
-
2474
-                        }
2475
-                    }
2476
-                }
2477
-
2478
-                $tpl_component['VARIATION_DETAIL'] = substr($tpl_component['VARIATION_DETAIL'], 0, -2);
2479
-
2480
-                $tpl_component['ADMIN_VARIATION_SPECIFIC_DEFINITION_CONTAINER_CLASS'] = ' wpshopHide';
2481
-                $tpl_component['VARIATION_DEFINITION'] = wpshop_attributes::get_variation_attribute(array('post_id' => $variation['post']->ID, 'input_class' => ' ', 'field_name' => wpshop_products::current_page_variation_code . '[' . $variation['post']->ID . ']', 'page_code' => self::current_page_variation_code, 'field_id' => self::current_page_variation_code . '_' . $variation['post']->ID, 'variation_dif_values' => (!empty($variation['variation_dif']) ? $variation['variation_dif'] : array())));
2482
-                $tpl_component['VARIATION_DEFINITION_CONTENT'] = wpshop_display::display_template_element('wpshop_admin_variation_item_specific_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
2483
-
2484
-                /*    Add the variation definition to output    */
2485
-                $existing_variation_list .= wpshop_display::display_template_element('wpshop_admin_variation_item_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
2486
-            }
2487
-
2488
-            $output .= wpshop_display::display_template_element('wpshop_admin_existing_variation_list', array('ADMIN_EXISTING_VARIATIONS_CONTAINER_CLASS' => '', 'ADMIN_EXISTING_VARIATIONS_CONTAINER' => $existing_variation_list), array(), 'admin');
2489
-            /*    Reset de la liste des résultats pour éviter les comportements indésirables    */
2490
-            wp_reset_query();
2491
-        } else {
2492
-            $output = __('No variation found for this product. Please use button above for create one', 'wpshop');
2493
-        }
2494
-
2495
-        return $output;
2496
-    }
2497
-
2498
-    /**
2499
-     * Retrieve and display the variation for a given product
2500
-     * @param integer $product_id The product identifier to get variation for
2501
-     */
2502
-    public static function wpshop_variation($post_id = '', $from_admin = false, $order_id = '', $qty = 1)
2503
-    {
2504
-        global $wp_query;
2505
-        $output = '';
2506
-
2507
-        $product_id = empty($post_id) ? $wp_query->post->ID : $post_id;
2508
-        $wpshop_product_attributes_frontend_display = get_post_meta($product_id, '_wpshop_product_attributes_frontend_display', true);
2509
-        $head_wpshop_variation_definition = get_post_meta($product_id, '_wpshop_variation_defining', true);
2510
-
2511
-        /**    Get attribute order for current product    */
2512
-        $product_attribute_order_detail = wpshop_attributes_set::getAttributeSetDetails(get_post_meta($product_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
2513
-        $output_order = array();
2514
-        if (count($product_attribute_order_detail) > 0) {
2515
-            if (!empty($product_attribute_order_detail)) {
2516
-                foreach ($product_attribute_order_detail as $product_attr_group_id => $product_attr_group_detail) {
2517
-                    foreach ($product_attr_group_detail['attribut'] as $position => $attribute_def) {
2518
-                        if (!empty($attribute_def->code)) {
2519
-                            $output_order[$attribute_def->code] = $position;
2520
-                        }
2521
-
2522
-                    }
2523
-                }
2524
-            }
2525
-        }
2526
-
2527
-        $variations_params = array();
2528
-        $variation_attribute = array();
2529
-        $variation_attribute_ordered = array();
2530
-        $possible_values = array();
2531
-        $possible_values_for_selection_calculation = array();
2532
-
2533
-        /*    Vérification de l'existence de déclinaison pour le produit    */
2534
-        $wpshop_variation_list = self::get_variation($product_id);
2535
-        if (!empty($wpshop_variation_list)) {
2536
-            foreach ($wpshop_variation_list as $variation) {
2537
-                if (!empty($variation['variation_def'])) {
2538
-                    $display_option = get_post_meta($post_id, '_wpshop_product_attributes_frontend_display', true);
2539
-                    foreach ($variation['variation_def'] as $attribute_code => $attribute_value) {
2540
-                        if (empty($display_option) || (!empty($display_option['attribute']) && !empty($display_option['attribute'][$attribute_code]) && !empty($display_option['attribute'][$attribute_code]['complete_sheet']))) {
2541
-                            $tpl_component = array();
2542
-
2543
-                            $attribute_db_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2544
-                            $default_value_is_serial = false;
2545
-                            $attribute_list_first_element = $attribute_db_definition->default_value;
2546
-                            if (!empty($attribute_db_definition->default_value) && ($attribute_db_definition->default_value == serialize(false) || wpshop_tools::is_serialized($attribute_db_definition->default_value))) {
2547
-                                $default_value_is_serial = true;
2548
-                                $tmp_default_value = unserialize($attribute_db_definition->default_value);
2549
-                                $attribute_list_first_element = !empty($tmp_default_value['field_options']['label_for_first_item']) ? $tmp_default_value['field_options']['label_for_first_item'] : null;
2550
-                            }
2551
-
2552
-                            if ($default_value_is_serial && !empty($attribute_list_first_element) && ($attribute_list_first_element != 'none')) {
2553
-                                $possible_values[$attribute_code][0][0] = ($default_value_is_serial && !empty($attribute_list_first_element) && ($attribute_list_first_element != 'none')) ? stripslashes(sprintf($attribute_list_first_element, strtolower($attribute_db_definition->frontend_label))) : __('Choose a value', 'wpshop');
2554
-                            }
2555
-
2556
-                            if (!empty($attribute_value) && ($attribute_db_definition->data_type_to_use == 'custom')) {
2557
-                                $tpl_component['VARIATION_VALUE'] = stripslashes(wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', 'custom'));
2558
-                                $position = wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'position', 'custom');
2559
-                            } else if (!empty($attribute_value) && ($attribute_db_definition->data_type_to_use == 'internal')) {
2560
-                                $post_def = get_post($attribute_value);
2561
-                                $tpl_component['VARIATION_VALUE'] = stripslashes($post_def->post_title);
2562
-                                $position = $post_def->menu_order;
2563
-                            }
2564
-
2565
-                            if (!empty($variation['variation_dif'])) {
2566
-                                foreach ($variation['variation_dif'] as $attribute_dif_code => $attribute_dif_value) {
2567
-                                    $wpshop_prices_attributes = unserialize(WPSHOP_ATTRIBUTE_PRICES);
2568
-                                    $the_value = $attribute_dif_value;
2569
-                                    if (in_array($attribute_dif_code, $wpshop_prices_attributes)) {
2570
-                                        $the_value = wpshop_display::format_field_output('wpshop_product_price', $attribute_dif_value);
2571
-                                    }
2572
-                                    $tpl_component['VARIATION_DIF_' . strtoupper($attribute_dif_code)] = stripslashes($the_value);
2573
-                                }
2574
-                            }
2575
-                            if (!empty($attribute_value)) {
2576
-                                $possible_values[$attribute_code][$position][$attribute_value] = wpshop_display::display_template_element('product_variation_item_possible_values', $tpl_component, array('type' => 'attribute_for_variation', 'id' => $attribute_code));
2577
-                                $possible_values_for_selection_calculation[$attribute_code][$attribute_value] = $tpl_component['VARIATION_VALUE'];
2578
-                            }
2579
-                            unset($tpl_component);
2580
-                        }
2581
-                    }
2582
-                }
2583
-            }
2584
-
2585
-            $variation_tpl = array();
2586
-            if (!empty($head_wpshop_variation_definition['attributes'])) {
2587
-                foreach ($head_wpshop_variation_definition['attributes'] as $attribute_code) {
2588
-                    $attribute_db_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2589
-
2590
-                    if (!empty($attribute_db_definition)) {
2591
-                        $attribute_display_state = wpshop_attributes::check_attribute_display($attribute_db_definition->is_visible_in_front, $wpshop_product_attributes_frontend_display, 'attribute', $attribute_code, 'complete_sheet');
2592
-
2593
-                        $is_required = ((!empty($head_wpshop_variation_definition['options']) && !empty($head_wpshop_variation_definition['options']['required_attributes']) && (in_array($attribute_code, $head_wpshop_variation_definition['options']['required_attributes'])))) ? true : false;
2594
-                        if (!$is_required && $attribute_db_definition->is_required == 'yes') {
2595
-                            $is_required = true;
2596
-                        }
2597
-
2598
-                        $input_def = array();
2599
-                        $input_def['type'] = $attribute_db_definition->frontend_input;
2600
-                        $value = isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code] : (!empty($attribute_db_definition->default_value) ? $attribute_db_definition->default_value : null);
2601
-                        if (in_array($attribute_db_definition->frontend_input, array('radio', 'checkbox'))) {
2602
-                            unset($possible_values[$attribute_code][0]);
2603
-                            $value = array($value);
2604
-                        }
2605
-                        $input_def['id'] = 'wpshop_variation_attr_' . $attribute_code;
2606
-                        $input_def['name'] = $attribute_code;
2607
-                        $real_possible_values = array();
2608
-                        if (!empty($possible_values[$attribute_code])) {
2609
-                            ksort($possible_values[$attribute_code]);
2610
-                            foreach ($possible_values[$attribute_code] as $position => $def) {
2611
-                                foreach ($def as $attribute_value => $attribute_value_output) {
2612
-                                    $real_possible_values[$attribute_value] = $attribute_value_output;
2613
-                                    if (!empty($attribute_value)) {
2614
-                                        global $wpdb;
2615
-                                        $query = $wpdb->prepare("SELECT post_status FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_value LIKE '%%" . serialize($attribute_code) . serialize($attribute_value) . "%%'", $product_id);
2616
-                                        if ('draft' == $wpdb->get_var($query)) {
2617
-                                            unset($real_possible_values[$attribute_value]);
2618
-                                        }
2619
-                                    }
2620
-                                }
2621
-                            }
2622
-                        }
2623
-                        $input_def['possible_value'] = $real_possible_values;
2624
-                        $input_def['valueToPut'] = 'index';
2625
-                        $input_def['value'] = $value;
2626
-
2627
-                        $input_def['options']['more_input'] = '';
2628
-                        if (!empty($possible_values_for_selection_calculation[$attribute_code])) {
2629
-                            foreach ($possible_values_for_selection_calculation[$attribute_code] as $value_id => $value) {
2630
-                                $input_def['options']['more_input'] .= '<input type="hidden" disabled="disabled" value="' . str_replace("\\", "", $value) . '" name="' . $input_def['id'] . '_current_value" id="' . $input_def['id'] . '_current_value_' . $value_id . '" />';
2631
-                            }
2632
-                        }
2633
-
2634
-                        $input_def['options_label']['original'] = true;
2635
-                        $input_def['option'] = 'data-nonce="' . wp_create_nonce('wpshop_ajax_wpshop_variation_selection') . '" class="wpshop_variation_selector_input' . ($is_required ? ' attribute_is_required_input attribute_is_required_input_' . $attribute_code . ' ' : '') . ($attribute_db_definition->_display_informations_about_value == 'yes' ? ' wpshop_display_information_about_value' : '') . ' ' . ((is_admin()) ? $attribute_db_definition->backend_css_class : $attribute_db_definition->frontend_css_class) . '" ';
2636
-
2637
-                        if (!empty($real_possible_values)) {
2638
-                            $tpl_component = array();
2639
-                            $attribute_output_def['value'] = isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code] : $input_def['value'];
2640
-                            $tpl_component['VARIATION_INPUT'] = wpshop_form::check_input_type($input_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) . $input_def['options']['more_input'];
2641
-                            $tpl_component['VARIATION_LABEL'] = ($is_required ? '<span class="attribute_is_required attribute_is_required_' . $attribute_code . '" >' . stripslashes($attribute_db_definition->frontend_label) . '</span> <span class="required" >*</span>' : stripslashes($attribute_db_definition->frontend_label));
2642
-                            $tpl_component['VARIATION_CODE'] = $attribute_code;
2643
-                            $tpl_component['VARIATION_LABEL_HELPER'] = !empty($attribute_db_definition->frontend_help_message) ? ' title="' . $attribute_db_definition->frontend_help_message . '" ' : '';
2644
-                            $tpl_component['VARIATION_LABEL_CLASS'] = !empty($attribute_db_definition->frontend_help_message) ? ' wpshop_att_variation_helper' : '';
2645
-                            $tpl_component['VARIATION_IDENTIFIER'] = $input_def['id'];
2646
-                            $tpl_component['VARIATION_PARENT_ID'] = $product_id;
2647
-                            $tpl_component['VARIATION_PARENT_TYPE'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
2648
-                            $tpl_component['VARIATION_CONTAINER_CLASS'] = ($is_required ? ' attribute_is_required_container attribute_is_required_container_' . $attribute_code : '') . ' wpshop_variation_' . $attribute_code . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_' . $product_id;
2649
-                            $tpl_component['VARIATION_REQUIRED_INDICATION'] = ($is_required) ? __('Required variation', 'wpshop') : '';
2650
-                            $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_code)] = wpshop_display::display_template_element('product_variation_item', $tpl_component);
2651
-                            $variation_attribute_ordered[$output_order[$attribute_code]] = $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_code)];
2652
-                        }
2653
-
2654
-                        $variation_attribute[] = $attribute_code;
2655
-                    }
2656
-                }
2657
-            }
2658
-
2659
-        }
2660
-        $variation_tpl['VARIATION_FORM_ELEMENT_ID'] = $product_id;
2661
-        wp_reset_query();
2662
-
2663
-        $attribute_defined_to_be_user_defined = wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode));
2664
-        if (!empty($attribute_defined_to_be_user_defined)) {
2665
-            foreach ($attribute_defined_to_be_user_defined as $attribute_not_in_variation_but_user_defined) {
2666
-                $is_required = ((!empty($head_wpshop_variation_definition['options']) && !empty($head_wpshop_variation_definition['options']['required_attributes']) && (in_array($attribute_not_in_variation_but_user_defined->code, $head_wpshop_variation_definition['options']['required_attributes']))) || $attribute_not_in_variation_but_user_defined->is_required == 'yes') ? true : false;
2667
-
2668
-                $attribute_display_state = wpshop_attributes::check_attribute_display($attribute_not_in_variation_but_user_defined->is_visible_in_front, $wpshop_product_attributes_frontend_display, 'attribute', $attribute_not_in_variation_but_user_defined->code, 'complete_sheet');
2669
-                if ($attribute_display_state && array_key_exists($attribute_not_in_variation_but_user_defined->code, $output_order) && !in_array($attribute_not_in_variation_but_user_defined->code, $variation_attribute) && ($attribute_not_in_variation_but_user_defined->is_used_for_variation == 'no')) {
2670
-                    $attribute_output_def = wpshop_attributes::get_attribute_field_definition($attribute_not_in_variation_but_user_defined, (is_array($head_wpshop_variation_definition) && isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_not_in_variation_but_user_defined->code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_not_in_variation_but_user_defined->code] : ''));
2671
-
2672
-                    $tpl_component = array();
2673
-                    $attribute_output_def['option'] = 'data-nonce="' . wp_create_nonce('wpshop_ajax_wpshop_variation_selection') . '" class="wpshop_variation_selector_input' . ($is_required ? ' attribute_is_required_input attribute_is_required_input_' . $attribute_not_in_variation_but_user_defined->code : '') . ' ' . (str_replace('"', '', str_replace('class="', '', $attribute_output_def['option']))) . ' ' . ((is_admin()) ? $attribute_not_in_variation_but_user_defined->backend_css_class : $attribute_not_in_variation_but_user_defined->frontend_css_class) . '" ';
2674
-                    $tpl_component['VARIATION_INPUT'] = wpshop_form::check_input_type($attribute_output_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '[free]') . $attribute_output_def['options'];
2675
-                    $tpl_component['VARIATION_LABEL'] = ($is_required ? '<span class="attribute_is_required attribute_is_required_' . $attribute_not_in_variation_but_user_defined->code . '" >' . stripslashes($attribute_not_in_variation_but_user_defined->frontend_label) . '</span> <span class="required" >*</span>' : stripslashes($attribute_not_in_variation_but_user_defined->frontend_label));
2676
-                    $tpl_component['VARIATION_CODE'] = $attribute_not_in_variation_but_user_defined->code;
2677
-                    $tpl_component['VARIATION_LABEL_HELPER'] = !empty($attribute_not_in_variation_but_user_defined->frontend_help_message) ? ' title="' . $attribute_not_in_variation_but_user_defined->frontend_help_message . '" ' : '';
2678
-                    $tpl_component['VARIATION_LABEL_CLASS'] = !empty($attribute_not_in_variation_but_user_defined->frontend_help_message) ? ' wpshop_att_variation_helper' : '';
2679
-                    $tpl_component['VARIATION_REQUIRED_INDICATION'] = ($is_required) ? __('Required variation', 'wpshop') : '';
2680
-                    $tpl_component['VARIATION_IDENTIFIER'] = $attribute_output_def['id'];
2681
-                    $tpl_component['VARIATION_PARENT_ID'] = $product_id;
2682
-                    $tpl_component['VARIATION_PARENT_TYPE'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
2683
-                    $tpl_component['VARIATION_CONTAINER_CLASS'] = ($is_required ? ' attribute_is_required_container attribute_is_required_container_' . $attribute_not_in_variation_but_user_defined->code : '') . ' wpshop_variation_' . $attribute_not_in_variation_but_user_defined->code . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_' . $product_id;
2684
-                    $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_not_in_variation_but_user_defined->code)] = ($attribute_output_def['type'] != 'hidden') ? wpshop_display::display_template_element('product_variation_item', $tpl_component) : wpshop_form::check_input_type($attribute_output_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '[free]') . $attribute_output_def['options'];
2685
-                    $variation_attribute_ordered[$output_order[$attribute_not_in_variation_but_user_defined->code]] = $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_not_in_variation_but_user_defined->code)];
2686
-                }
2687
-            }
2688
-        }
2689
-        $variation_tpl['VARIATION_FORM_VARIATION_LIST'] = '';
2690
-        if (!empty($variation_attribute_ordered) && is_array($variation_attribute_ordered)) {
2691
-            ksort($variation_attribute_ordered);
2692
-            foreach ($variation_attribute_ordered as $attribute_variation_to_output) {
2693
-                $variation_tpl['VARIATION_FORM_VARIATION_LIST'] .= $attribute_variation_to_output;
2694
-            }
2695
-        }
2696
-        $variation_tpl['FROM_ADMIN_INDICATOR'] = $variation_tpl['ORDER_ID_INDICATOR'] = '';
2697
-        $variation_tpl['PRODUCT_ADDED_TO_CART_QTY'] = (!empty($qty)) ? $qty : 1;
2698
-        if ($from_admin && !empty($order_id)) {
2699
-            $variation_tpl['FROM_ADMIN_INDICATOR'] = '<input type="hidden" name="wps_orders_from_admin" value="1" />';
2700
-            $variation_tpl['ORDER_ID_INDICATOR'] = '<input type="hidden" name="wps_orders_order_id" value="' . $order_id . '" />';
2701
-        }
2702
-        $output = !empty($variation_tpl['VARIATION_FORM_VARIATION_LIST']) ? wpshop_display::display_template_element('product_variation_form', $variation_tpl) : '';
2703
-
2704
-        return $output;
2705
-    }
2706
-
2707
-    public static function get_parent_variation($variation_id)
2708
-    {
2709
-        $result = array();
2710
-        if (!empty($variation_id)) {
2711
-            $variation_post = get_post($variation_id);
2712
-            if (!empty($variation_post) && !empty($variation_post->post_parent)) {
2713
-                $result['parent_post'] = get_post($variation_post->post_parent);
2714
-                $result['parent_post_meta'] = get_post_meta($variation_post->post_parent, '_wpshop_product_metadata', true);
2715
-            }
2716
-        }
2717
-        return $result;
2718
-    }
2719
-
2720
-    /**
2721
-     * Display the current configuration for a given product
2722
-     * @param array $shortcode_attribute Some parameters given by the shortcode for display
2723
-     */
2724
-    public function wpshop_product_variations_summary($shortcode_attribute)
2725
-    {
2726
-        $output = '';
2727
-
2728
-        $product_variations_selection_args = array(
2729
-            'CURRENCY_SELECTOR' => wpshop_attributes_unit::wpshop_shop_currency_list_field(),
2730
-            'PRODUCT_VARIATION_SELECTION_DISPLAY' => '',
2731
-        );
2732
-
2733
-        $current_user_id = get_current_user_id();
2734
-
2735
-        if (!empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items'])) {
2736
-            foreach ($_SESSION['cart']['order_items'] as $item_id => $item) {
2737
-                if (!empty($item) && !empty($item['item_id'])) {
2738
-
2739
-                    $free_variations = array();
2740
-                    if (!empty($item['item_meta']) && !empty($item['item_meta']['free_variation'])) {
2741
-                        foreach ($item['item_meta']['free_variation'] as $attribute_code => $attribute_value) {
2742
-                            $free_variations[] = $attribute_code . '-_variation_val_-' . $attribute_value;
2743
-                        }
2744
-                    }
2745
-
2746
-                    $variations = array();
2747
-                    if (!empty($item['item_meta']) && !empty($item['item_meta']['variations'])) {
2748
-                        foreach ($item['item_meta']['variations'] as $variation_id => $variation_def) {
2749
-
2750
-                            if (!empty($variation_def['item_meta']) && !empty($variation_def['item_meta']['variation_definition'])) {
2751
-                                foreach ($variation_def['item_meta']['variation_definition'] as $attribute_code => $attribute_selected_data) {
2752
-                                    $variations[] = $attribute_code . '-_variation_val_-' . $attribute_selected_data['ID'];
2753
-                                }
2754
-                            }
2755
-                        }
2756
-                    }
2757
-                    $product_variation_summary = self::wpshop_ajax_wpshop_variation_selection($item['item_id'], $variations, $free_variations, null, $item['item_qty']);
2758
-
2759
-                    $product_variations_selection_args['PRODUCT_VARIATION_SELECTION_DISPLAY'] .= $product_variation_summary[1]['product_output'];
2760
-                }
2761
-            }
2762
-        }
2763
-
2764
-        $output .= wpshop_display::display_template_element('wpshop_product_configuration_summary', $product_variations_selection_args);
2765
-
2766
-        echo $output;
2767
-    }
2768
-
2769
-    /**
2770
-     * Affichage du résumé du produit sélectionné avec le prix par option / Display a summary of selected product with the price per option
2771
-     *
2772
-     * @param integer $product_id L'identifiant du produit qui est ajouté au panier / The product identifier added to cart
2773
-     * @param array $wpshop_variation_selected La liste des options ayant un prix sélectionnées par le client / Options list with price selected by the customer
2774
-     * @param array $wpshop_free_variation La liste des options n'entrainant pas de modification du prix final sélectionnées par le client / Options list without price selected by the customer
2775
-     * @param string $wpshop_current_for_display Inconnu / Unknown
2776
-     * @param integer $product_qty La quantité commandée par le client / Quantity ordered by the customer
2777
-     *
2778
-     * @return array Le résultat du calcul pour l'affichage / Output result
2779
-     */
2780
-    public static function wpshop_ajax_wpshop_variation_selection($product_id, $wpshop_variation_selected, $wpshop_free_variation, $wpshop_current_for_display, $product_qty = 1)
2781
-    {
2782
-        global $wpdb;
2783
-
2784
-        $wpshop_cart = new wps_cart();
2785
-        $wpshop_products = new wpshop_products();
2786
-
2787
-        $response = '';
2788
-        $response_status = $has_variation = false;
2789
-        $tpl_component = array();
2790
-
2791
-        // Check if variations exists
2792
-        if (!empty($wpshop_variation_selected) || !empty($wpshop_free_variation)) {
2793
-
2794
-            //Recover all selected variations
2795
-            $variations_selected = array();
2796
-            if (!empty($wpshop_variation_selected)) {
2797
-                foreach ($wpshop_variation_selected as $selected_variation) {
2798
-                    $variation_definition = explode('-_variation_val_-', $selected_variation);
2799
-                    $variations_selected[$variation_definition[0]] = $variation_definition[1];
2800
-                }
2801
-            }
2802
-
2803
-            // Check variations priority
2804
-            $product_with_variation = wpshop_products::get_variation_by_priority($variations_selected, $product_id);
2805
-
2806
-            // Check if $product_with_variation have variations
2807
-            if (!empty($product_with_variation[$product_id]['variations']) || !empty($wpshop_free_variation)) {
2808
-
2809
-                $formatted_product = $wpshop_cart->prepare_product_to_add_to_cart($product_id, $product_qty, $variations_selected);
2810
-                $product_to_add_to_cart = $formatted_product[0];
2811
-                foreach ($formatted_product[0] as $pid => $product_more_content) {
2812
-                    $order_items[$pid]['product_id'] = $product_more_content['id'];
2813
-
2814
-                    /** For product with variation    */
2815
-                    $order_items[$pid]['product_variation_type'] = !empty($product_more_content['variation_priority']) ? $product_more_content['variation_priority'] : '';
2816
-                    $order_items[$pid]['free_variation'] = !empty($product_more_content['free_variation']) ? $product_more_content['free_variation'] : '';
2817
-                    $order_items[$pid]['product_variation'] = '';
2818
-                    if (!empty($product_more_content['variations'])) {
2819
-                        foreach ($product_more_content['variations'] as $variation_id) {
2820
-                            $order_items[$pid]['product_variation'][] = $variation_id;
2821
-                        }
2822
-                    }
2823
-                }
2824
-
2825
-                // If Product list is not empty, add products to order
2826
-                if (!empty($order_items)) {
2827
-                    foreach ($order_items as $product_id => $d) {
2828
-                        $product_key = $product_id;
2829
-
2830
-                        // Formate datas
2831
-                        $product_id = $head_product_id = $d['product_id'];
2832
-                        $product_variation = !empty($d['product_variation']) ? $d['product_variation'] : null;
2833
-
2834
-                        // If product is a single variation product
2835
-                        if (!empty($product_variation) && (count($product_variation) == 1)) {
2836
-                            $product_id = $product_variation[0];
2837
-                        }
2838
-
2839
-                        // Construct final product
2840
-                        $product = wpshop_products::get_product_data($d['product_id'], true);
2841
-                        $the_product = array_merge(array('product_id' => $d['product_id'], 'product_qty' => 1), $product);
2842
-
2843
-                        //    Add variation to product into cart for storage
2844
-                        if (!empty($product_variation)) {
2845
-                            $the_product = wpshop_products::get_variation_price_behaviour($the_product, $product_variation, $head_product_id, array('type' => $d['product_variation_type']));
2846
-                        }
2847
-
2848
-                        // Free Variations Checking
2849
-                        if (!empty($d['free_variation'])) {
2850
-                            $the_product['item_meta']['free_variation'] = $d['free_variation'];
2851
-                            $head_product_id = $the_product['product_id'];
2852
-                        }
2853
-
2854
-                        // If product is a variation, we check parent product general
2855
-                        if (get_post_type($the_product['product_id']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
2856
-                            $parent_def = wpshop_products::get_parent_variation($the_product['product_id']);
2857
-                            if (!empty($parent_def) && !empty($parent_def['parent_post'])) {
2858
-                                $variation_def = get_post_meta($parent_def['parent_post']->ID, '_wpshop_variation_defining', true);
2859
-                                $parent_meta = $parent_def['parent_post_meta'];
2860
-                                if (!empty($variation_def) && !empty($variation_def['options']) && !empty($variation_def['options']['priority']) && in_array('combined', $variation_def['options']['priority']) && !empty($variation_def['options']['price_behaviour']) && in_array('addition', $variation_def['options']['price_behaviour']) && !empty($variation_def['attributes']) && count($variation_def['attributes']) > 1) {
2861
-                                    $the_product['product_price'] += number_format(str_replace(',', '.', $parent_meta['product_price']), 2, '.', '');
2862
-                                    $the_product['price_ht'] += number_format(str_replace(',', '.', $parent_meta['price_ht']), 2, '.', '');
2863
-                                    $the_product['tva'] += number_format(str_replace(',', '.', $parent_meta['tva']), 2, '.', '');
2864
-                                }
2865
-                            }
2866
-                        }
2867
-                    }
2868
-                }
2869
-
2870
-                if (!empty($the_product) && empty($the_product['price_ttc_before_discount']) && empty($the_product['price_ht_before_discount'])) {
2871
-                    $price_infos = wpshop_prices::check_product_price($the_product, true);
2872
-                    if (!empty($price_infos['discount']['discount_exist'])) {
2873
-                        $the_product['price_ttc_before_discount'] = $the_product['product_price'];
2874
-                        $the_product['price_ht_before_discount'] = $the_product['price_ht'];
2875
-                    }
2876
-                    $the_product['price_ht'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_et_price'] : $price_infos['et'];
2877
-                    $the_product['product_price'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_ati_price'] : $price_infos['ati'];
2878
-                    $the_product['tva'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_tva'] : $price_infos['tva'];
2879
-                }
2880
-
2881
-                $product = wpshop_products::get_product_data($product_id, true, '"publish", "draft"');
2882
-                // Add free variations to product
2883
-                if (!empty($wpshop_free_variation)) {
2884
-                    $the_product['item_meta']['free_variation'] = $wpshop_free_variation;
2885
-                }
2886
-                // Change picture if have a selected variation
2887
-                $the_selected_variation = !empty($product_with_variation) && !empty($product_with_variation[$head_product_id]) && !empty($product_with_variation[$head_product_id]['variations']) ? $product_with_variation[$head_product_id]['variations'] : null;
2888
-                $response['wps_product_image'] = $wpshop_products->wps_selected_variation_picture($head_product_id, $the_selected_variation);
2889
-
2890
-                // Price Display
2891
-                $price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
2892
-                $price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front, $product['custom_display'], 'attribute', 'product_price', 'complete_sheet');
2893
-                $productPrice = '';
2894
-                if ($price_display) {
2895
-                    $response['product_price_output'] = wpshop_prices::get_product_price($the_product, 'price_display', 'complete_sheet', false, true);
2896
-                }
2897
-
2898
-                //Get Summary cart
2899
-                $response['product_output'] = $wpshop_products->wps_get_summary_variations_product($product_id, $the_product, (!empty($wpshop_variation_selected) || !empty($wpshop_free_variation) ? true : false));
2900
-                $response_status = true;
2901
-            } else {
2902
-                //Product without variations
2903
-                $product_data = wpshop_products::get_product_data($product_id);
2904
-                $response['product_price_output'] = wpshop_prices::get_product_price($product_data, 'price_display', 'complete_sheet');
2905
-            }
2906
-        }
2907
-
2908
-        return array($response_status, $response);
2909
-    }
2910
-
2911
-    /**
2912
-     * Display information for a given value of an attribute defined as an entity, when attribute option for detail view is set as true
2913
-     *
2914
-     * @param array $shortcode_attribute Some parameters given by the shortcode for display
2915
-     */
2916
-    public function wpshop_product_variation_value_detail($shortcode_attribute)
2917
-    {
2918
-        echo wpshop_display::display_template_element('wpshop_product_variation_value_detail_container', array());
2919
-    }
2920
-
2921
-    /**
2922
-     * Build the product structure with variation for product choosed by the user into frontend sheet
2923
-     *
2924
-     * @param array $selected_variation THe list of variation choosed by the user in product frontend sheet
2925
-     * @param integer $product_id The basic product choose by the user in frontend
2926
-     *
2927
-     * @return array The product list for adding to the cart build by variation priority
2928
-     */
2929
-    public static function get_variation_by_priority($selected_variation, $product_id, $add_to_cart_action = false)
2930
-    {
2931
-        global $wpdb;
2932
-        $all_required_variations_selected = $no_selected_variation = true;
2933
-        $single_variations = $combined_variations = $product_to_add_to_cart = array();
2934
-
2935
-        // Check if all required variations are selected
2936
-        $required_attributes_list = wpshop_prices::check_required_attributes($product_id);
2937
-        foreach ($selected_variation as $k => $value) {
2938
-            if ($value == 0 && in_array($k, $required_attributes_list)) {
2939
-                $all_required_variations_selected = false;
2940
-            }
2941
-            if ($value != 0) {
2942
-                $no_selected_variation = false;
2943
-            }
2944
-        }
2945
-        if (!empty($selected_variation)) {
2946
-            //Check variations configuration
2947
-            $product_variation_configuration = get_post_meta($product_id, '_wpshop_variation_defining', true);
2948
-            // Check variations type
2949
-            $product_variation_type = (!empty($product_variation_configuration) && !empty($product_variation_configuration['variation_type'])) ? $product_variation_configuration['variation_type'] : 'single';
2950
-            $product_to_add_to_cart[$product_id]['variation_priority'] = $product_variation_type;
2951
-
2952
-            //Check defined variation priority
2953
-            $priority = (!empty($product_variation_configuration['options']) && !empty($product_variation_configuration['options']['priority'][0])) ? $product_variation_configuration['options']['priority'][0] : 'combined';
2954
-            $product_to_add_to_cart[$product_id]['defined_variation_priority'] = $priority;
2955
-
2956
-            // Recover all product variations
2957
-            $query_variation = $selected_variation;
2958
-            // Delete free variations
2959
-            unset($query_variation['free']);
2960
-
2961
-            if ($product_variation_type == 'single') {
2962
-                // Get single variations
2963
-                $single_variations = array();
2964
-                foreach ($selected_variation as $attribute_code => $attribute_value) {
2965
-                    if (isset($attribute_value) && $attribute_code != 'free') {
2966
-                        $query = $wpdb->prepare("SELECT ID FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_value = '" . serialize(array($attribute_code => $attribute_value)) . "'", $product_id);
2967
-                        $single_variation_id = $wpdb->get_var($query);
2968
-                        if (!empty($single_variation_id)) {
2969
-                            $single_variations[] = $single_variation_id;
2970
-                            unset($query_variation[$attribute_code]);
2971
-                        }
2972
-                    }
2973
-                }
2974
-            } else {
2975
-                // Get combined variations
2976
-                $query = $wpdb->prepare("SELECT ID FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_key = '_wpshop_variations_attribute_def' AND P_META.meta_value = '" . serialize($query_variation) . "'", $product_id);
2977
-                $combined_variation_id = $wpdb->get_var($query);
2978
-                if (!empty($combined_variation_id)) {
2979
-                    $combined_variations[] = $combined_variation_id;
2980
-                }
2981
-
2982
-            }
2983
-
2984
-            //If all required variations are not selected
2985
-            if (!$all_required_variations_selected || $no_selected_variation) {
2986
-                $product_to_add_to_cart['text_from'] = 'on';
2987
-                // If we choose to display lower price variation combinaison
2988
-                if (empty($product_variation_configuration) || (!empty($product_variation_configuration) && empty($product_variation_configuration['options'])) || (!empty($product_variation_configuration) && !empty($product_variation_configuration['options']) && !empty($product_variation_configuration['options']['price_display']) && !empty($product_variation_configuration['options']['price_display']['lower_price']))) {
2989
-                    //Get lower price variation combinaison
2990
-                    $lower_price_variations = wpshop_prices::check_product_lower_price($product_id);
2991
-                    if (!empty($lower_price_variations['variations']) && is_array($lower_price_variations['variations'])) {
2992
-                        foreach ($lower_price_variations['variations'] as $lower_price_variation) {
2993
-                            $product_to_add_to_cart[$product_id]['variations'][] = $lower_price_variation;
2994
-                        }
2995
-                    }
2996
-                    $product_to_add_to_cart['display_lower_price'] = true;
2997
-                } else {
2998
-                    $product_to_add_to_cart[$product_id]['variations'] = array();
2999
-                }
3000
-            } else {
3001
-                $product_to_add_to_cart['text_from'] = '';
3002
-                $product_to_add_to_cart[$product_id]['variations'] = (!empty($product_variation_type) && $product_variation_type == 'single') ? $single_variations : $combined_variations;
3003
-            }
3004
-        }
3005
-
3006
-        return $product_to_add_to_cart;
3007
-    }
3008
-
3009
-    public static function get_variation_price_behaviour($product_into_cart, $product_variations, $head_product_id, $variations_options)
3010
-    {
3011
-        global $wpdb;
3012
-
3013
-        if (!empty($product_variations)) {
3014
-            // Initialize variations total price datas
3015
-            $variations_total_price = array('price_et' => 0, 'vat_amount' => 0, 'price_ati' => 0);
3016
-            $variations_discount_total_price = array('price_et' => 0, 'vat_amount' => 0, 'price_ati' => 0);
3017
-            $vat_rate = 0;
3018
-            $discount_amount = $discount_rate = $special_price = 0;
3019
-            // Recover Head product metadata
3020
-            $head_product_metadata = get_post_meta($head_product_id, '_wpshop_product_metadata', true);
3021
-            $tva_rate_id = (!empty($head_product_metadata['tx_tva'])) ? $head_product_metadata['tx_tva'] : 0;
3022
-            if (!empty($tva_rate_id)) {
3023
-                // Recover VAT Rate of product
3024
-                $query = $wpdb->prepare('SELECT value FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $tva_rate_id);
3025
-                $vat_rate = $wpdb->get_var($query);
3026
-            }
3027
-            $price_piloting = get_option('wpshop_shop_price_piloting');
3028
-            foreach ($product_variations as $product_variation) {
3029
-                $variation_metadata['product_price'] = 0;
3030
-                $variation_metadata = get_post_meta($product_variation, '_wpshop_product_metadata', true);
3031
-                if (!empty($variation_metadata)) {
3032
-                    $p_et = ((empty($price_piloting) || $price_piloting == 'TTC') ? ((!empty($variation_metadata['product_price']) ? $variation_metadata['product_price'] : 0) / (1 + ($vat_rate / 100))) : $variation_metadata['price_ht']);
3033
-                    $p_ati = ((empty($price_piloting) || $price_piloting == 'TTC') ? (!empty($variation_metadata['product_price']) ? $variation_metadata['product_price'] : 0) : ($variation_metadata['price_ht'] * (1 + ($vat_rate / 100))));
3034
-
3035
-                    $variations_total_price['price_et'] += $p_et;
3036
-                    $variations_total_price['price_ati'] += $p_ati;
3037
-                    $variations_total_price['vat_amount'] += $p_ati - $p_et;
3038
-
3039
-                    $variation_metadata['tx_tva'] = $product_into_cart['tx_tva'] = $vat_rate;
3040
-
3041
-                    // Check discount
3042
-                    $discount_config = wpshop_prices::check_discount_for_product($product_variation);
3043
-                    if (!empty($discount_config) && !empty($discount_config['value'])) {
3044
-                        $variation_discount_prices = wpshop_prices::calcul_discounted_price($variation_metadata, $discount_config);
3045
-                        $variations_discount_total_price['price_et'] += $variation_discount_prices['price_ht'];
3046
-                        $variations_discount_total_price['price_ati'] += $variation_discount_prices['product_price'];
3047
-                        $variations_discount_total_price['vat_amount'] += $variation_discount_prices['tva'];
3048
-
3049
-                        if (!empty($discount_config['type']) && $discount_config['type'] == 'discount_amount') {
3050
-                            $product_into_cart['discount_amount'] = $discount_amount + $product_into_cart['discount_amount'];
3051
-                        } elseif (!empty($discount_config['type']) && $discount_config['type'] == 'discount_rate') {
3052
-                            $product_into_cart['discount_rate'] = $discount_rate + $discount_config['value'];
3053
-                        } elseif (!empty($discount_config['type']) && $discount_config['type'] == 'special_price') {
3054
-                            $product_into_cart['special_price'] = $special_price + $discount_config['value'];
3055
-                        }
3056
-                    }
3057
-
3058
-                    $product_variation_def = wpshop_products::get_product_data($product_variation, true, '"publish", "draft"');
3059
-                    $product_into_cart['item_meta']['variations'][$product_variation] = $product_variation_def;
3060
-                }
3061
-            }
3062
-
3063
-            // Check if add or replace variation price to head product
3064
-            $product_variation_defining = get_post_meta($head_product_id, '_wpshop_variation_defining', true);
3065
-            if ((!empty($product_variation_def['price_behaviour'])) || empty($product_variation_defining) || (!empty($product_variation_defining) && empty($product_variation_defining['options'])) || ((!empty($product_variation_defining)) && !empty($product_variation_defining['options']) && !empty($product_variation_defining['options']['price_behaviour']) && !empty($product_variation_defining['options']['price_behaviour'][0]) && $product_variation_defining['options']['price_behaviour'][0] == 'replacement')) {
3066
-                //Replace the product price
3067
-                if (!empty($variations_discount_total_price) && !empty($variations_discount_total_price['price_ati'])) {
3068
-                    $product_into_cart['price_ttc_before_discount'] = $variations_total_price['price_ati'];
3069
-                    $product_into_cart['price_ht_before_discount'] = $variations_total_price['price_et'];
3070
-
3071
-                    $product_into_cart['product_price'] = $variations_discount_total_price['price_ati'];
3072
-                    $product_into_cart['price_ht'] = $variations_discount_total_price['price_et'];
3073
-                    $product_into_cart['tva'] = $variations_discount_total_price['vat_amount'];
3074
-                } else {
3075
-                    $product_into_cart['product_price'] = $variations_total_price['price_ati'];
3076
-                    $product_into_cart['price_ht'] = $variations_total_price['price_et'];
3077
-                    $product_into_cart['tva'] = $variations_total_price['vat_amount'];
3078
-                }
3079
-            } else {
3080
-                // Add variations price to product price
3081
-                if (!empty($variations_discount_total_price) && !empty($variations_discount_total_price['price_ati'])) {
3082
-                    $product_into_cart['price_ttc_before_discount'] = ($product_into_cart['product_price'] + $variations_total_price['price_ati']);
3083
-                    $product_into_cart['price_ht_before_discount'] = ($product_into_cart['price_ht'] + $variations_total_price['price_et']);
3084
-
3085
-                    $product_into_cart['product_price'] += $variations_discount_total_price['price_ati'];
3086
-                    $product_into_cart['price_ht'] += $variations_discount_total_price['price_et'];
3087
-                    $product_into_cart['tva'] += $variations_discount_total_price['vat_amount'];
3088
-                } else {
3089
-                    $product_into_cart['product_price'] += $variations_total_price['price_ati'];
3090
-                    $product_into_cart['price_ht'] += $variations_total_price['price_et'];
3091
-                    $product_into_cart['tva'] += $variations_total_price['vat_amount'];
3092
-                }
3093
-                // Check parent discount
3094
-                $parent_discount_config = wpshop_prices::check_discount_for_product($head_product_id);
3095
-                if (!empty($parent_discount_config)) {
3096
-                    $product_into_cart['price_ttc_before_discount'] = (!empty($product_into_cart['price_ttc_before_discount'])) ? $product_into_cart['price_ttc_before_discount'] : $product_into_cart['product_price'];
3097
-                    $product_into_cart['price_ht_before_discount'] = (!empty($product_into_cart['price_ht_before_discount'])) ? $product_into_cart['price_ht_before_discount'] : $product_into_cart['price_ht'];
3098
-                    $product_into_cart = wpshop_prices::calcul_discounted_price($product_into_cart, $parent_discount_config);
3099
-
3100
-                    if (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'discount_amount') {
3101
-                        $product_into_cart['discount_amount'] = $discount_amount + $parent_discount_config['value'];
3102
-                    } elseif (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'discount_rate') {
3103
-                        $product_into_cart['discount_rate'] = $discount_rate + $parent_discount_config['value'];
3104
-                    } elseif (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'special_price') {
3105
-                        $product_into_cart['special_price'] = $special_price + $parent_discount_config['value'];
3106
-                    }
3107
-                }
3108
-            }
3109
-        } else {
3110
-            // If product have just Free variations
3111
-            $discount_config = wpshop_prices::check_discount_for_product($head_product_id);
3112
-            if (!empty($discount_config)) {
3113
-                $product_into_cart['price_ttc_before_discount'] = (!empty($product_into_cart['price_ttc_before_discount'])) ? $product_into_cart['price_ttc_before_discount'] : $product_into_cart['product_price'];
3114
-                $product_into_cart['price_ht_before_discount'] = (!empty($product_into_cart['price_ht_before_discount'])) ? $product_into_cart['price_ht_before_discount'] : $product_into_cart['price_ht'];
3115
-                $product_into_cart = wpshop_prices::calcul_discounted_price($product_into_cart, $discount_config);
3116
-            }
3117
-        }
3118
-
3119
-        // Text From indicator
3120
-        if (!empty($variations_options) && !empty($variations_options['text_from'])) {
3121
-            $product_into_cart['text_from'] = $variations_options['text_from'];
3122
-        }
3123
-        return $product_into_cart;
3124
-    }
3125
-
3126
-    /**
3127
-     * Read an array with product options chosen by the customer, order into an array regarding admin definition
3128
-     *
3129
-     * @param array $product_definition_value The array with the selected product option to ordered
3130
-     * @param array $output_order The good order for attribute defined by administrator
3131
-     * @param dtring $from_page A string allowing to take a specific template regarding the current page
3132
-     *
3133
-     * @return array The array containing all product options ordered as the admin configure it
3134
-     */
3135
-    public static function get_selected_variation_display($product_definition_value, $output_order, $from_page = null, $template_part = 'wpshop', $output_type = null)
3136
-    {
3137
-        $variation_attribute_ordered = array();
3138
-        $variation_attribute_ordered['prices'] = array();
3139
-        $variation_attribute_ordered['attribute_list'] = array();
3140
-
3141
-        if (!empty($product_definition_value['variation_definition']) && is_array($product_definition_value['variation_definition'])) {
3142
-            foreach ($product_definition_value['variation_definition'] as $variation_attribute_code => $variation_attribute_detail) {
3143
-                $variation_tpl_component = array();
3144
-                foreach ($variation_attribute_detail as $info_name => $info_value) {
3145
-                    $variation_tpl_component['VARIATION_' . strtoupper($info_name)] = in_array($info_name, unserialize(WPSHOP_ATTRIBUTE_PRICES)) ? wpshop_display::format_field_output('wpshop_product_price', $info_value) : stripslashes($info_value);
3146
-                }
3147
-                $variation_tpl_component['VARIATION_ID'] = $variation_attribute_code;
3148
-                $variation_tpl_component['VARIATION_ATT_CODE'] = $variation_attribute_code;
3149
-                if (!empty($output_order[$variation_attribute_code])) {
3150
-                    $display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3151
-                    if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3152
-                        $variation_attribute_ordered['attribute_list'][$output_order[$variation_attribute_code]] = $display_data;
3153
-                    }
3154
-                } else {
3155
-                    $display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3156
-                    if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3157
-                        $variation_attribute_ordered['attribute_list'][] = $display_data;
3158
-                    }
3159
-                }
3160
-                unset($variation_tpl_component);
3161
-            }
3162
-        }
3163
-
3164
-        if (!empty($product_definition_value['variations']) && is_array($product_definition_value['variations'])) {
3165
-            foreach ($product_definition_value['variations'] as $variation_id => $variation_details) {
3166
-                $variation_tpl_component = array();
3167
-                foreach ($variation_details as $info_name => $info_value) {
3168
-                    if ($info_name != 'item_meta') {
3169
-                        $variation_tpl_component['VARIATION_DETAIL_' . strtoupper($info_name)] = in_array($info_name, unserialize(WPSHOP_ATTRIBUTE_PRICES)) ? wpshop_display::format_field_output('wpshop_product_price', $info_value) : stripslashes($info_value);
3170
-                    }
3171
-                }
3172
-                foreach ($variation_details['item_meta']['variation_definition'] as $variation_attribute_code => $variation_attribute_def) {
3173
-                    $variation_tpl_component['VARIATION_NAME'] = stripslashes($variation_attribute_def['NAME']);
3174
-                    $variation_tpl_component['VARIATION_VALUE'] = stripslashes($variation_attribute_def['VALUE']);
3175
-                    $variation_tpl_component['VARIATION_ID'] = $variation_id;
3176
-                    $variation_tpl_component['VARIATION_ATT_CODE'] = $variation_attribute_code;
3177
-
3178
-                    $variation_attribute_ordered['prices'][$variation_attribute_code] = $variation_tpl_component['VARIATION_DETAIL_PRODUCT_PRICE'];
3179
-                }
3180
-                if (!empty($output_order[$variation_attribute_code])) {
3181
-                    $variation_attribute_ordered['attribute_list'][$output_order[$variation_attribute_code]] = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3182
-                }
3183
-                unset($variation_tpl_component);
3184
-            }
3185
-        }
3186
-
3187
-        /**    Free Variation part    */
3188
-        if (!empty($product_definition_value['free_variation']) && is_array($product_definition_value['free_variation'])) {
3189
-            foreach ($product_definition_value['free_variation'] as $build_variation_key => $build_variation) {
3190
-                if (strpos($build_variation, '-_variation_val_-')) {
3191
-                    $variation_definition = explode('-_variation_val_-', $build_variation);
3192
-                    $attribute_code = $variation_definition[0];
3193
-                    $attribute_selected_value = $variation_definition[1];
3194
-                } else {
3195
-                    $attribute_code = $build_variation_key;
3196
-                    $attribute_selected_value = $build_variation;
3197
-                }
3198
-
3199
-                $free_variation_attribute_def = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
3200
-                $variation_tpl_component['VARIATION_NAME'] = stripslashes($free_variation_attribute_def->frontend_label);
3201
-                $value_to_outut = $attribute_selected_value;
3202
-                switch ($free_variation_attribute_def->data_type) {
3203
-                    case 'datetime':
3204
-                        $value_to_outut = mysql2date(get_option('date_format'), $attribute_selected_value, true);
3205
-                        break;
3206
-                }
3207
-
3208
-                if (in_array($free_variation_attribute_def->backend_input, array('select', 'multiple-select'))) {
3209
-                    switch ($free_variation_attribute_def->data_type_to_use) {
3210
-                        case 'custom':
3211
-                        case 'internal':
3212
-                            $possible_values = wpshop_attributes::get_select_output($free_variation_attribute_def);
3213
-                            $value_to_outut = $possible_values['possible_value'][$attribute_selected_value];
3214
-                            break;
3215
-                    }
3216
-                }
3217
-                $variation_tpl_component['VARIATION_VALUE'] = stripslashes($value_to_outut);
3218
-                $variation_tpl_component['VARIATION_ID'] = $attribute_code;
3219
-                $variation_tpl_component['VARIATION_ATT_CODE'] = $attribute_code;
3220
-                if (!empty($value_to_outut) && !empty($output_order[$free_variation_attribute_def->code])) {
3221
-                    $display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $attribute_code), $template_part);
3222
-                    if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3223
-                        $variation_attribute_ordered['attribute_list'][$output_order[$free_variation_attribute_def->code]] = $display_data;
3224
-                    }
3225
-                }
3226
-                unset($variation_tpl_component);
3227
-            }
3228
-        }
3229
-        return $variation_attribute_ordered;
3230
-    }
3231
-
3232
-    public function wps_selected_variation_picture($head_product_id, $variations)
3233
-    {
3234
-        $response = array();
3235
-        /** Selected Product image **/
3236
-        $post_thumbnail_id = get_post_thumbnail_id($head_product_id);
3237
-        if (!empty($post_thumbnail_id)) {
3238
-            $response['img_id'] = $post_thumbnail_id;
3239
-            $response['img'] = wp_get_attachment_image($post_thumbnail_id, 'wpshop-product-galery');
3240
-            $response['img_url'] = wp_get_attachment_url($post_thumbnail_id);
3241
-        }
3242
-
3243
-        /** check if have one variation **/
3244
-        if (!empty($variations) && count($variations) == 1) {
3245
-            $variation_attached_image_id = get_post_meta($variations[0], '_wps_variation_attached_picture', true);
3246
-            if (!empty($variation_attached_image_id)) {
3247
-                $response['img_id'] = $variation_attached_image_id;
3248
-                $response['img'] = wp_get_attachment_image($variation_attached_image_id, 'wpshop-product-galery');
3249
-                $response['img_url'] = wp_get_attachment_url($variation_attached_image_id);
3250
-            }
3251
-        }
3252
-        return $response;
3253
-    }
3254
-
3255
-    /** Add a meta box of product sales history in product administration panel **/
3256
-    public function meta_box_product_sale_informations()
3257
-    {
3258
-        global $post;
3259
-        $product_id = $post->ID;
3260
-
3261
-        $variations = self::get_variation($product_id);
3262
-
3263
-        $sales_informations = array();
3264
-        /** Query **/
3265
-        $data_to_compare = '"item_id";s:' . strlen($product_id) . ':"' . $product_id . '";';
3266
-        $query_args = array('posts_per_page' => -1, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ORDER, 'meta_query' => array(array('key' => '_order_postmeta', 'value' => $data_to_compare, 'compare' => 'LIKE')));
3267
-        $orders = new WP_Query($query_args);
3268
-        if (!empty($orders) && !empty($orders->posts)) {
3269
-            foreach ($orders->posts as $order) {
3270
-                $order_meta = get_post_meta($order->ID, '_order_postmeta', true);
3271
-                $order_info = get_post_meta($order->ID, '_order_info', true);
3272
-                $sales_informations[] = array(
3273
-                    'order_key' => (!empty($order_meta) && !empty($order_meta['order_key'])) ? $order_meta['order_key'] : '',
3274
-                    'order_date' => (!empty($order_meta) && !empty($order_meta['order_date'])) ? $order_meta['order_date'] : '',
3275
-                    'customer_firstname' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_first_name'])) ? $order_info['billing']['address']['address_first_name'] : '',
3276
-                    'customer_name' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_last_name'])) ? $order_info['billing']['address']['address_last_name'] : '',
3277
-                    'customer_email' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_user_email'])) ? $order_info['billing']['address']['address_user_email'] : '',
3278
-                );
3279
-            }
3280
-        }
3281
-
3282
-        /** If product has been ordered **/
3283
-        $output = '';
3284
-        if (!empty($sales_informations)) {
3285
-            $output .= '<p>' . __('This product has been ordered', 'wpshop') . ' :</p>';
3286
-            $output .= '<ul>';
3287
-            foreach ($sales_informations as $sales_information) {
3288
-                $output .= '<li>' . sprintf(__('Ordered by %s %s (%s) on %s (Order ref. : %s)', 'wpshop'), $sales_information['customer_name'], $sales_information['customer_firstname'], $sales_information['customer_email'], $sales_information['order_date'], $sales_information['order_key']) . '</li>';
3289
-            }
3290
-            $output .= '</ul>';
3291
-        } else {
3292
-            $output .= __('This product has never been ordered', 'wpshop');
3293
-        }
3294
-
3295
-        echo $output;
3296
-    }
3297
-
3298
-    public function wps_get_summary_variations_product($product_id, $the_product, $has_variation)
3299
-    {
3300
-        global $wpdb, $wpshop_payment;
3301
-        $output = '';
3302
-        $tpl_component = array();
3303
-        /**    Get attribute order for current product    */
3304
-        $product_attribute_order_detail = wpshop_attributes_set::getAttributeSetDetails(get_post_meta($product_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
3305
-        $output_order = array();
3306
-        if (count($product_attribute_order_detail) > 0) {
3307
-            foreach ($product_attribute_order_detail as $product_attr_group_id => $product_attr_group_detail) {
3308
-                foreach ($product_attr_group_detail['attribut'] as $position => $attribute_def) {
3309
-                    if (!empty($attribute_def->code)) {
3310
-                        $output_order[$attribute_def->code] = $position;
3311
-                    }
3312
-
3313
-                }
3314
-            }
3315
-        }
3316
-        $variation_attribute_ordered = array();
3317
-
3318
-        /** Check if product is a variation and change his name **/
3319
-        $product_post_type = get_post_type($the_product['product_id']);
3320
-        if (!empty($product_post_type) && $product_post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
3321
-            $parent_infos = wpshop_products::get_parent_variation($the_product['product_id']);
3322
-            $parent_post = (!empty($parent_infos) && !empty($parent_infos['parent_post'])) ? $parent_infos['parent_post'] : array();
3323
-            $the_product['product_name'] = $the_product['post_title'] = $parent_post->post_title;
3324
-        }
3325
-
3326
-        foreach ($the_product as $product_definition_key => $product_definition_value) {
3327
-            if ($product_definition_key != 'item_meta') {
3328
-                $tpl_component['PRODUCT_MAIN_INFO_' . strtoupper($product_definition_key)] = $product_definition_value;
3329
-                if (!empty($wpshop_current_for_display) && in_array($product_definition_key, unserialize(WPSHOP_ATTRIBUTE_PRICES))) {
3330
-                    $tpl_component['PRODUCT_MAIN_INFO_' . strtoupper($product_definition_key)] = $product_definition_value;
3331
-                }
3332
-            } else {
3333
-                $variation_attribute_ordered = wpshop_products::get_selected_variation_display($product_definition_value, $output_order, 'selection_summary');
3334
-            }
3335
-        }
3336
-
3337
-        ksort($variation_attribute_ordered['attribute_list']);
3338
-        $tpl_component['PRODUCT_VARIATION_SUMMARY_DETAILS'] = '';
3339
-        foreach ($variation_attribute_ordered['attribute_list'] as $attribute_variation_to_output) {
3340
-            $tpl_component['PRODUCT_VARIATION_SUMMARY_DETAILS'] .= $attribute_variation_to_output;
3341
-        }
3342
-
3343
-        /**    For security get all attributes defined as user defined or used in variation in order to set default value to empty    */
3344
-        $attribute_list = wpshop_attributes::getElement('yes', "'valid'", "is_used_for_variation", true);
3345
-        if (!empty($attribute_list)) {
3346
-            foreach ($attribute_list as $attribute_def) {
3347
-                $tpl_component['VARIATION_SUMMARY_ATTRIBUTE_PER_PRICE_' . strtoupper($attribute_def->code)] = '-';
3348
-            }
3349
-        }
3350
-
3351
-        /**    Fill the array with all prices for different variations    */
3352
-        foreach ($variation_attribute_ordered['prices'] as $attribute => $prices) {
3353
-            $tpl_component['VARIATION_SUMMARY_ATTRIBUTE_PER_PRICE_' . strtoupper($attribute)] = $prices;
3354
-        }
3355
-
3356
-        $tpl_component['PRODUCT_VARIATION_SUMMARY_MORE_CONTENT'] = '';
3357
-        $query = $wpdb->prepare("SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = %s ", '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options');
3358
-        $post_list_with_options = $wpdb->get_results($query);
3359
-        if (!empty($post_list_with_options)) {
3360
-            $additionnal_price = 0;
3361
-            foreach ($post_list_with_options as $product_info) {
3362
-                $product_meta = unserialize($product_info->meta_value);
3363
-                if (!empty($product_meta['cart']) && !empty($product_meta['cart']['auto_add']) && ($product_meta['cart']['auto_add'] == 'yes')) {
3364
-                    $product = wpshop_products::get_product_data($product_info->post_id, true, '"publish", "draft"');
3365
-
3366
-                    $the_product = array_merge(array(
3367
-                        'product_id' => $product_info->post_id,
3368
-                        'product_qty' => 1,
3369
-                    ), $product);
3370
-
3371
-                    $additionnal_price += (!$different_currency || ($change_rate == 1)) ? $the_product['product_price'] : ($the_product['product_price'] * $change_rate);
3372
-                    $tpl_component['AUTO_PRODUCT_NAME'] = $the_product['product_name'];
3373
-
3374
-                    $tpl_component['AUTO_PRODUCT_PRODUCT_PRICE'] = wpshop_display::format_field_output('wpshop_product_price', (!$different_currency || ($change_rate == 1)) ? $the_product['product_price'] : ($the_product['product_price'] * $change_rate));
3375
-                    $tpl_component['PRODUCT_VARIATION_SUMMARY_MORE_CONTENT'] = wpshop_display::display_template_element('wpshop_product_configuration_summary_detail_auto_product', $tpl_component);
3376
-                }
3377
-            }
3378
-        }
3379
-
3380
-        $tpl_component['PRODUCT_VARIATION_SUMMARY_GRAND_TOTAL'] = '';
3381
-        $tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT'] = '';
3382
-        if (!empty($additionnal_price)) {
3383
-            $tpl_component['SUMMARY_FINAL_RESULT_PRICE'] = wpshop_display::format_field_output('wpshop_product_price', $tpl_component['PRODUCT_MAIN_INFO_PRODUCT_PRICE'] + $additionnal_price);
3384
-            $tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT'] = ($tpl_component['PRODUCT_MAIN_INFO_PRODUCT_PRICE'] + $additionnal_price);
3385
-            $tpl_component['PRODUCT_VARIATION_SUMMARY_GRAND_TOTAL'] = wpshop_display::display_template_element('wpshop_product_configuration_summary_detail_final_result', $tpl_component);
3386
-        }
3387
-
3388
-        /**    Call informtion for partial payment    */
3389
-        $partial_payment = $wpshop_payment->partial_payment_calcul($tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT']);
3390
-        $tpl_component['PARTIAL_PAYMENT_INFO'] = !empty($partial_payment['amount_to_pay']) ? $partial_payment['display'] : '';
3391
-
3392
-        /**    Define the current selected currency for the order summary    */
2207
+		$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
2208
+		if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
2209
+			/*    Include the old way template part    */
2210
+			ob_start();
2211
+			require wpshop_display::get_template_file($tpl_way_to_take[1]);
2212
+			$galery_output = ob_get_contents();
2213
+			ob_end_clean();
2214
+		} else {
2215
+			$galery_output = wpshop_display::display_template_element($template_part, $tpl_component);
2216
+		}
2217
+		unset($tpl_component);
2218
+
2219
+		return $galery_output;
2220
+	}
2221
+
2222
+	/**
2223
+	 * Define the metabox to display in product edition page in backend
2224
+	 * @param object $post The current element displayed for edition
2225
+	 */
2226
+	public static function meta_box_variations($post)
2227
+	{
2228
+		$output = '';
2229
+		/*    Variations container    */
2230
+		$tpl_component = array();
2231
+		$tpl_component['ADMIN_VARIATION_CONTAINER'] = self::display_variation_admin($post->ID);
2232
+		$tpl_component['LINK_NEW_INTERFACE'] = wp_nonce_url(get_edit_post_link($post->ID) . '&wps_variation_interface=true', 'wps_remove_variation_interface');
2233
+		$output .= wpshop_display::display_template_element('wpshop_admin_variation_metabox', $tpl_component, array(), 'admin');
2234
+		echo '<span class="wpshop_loading_ wpshopHide" ><img src="' . admin_url('images/loading.gif') . '" alt="loading picture" /></span>' . $output . '<div class="wpshop_cls" ></div>';
2235
+	}
2236
+
2237
+	/**
2238
+	 * Call variation creation function with a list of defined variation
2239
+	 *
2240
+	 * @param array $possible_variations A list of variation to create for the current element
2241
+	 * @param integer $element_id The product we want to create variation for
2242
+	 *
2243
+	 * @return mixed The last created variation identifier
2244
+	 */
2245
+	public static function creation_variation_callback($possible_variations, $element_id)
2246
+	{
2247
+		/** Get existing variation    */
2248
+		$existing_variations_in_db = wpshop_products::get_variation($element_id);
2249
+		$existing_variations = array();
2250
+		if (!empty($existing_variations_in_db)) {
2251
+			foreach ($existing_variations_in_db as $variations_def) {
2252
+				$existing_variations[] = $variations_def['variation_def'];
2253
+			}
2254
+		}
2255
+		/** New variation definition    */
2256
+		$attribute_defining_variation = get_post_meta($element_id, '_wpshop_variation_defining', true);
2257
+
2258
+		/**    Read possible values    */
2259
+		foreach ($possible_variations as $variation_definition) {
2260
+			if (in_array($variation_definition, $existing_variations)) {
2261
+				continue;
2262
+			}
2263
+
2264
+			$attribute_to_set = array();
2265
+			foreach ($variation_definition as $attribute_code => $attribute_selected_value) {
2266
+				$attribute = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2267
+				$attribute_to_set[$attribute->data_type][$attribute_code] = $attribute_selected_value;
2268
+				if (empty($attribute_defining_variation['attributes']) || (!in_array($attribute_code, $attribute_defining_variation['attributes']))) {
2269
+					$attribute_defining_variation['attributes'][] = $attribute_code;
2270
+				}
2271
+			}
2272
+			$variation_id = wpshop_products::create_variation($element_id, $attribute_to_set);
2273
+		}
2274
+		update_post_meta($element_id, '_wpshop_variation_defining', $attribute_defining_variation);
2275
+
2276
+		return !empty($variation_id) ? $variation_id : 0;
2277
+	}
2278
+
2279
+	/**
2280
+	 * Create a new variation for product
2281
+	 *
2282
+	 * @param integer $head_product The product identifier to create the new variation for
2283
+	 * @param array $variation_attributes Attribute list for the variation
2284
+	 *
2285
+	 * @return mixed <number, WP_Error> The variation identifier or an error in case the creation was not succesfull
2286
+	 */
2287
+	public static function create_variation($head_product, $variation_attributes)
2288
+	{
2289
+		/** Create custom title */
2290
+		$title_variation = "";
2291
+		if (!empty($variation_attributes)) {
2292
+			foreach ($variation_attributes as $type => $array) {
2293
+				if (!empty($array)) {
2294
+					foreach ($array as $code => $value) {
2295
+						$title_variation .= " " . $code . " " . $value;
2296
+					}
2297
+				}
2298
+			}
2299
+		}
2300
+
2301
+		$variation = array(
2302
+			'post_title' => sprintf(__('Product %s variation %s', 'wpshop'), $head_product, get_the_title($head_product)) . $title_variation,
2303
+			'post_content' => '',
2304
+			'post_status' => 'publish',
2305
+			'post_author' => get_current_user_id(),
2306
+			'post_parent' => $head_product,
2307
+			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
2308
+		);
2309
+		$variation_id = wp_insert_post($variation);
2310
+
2311
+		wpshop_attributes::saveAttributeForEntity($variation_attributes, wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $variation_id, WPSHOP_CURRENT_LOCALE, '');
2312
+
2313
+		/*    Update product price looking for shop parameters    */
2314
+		wpshop_products::calculate_price($variation_id);
2315
+
2316
+		/*    Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working    */
2317
+		$productMetaDatas = array();
2318
+		foreach ($variation_attributes as $attributeType => $attributeValues) {
2319
+			foreach ($attributeValues as $attributeCode => $attributeValue) {
2320
+				if (!empty($attributeValue)) {
2321
+					$productMetaDatas[$attributeCode] = $attributeValue;
2322
+				}
2323
+			}
2324
+		}
2325
+		update_post_meta($variation_id, '_wpshop_variations_attribute_def', $productMetaDatas);
2326
+		update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
2327
+		update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, get_post_meta($head_product, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
2328
+
2329
+		return $variation_id;
2330
+	}
2331
+
2332
+	/**
2333
+	 * Get variation list for a given product
2334
+	 *
2335
+	 * @param integer $head_product The product identifier to get the variation for
2336
+	 * @return object The variation list
2337
+	 */
2338
+	public static function get_variation($head_product)
2339
+	{
2340
+		global $wpdb;
2341
+		$variations_output = null;
2342
+		$args = array(
2343
+			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
2344
+			'post_parent' => $head_product,
2345
+			'orderby' => 'ID',
2346
+			'order' => 'ASC',
2347
+			'posts_per_page' => -1,
2348
+			'post_status' => 'any',
2349
+		);
2350
+		$variations = get_posts($args);
2351
+
2352
+		if (!empty($variations)) {
2353
+			$head_wpshop_variation_definition = get_post_meta($head_product, '_wpshop_variation_defining', true);
2354
+
2355
+			foreach ($variations as $post_def) {
2356
+				$data = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $post_def->ID, WPSHOP_CURRENT_LOCALE, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, "'valid', 'deleted'");
2357
+				foreach ($data as $content) {
2358
+					$attribute_value = 'attribute_value_' . $content->data_type;
2359
+					if (!empty($content->$attribute_value)) {
2360
+						if (!empty($head_wpshop_variation_definition['attributes']) && in_array($content->code, $head_wpshop_variation_definition['attributes'])) {
2361
+							$variations_output[$post_def->ID]['variation_def'][$content->code] = $content->$attribute_value;
2362
+						} else {
2363
+							$variations_output[$post_def->ID]['variation_dif'][$content->code] = $content->$attribute_value;
2364
+						}
2365
+					}
2366
+				}
2367
+				$variations_output[$post_def->ID]['post'] = $post_def;
2368
+			}
2369
+		}
2370
+		wp_reset_query();
2371
+		return $variations_output;
2372
+	}
2373
+
2374
+	/**
2375
+	 * Affichage des variations d'un produit dans l'administration
2376
+	 *
2377
+	 * @param integer $head_product L'identifiant du produit dont on veut afficher les variations
2378
+	 * @return string Le code html permettant l'affichage des variations dans l'interface d'édition du produit
2379
+	 */
2380
+	public static function display_variation_admin($head_product)
2381
+	{
2382
+		$output = '';
2383
+		$productCurrency = wpshop_tools::wpshop_get_currency();
2384
+		/*    Récupération de la liste des variations pour le produit en cours d'édition    */
2385
+		$variations = self::get_variation($head_product);
2386
+		$price_piloting = get_option('wpshop_shop_price_piloting');
2387
+
2388
+		/*    Affichage de la liste des variations pour le produit en cours d'édition    */
2389
+		if (!empty($variations) && is_array($variations)) {
2390
+			$existing_variation_list = wpshop_display::display_template_element('wpshop_admin_existing_variation_controller', array(), array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product), 'admin');
2391
+
2392
+			foreach ($variations as $variation) {
2393
+				$tpl_component = array();
2394
+
2395
+				$tpl_component['ADMIN_EXISTING_VARIATIONS_CLASS'] = ' wpshop_variation_' . self::currentPageCode;
2396
+				$tpl_component['VARIATION_IDENTIFIER'] = $variation['post']->ID;
2397
+				$tpl_component['VARIATION_DETAIL'] = '  ';
2398
+				$p = (!empty($variation['variation_dif']['product_price']) || !empty($variation['variation_dif']['price_ht'])) ? ((!empty($price_piloting) && $price_piloting == 'HT' && !empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : (!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0) : 0;
2399
+				$tpl_component['VARIATION_DETAIL_PRICE'] = number_format($p, 2, '.', '') . ' ' . $productCurrency . ' ' . ((!empty($price_piloting) && $price_piloting == 'HT') ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
2400
+				if (!empty($price_piloting) && $price_piloting == 'HT') {
2401
+
2402
+				} else {
2403
+
2404
+				}
2405
+				$post_obj = $variation['post'];
2406
+
2407
+				$parent_product_infos = wpshop_products::get_parent_variation($post_obj->ID);
2408
+				if (!empty($parent_product_infos)) {
2409
+					$parent_post = $parent_product_infos['parent_post'];
2410
+					$product_option_postmeta = get_post_meta($parent_post->ID, '_wpshop_variation_defining', true);
2411
+					if (!empty($product_option_postmeta['options']['price_behaviour']) && !empty($product_option_postmeta['options']['price_behaviour'][0]) && $product_option_postmeta['options']['price_behaviour'][0] == 'addition') {
2412
+						if (!empty($price_piloting) && $price_piloting == 'HT') {
2413
+							$product_price = ((!empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : 0) + $parent_product_infos['parent_post_meta']['price_ht'];
2414
+						} else {
2415
+							$product_price = ((!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0) + $parent_product_infos['parent_post_meta']['product_price'];
2416
+						}
2417
+						$tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Variation price combined with the parent product price', 'wpshop');
2418
+					} else {
2419
+						if (!empty($price_piloting) && $price_piloting == 'HT') {
2420
+							$product_price = (!empty($variation['variation_dif']['price_ht'])) ? $variation['variation_dif']['price_ht'] : 0;
2421
+						} else {
2422
+							$product_price = (!empty($variation['variation_dif']['product_price'])) ? $variation['variation_dif']['product_price'] : 0;
2423
+						}
2424
+						$tpl_component['VARIATION_DETAIL_SALE_PRICE_INDICATION'] = __('Only variation\'s price is used', 'wpshop');
2425
+					}
2426
+					$product_price = number_format(str_replace(',', '.', $product_price), 2, '.', '') . ' ' . $productCurrency . ' ' . ((!empty($price_piloting) && $price_piloting == 'HT') ? __('ET', 'wpshop') : __('ATI', 'wpshop'));
2427
+					$tpl_component['VARIATION_DETAIL_SALE_PRICE'] = $product_price;
2428
+				}
2429
+
2430
+				if (!empty($variation['variation_def'])) {
2431
+					foreach ($variation['variation_def'] as $variation_key => $variation_value) {
2432
+						if (!empty($variation_value)) {
2433
+							$attribute_def_for_variation = wpshop_attributes::getElement($variation_key, "'valid'", 'code');
2434
+							$tpl_component['VARIATION_DETAIL'] .= '<input type="hidden" name="' . self::current_page_variation_code . '[' . $variation['post']->ID . '][attribute][' . $attribute_def_for_variation->data_type . '][' . $variation_key . ']" value="' . $variation_value . '" />' . wpshop_display::display_template_element('wpshop_admin_variation_item_def_header', array('VARIATION_ATTRIBUTE_CODE' => $attribute_def_for_variation->frontend_label, 'VARIATION_ATTRIBUTE_CODE_VALUE' => stripslashes(wpshop_attributes::get_attribute_type_select_option_info($variation_value, 'label', $attribute_def_for_variation->data_type_to_use, true))), array(), 'admin');
2435
+							$tpl_component['VARIATION_IMAGE_CHOICE'] = '';
2436
+
2437
+							/** Define Link image to variation interface **/
2438
+							if (!empty($product_option_postmeta) && !empty($product_option_postmeta['attributes']) && !empty($product_option_postmeta['variation_type']) && ((count($product_option_postmeta['attributes']) == 1 && $product_option_postmeta['variation_type'] == 'single') || ($product_option_postmeta['variation_type'] == 'combined'))) {
2439
+								$pictures = get_posts(array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $head_product));
2440
+
2441
+								$media_id_data = get_post_meta($head_product, '_wps_product_media', true);
2442
+								if (!empty($media_id_data)) {
2443
+									$medias_ids = explode(',', $media_id_data);
2444
+
2445
+									if (!empty($medias_ids)) {
2446
+										foreach ($medias_ids as $media_id) {
2447
+											if (!empty($media_id)) {
2448
+												$pictures[] = get_post($media_id);
2449
+											}
2450
+										}
2451
+									}
2452
+								}
2453
+
2454
+								$pictures_data = '';
2455
+								if (!empty($pictures)) {
2456
+									$selected_picture = get_post_meta($variation['post']->ID, '_wps_variation_attached_picture', true);
2457
+
2458
+									$done_picture = array();
2459
+									foreach ($pictures as $picture) {
2460
+										if (!in_array($picture->ID, $done_picture)) {
2461
+											if (wp_attachment_is_image($picture->ID)) {
2462
+												$checked = ((!empty($selected_picture) && $selected_picture == $picture->ID) ? 'checked="checked"' : '');
2463
+												$pictures_data .= wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_element', array('PICTURE_CHOICE_VARIATION_ID' => $picture->ID, 'PRODUCT_VARIATION_ID' => $variation['post']->ID, 'PICTURE_CHOICE_SELECTED' => $checked, 'PICTURE_CHOICE_VARIATION_IMG' => wp_get_attachment_image($picture->ID, 'thumbnail')), array(), 'admin');
2464
+
2465
+												$done_picture[] = $picture->ID;
2466
+											}
2467
+										}
2468
+									}
2469
+								}
2470
+
2471
+								$tpl_component['VARIATION_IMAGE_CHOICE'] = wpshop_display::display_template_element('wpshop_admin_variation_picture_choice_container', array('PICTURE_CHOICE_CONTAINER_CONTENT' => $pictures_data), array(), 'admin');
2472
+							}
2473
+
2474
+						}
2475
+					}
2476
+				}
2477
+
2478
+				$tpl_component['VARIATION_DETAIL'] = substr($tpl_component['VARIATION_DETAIL'], 0, -2);
2479
+
2480
+				$tpl_component['ADMIN_VARIATION_SPECIFIC_DEFINITION_CONTAINER_CLASS'] = ' wpshopHide';
2481
+				$tpl_component['VARIATION_DEFINITION'] = wpshop_attributes::get_variation_attribute(array('post_id' => $variation['post']->ID, 'input_class' => ' ', 'field_name' => wpshop_products::current_page_variation_code . '[' . $variation['post']->ID . ']', 'page_code' => self::current_page_variation_code, 'field_id' => self::current_page_variation_code . '_' . $variation['post']->ID, 'variation_dif_values' => (!empty($variation['variation_dif']) ? $variation['variation_dif'] : array())));
2482
+				$tpl_component['VARIATION_DEFINITION_CONTENT'] = wpshop_display::display_template_element('wpshop_admin_variation_item_specific_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
2483
+
2484
+				/*    Add the variation definition to output    */
2485
+				$existing_variation_list .= wpshop_display::display_template_element('wpshop_admin_variation_item_def', $tpl_component, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT => $head_product, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION => $variation['post']->ID), 'admin');
2486
+			}
2487
+
2488
+			$output .= wpshop_display::display_template_element('wpshop_admin_existing_variation_list', array('ADMIN_EXISTING_VARIATIONS_CONTAINER_CLASS' => '', 'ADMIN_EXISTING_VARIATIONS_CONTAINER' => $existing_variation_list), array(), 'admin');
2489
+			/*    Reset de la liste des résultats pour éviter les comportements indésirables    */
2490
+			wp_reset_query();
2491
+		} else {
2492
+			$output = __('No variation found for this product. Please use button above for create one', 'wpshop');
2493
+		}
2494
+
2495
+		return $output;
2496
+	}
2497
+
2498
+	/**
2499
+	 * Retrieve and display the variation for a given product
2500
+	 * @param integer $product_id The product identifier to get variation for
2501
+	 */
2502
+	public static function wpshop_variation($post_id = '', $from_admin = false, $order_id = '', $qty = 1)
2503
+	{
2504
+		global $wp_query;
2505
+		$output = '';
2506
+
2507
+		$product_id = empty($post_id) ? $wp_query->post->ID : $post_id;
2508
+		$wpshop_product_attributes_frontend_display = get_post_meta($product_id, '_wpshop_product_attributes_frontend_display', true);
2509
+		$head_wpshop_variation_definition = get_post_meta($product_id, '_wpshop_variation_defining', true);
2510
+
2511
+		/**    Get attribute order for current product    */
2512
+		$product_attribute_order_detail = wpshop_attributes_set::getAttributeSetDetails(get_post_meta($product_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
2513
+		$output_order = array();
2514
+		if (count($product_attribute_order_detail) > 0) {
2515
+			if (!empty($product_attribute_order_detail)) {
2516
+				foreach ($product_attribute_order_detail as $product_attr_group_id => $product_attr_group_detail) {
2517
+					foreach ($product_attr_group_detail['attribut'] as $position => $attribute_def) {
2518
+						if (!empty($attribute_def->code)) {
2519
+							$output_order[$attribute_def->code] = $position;
2520
+						}
2521
+
2522
+					}
2523
+				}
2524
+			}
2525
+		}
2526
+
2527
+		$variations_params = array();
2528
+		$variation_attribute = array();
2529
+		$variation_attribute_ordered = array();
2530
+		$possible_values = array();
2531
+		$possible_values_for_selection_calculation = array();
2532
+
2533
+		/*    Vérification de l'existence de déclinaison pour le produit    */
2534
+		$wpshop_variation_list = self::get_variation($product_id);
2535
+		if (!empty($wpshop_variation_list)) {
2536
+			foreach ($wpshop_variation_list as $variation) {
2537
+				if (!empty($variation['variation_def'])) {
2538
+					$display_option = get_post_meta($post_id, '_wpshop_product_attributes_frontend_display', true);
2539
+					foreach ($variation['variation_def'] as $attribute_code => $attribute_value) {
2540
+						if (empty($display_option) || (!empty($display_option['attribute']) && !empty($display_option['attribute'][$attribute_code]) && !empty($display_option['attribute'][$attribute_code]['complete_sheet']))) {
2541
+							$tpl_component = array();
2542
+
2543
+							$attribute_db_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2544
+							$default_value_is_serial = false;
2545
+							$attribute_list_first_element = $attribute_db_definition->default_value;
2546
+							if (!empty($attribute_db_definition->default_value) && ($attribute_db_definition->default_value == serialize(false) || wpshop_tools::is_serialized($attribute_db_definition->default_value))) {
2547
+								$default_value_is_serial = true;
2548
+								$tmp_default_value = unserialize($attribute_db_definition->default_value);
2549
+								$attribute_list_first_element = !empty($tmp_default_value['field_options']['label_for_first_item']) ? $tmp_default_value['field_options']['label_for_first_item'] : null;
2550
+							}
2551
+
2552
+							if ($default_value_is_serial && !empty($attribute_list_first_element) && ($attribute_list_first_element != 'none')) {
2553
+								$possible_values[$attribute_code][0][0] = ($default_value_is_serial && !empty($attribute_list_first_element) && ($attribute_list_first_element != 'none')) ? stripslashes(sprintf($attribute_list_first_element, strtolower($attribute_db_definition->frontend_label))) : __('Choose a value', 'wpshop');
2554
+							}
2555
+
2556
+							if (!empty($attribute_value) && ($attribute_db_definition->data_type_to_use == 'custom')) {
2557
+								$tpl_component['VARIATION_VALUE'] = stripslashes(wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'label', 'custom'));
2558
+								$position = wpshop_attributes::get_attribute_type_select_option_info($attribute_value, 'position', 'custom');
2559
+							} else if (!empty($attribute_value) && ($attribute_db_definition->data_type_to_use == 'internal')) {
2560
+								$post_def = get_post($attribute_value);
2561
+								$tpl_component['VARIATION_VALUE'] = stripslashes($post_def->post_title);
2562
+								$position = $post_def->menu_order;
2563
+							}
2564
+
2565
+							if (!empty($variation['variation_dif'])) {
2566
+								foreach ($variation['variation_dif'] as $attribute_dif_code => $attribute_dif_value) {
2567
+									$wpshop_prices_attributes = unserialize(WPSHOP_ATTRIBUTE_PRICES);
2568
+									$the_value = $attribute_dif_value;
2569
+									if (in_array($attribute_dif_code, $wpshop_prices_attributes)) {
2570
+										$the_value = wpshop_display::format_field_output('wpshop_product_price', $attribute_dif_value);
2571
+									}
2572
+									$tpl_component['VARIATION_DIF_' . strtoupper($attribute_dif_code)] = stripslashes($the_value);
2573
+								}
2574
+							}
2575
+							if (!empty($attribute_value)) {
2576
+								$possible_values[$attribute_code][$position][$attribute_value] = wpshop_display::display_template_element('product_variation_item_possible_values', $tpl_component, array('type' => 'attribute_for_variation', 'id' => $attribute_code));
2577
+								$possible_values_for_selection_calculation[$attribute_code][$attribute_value] = $tpl_component['VARIATION_VALUE'];
2578
+							}
2579
+							unset($tpl_component);
2580
+						}
2581
+					}
2582
+				}
2583
+			}
2584
+
2585
+			$variation_tpl = array();
2586
+			if (!empty($head_wpshop_variation_definition['attributes'])) {
2587
+				foreach ($head_wpshop_variation_definition['attributes'] as $attribute_code) {
2588
+					$attribute_db_definition = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
2589
+
2590
+					if (!empty($attribute_db_definition)) {
2591
+						$attribute_display_state = wpshop_attributes::check_attribute_display($attribute_db_definition->is_visible_in_front, $wpshop_product_attributes_frontend_display, 'attribute', $attribute_code, 'complete_sheet');
2592
+
2593
+						$is_required = ((!empty($head_wpshop_variation_definition['options']) && !empty($head_wpshop_variation_definition['options']['required_attributes']) && (in_array($attribute_code, $head_wpshop_variation_definition['options']['required_attributes'])))) ? true : false;
2594
+						if (!$is_required && $attribute_db_definition->is_required == 'yes') {
2595
+							$is_required = true;
2596
+						}
2597
+
2598
+						$input_def = array();
2599
+						$input_def['type'] = $attribute_db_definition->frontend_input;
2600
+						$value = isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code] : (!empty($attribute_db_definition->default_value) ? $attribute_db_definition->default_value : null);
2601
+						if (in_array($attribute_db_definition->frontend_input, array('radio', 'checkbox'))) {
2602
+							unset($possible_values[$attribute_code][0]);
2603
+							$value = array($value);
2604
+						}
2605
+						$input_def['id'] = 'wpshop_variation_attr_' . $attribute_code;
2606
+						$input_def['name'] = $attribute_code;
2607
+						$real_possible_values = array();
2608
+						if (!empty($possible_values[$attribute_code])) {
2609
+							ksort($possible_values[$attribute_code]);
2610
+							foreach ($possible_values[$attribute_code] as $position => $def) {
2611
+								foreach ($def as $attribute_value => $attribute_value_output) {
2612
+									$real_possible_values[$attribute_value] = $attribute_value_output;
2613
+									if (!empty($attribute_value)) {
2614
+										global $wpdb;
2615
+										$query = $wpdb->prepare("SELECT post_status FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_value LIKE '%%" . serialize($attribute_code) . serialize($attribute_value) . "%%'", $product_id);
2616
+										if ('draft' == $wpdb->get_var($query)) {
2617
+											unset($real_possible_values[$attribute_value]);
2618
+										}
2619
+									}
2620
+								}
2621
+							}
2622
+						}
2623
+						$input_def['possible_value'] = $real_possible_values;
2624
+						$input_def['valueToPut'] = 'index';
2625
+						$input_def['value'] = $value;
2626
+
2627
+						$input_def['options']['more_input'] = '';
2628
+						if (!empty($possible_values_for_selection_calculation[$attribute_code])) {
2629
+							foreach ($possible_values_for_selection_calculation[$attribute_code] as $value_id => $value) {
2630
+								$input_def['options']['more_input'] .= '<input type="hidden" disabled="disabled" value="' . str_replace("\\", "", $value) . '" name="' . $input_def['id'] . '_current_value" id="' . $input_def['id'] . '_current_value_' . $value_id . '" />';
2631
+							}
2632
+						}
2633
+
2634
+						$input_def['options_label']['original'] = true;
2635
+						$input_def['option'] = 'data-nonce="' . wp_create_nonce('wpshop_ajax_wpshop_variation_selection') . '" class="wpshop_variation_selector_input' . ($is_required ? ' attribute_is_required_input attribute_is_required_input_' . $attribute_code . ' ' : '') . ($attribute_db_definition->_display_informations_about_value == 'yes' ? ' wpshop_display_information_about_value' : '') . ' ' . ((is_admin()) ? $attribute_db_definition->backend_css_class : $attribute_db_definition->frontend_css_class) . '" ';
2636
+
2637
+						if (!empty($real_possible_values)) {
2638
+							$tpl_component = array();
2639
+							$attribute_output_def['value'] = isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_code] : $input_def['value'];
2640
+							$tpl_component['VARIATION_INPUT'] = wpshop_form::check_input_type($input_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) . $input_def['options']['more_input'];
2641
+							$tpl_component['VARIATION_LABEL'] = ($is_required ? '<span class="attribute_is_required attribute_is_required_' . $attribute_code . '" >' . stripslashes($attribute_db_definition->frontend_label) . '</span> <span class="required" >*</span>' : stripslashes($attribute_db_definition->frontend_label));
2642
+							$tpl_component['VARIATION_CODE'] = $attribute_code;
2643
+							$tpl_component['VARIATION_LABEL_HELPER'] = !empty($attribute_db_definition->frontend_help_message) ? ' title="' . $attribute_db_definition->frontend_help_message . '" ' : '';
2644
+							$tpl_component['VARIATION_LABEL_CLASS'] = !empty($attribute_db_definition->frontend_help_message) ? ' wpshop_att_variation_helper' : '';
2645
+							$tpl_component['VARIATION_IDENTIFIER'] = $input_def['id'];
2646
+							$tpl_component['VARIATION_PARENT_ID'] = $product_id;
2647
+							$tpl_component['VARIATION_PARENT_TYPE'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
2648
+							$tpl_component['VARIATION_CONTAINER_CLASS'] = ($is_required ? ' attribute_is_required_container attribute_is_required_container_' . $attribute_code : '') . ' wpshop_variation_' . $attribute_code . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_' . $product_id;
2649
+							$tpl_component['VARIATION_REQUIRED_INDICATION'] = ($is_required) ? __('Required variation', 'wpshop') : '';
2650
+							$variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_code)] = wpshop_display::display_template_element('product_variation_item', $tpl_component);
2651
+							$variation_attribute_ordered[$output_order[$attribute_code]] = $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_code)];
2652
+						}
2653
+
2654
+						$variation_attribute[] = $attribute_code;
2655
+					}
2656
+				}
2657
+			}
2658
+
2659
+		}
2660
+		$variation_tpl['VARIATION_FORM_ELEMENT_ID'] = $product_id;
2661
+		wp_reset_query();
2662
+
2663
+		$attribute_defined_to_be_user_defined = wpshop_attributes::get_attribute_user_defined(array('entity_type_id' => self::currentPageCode));
2664
+		if (!empty($attribute_defined_to_be_user_defined)) {
2665
+			foreach ($attribute_defined_to_be_user_defined as $attribute_not_in_variation_but_user_defined) {
2666
+				$is_required = ((!empty($head_wpshop_variation_definition['options']) && !empty($head_wpshop_variation_definition['options']['required_attributes']) && (in_array($attribute_not_in_variation_but_user_defined->code, $head_wpshop_variation_definition['options']['required_attributes']))) || $attribute_not_in_variation_but_user_defined->is_required == 'yes') ? true : false;
2667
+
2668
+				$attribute_display_state = wpshop_attributes::check_attribute_display($attribute_not_in_variation_but_user_defined->is_visible_in_front, $wpshop_product_attributes_frontend_display, 'attribute', $attribute_not_in_variation_but_user_defined->code, 'complete_sheet');
2669
+				if ($attribute_display_state && array_key_exists($attribute_not_in_variation_but_user_defined->code, $output_order) && !in_array($attribute_not_in_variation_but_user_defined->code, $variation_attribute) && ($attribute_not_in_variation_but_user_defined->is_used_for_variation == 'no')) {
2670
+					$attribute_output_def = wpshop_attributes::get_attribute_field_definition($attribute_not_in_variation_but_user_defined, (is_array($head_wpshop_variation_definition) && isset($head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_not_in_variation_but_user_defined->code]) ? $head_wpshop_variation_definition['options']['attributes_default_value'][$attribute_not_in_variation_but_user_defined->code] : ''));
2671
+
2672
+					$tpl_component = array();
2673
+					$attribute_output_def['option'] = 'data-nonce="' . wp_create_nonce('wpshop_ajax_wpshop_variation_selection') . '" class="wpshop_variation_selector_input' . ($is_required ? ' attribute_is_required_input attribute_is_required_input_' . $attribute_not_in_variation_but_user_defined->code : '') . ' ' . (str_replace('"', '', str_replace('class="', '', $attribute_output_def['option']))) . ' ' . ((is_admin()) ? $attribute_not_in_variation_but_user_defined->backend_css_class : $attribute_not_in_variation_but_user_defined->frontend_css_class) . '" ';
2674
+					$tpl_component['VARIATION_INPUT'] = wpshop_form::check_input_type($attribute_output_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '[free]') . $attribute_output_def['options'];
2675
+					$tpl_component['VARIATION_LABEL'] = ($is_required ? '<span class="attribute_is_required attribute_is_required_' . $attribute_not_in_variation_but_user_defined->code . '" >' . stripslashes($attribute_not_in_variation_but_user_defined->frontend_label) . '</span> <span class="required" >*</span>' : stripslashes($attribute_not_in_variation_but_user_defined->frontend_label));
2676
+					$tpl_component['VARIATION_CODE'] = $attribute_not_in_variation_but_user_defined->code;
2677
+					$tpl_component['VARIATION_LABEL_HELPER'] = !empty($attribute_not_in_variation_but_user_defined->frontend_help_message) ? ' title="' . $attribute_not_in_variation_but_user_defined->frontend_help_message . '" ' : '';
2678
+					$tpl_component['VARIATION_LABEL_CLASS'] = !empty($attribute_not_in_variation_but_user_defined->frontend_help_message) ? ' wpshop_att_variation_helper' : '';
2679
+					$tpl_component['VARIATION_REQUIRED_INDICATION'] = ($is_required) ? __('Required variation', 'wpshop') : '';
2680
+					$tpl_component['VARIATION_IDENTIFIER'] = $attribute_output_def['id'];
2681
+					$tpl_component['VARIATION_PARENT_ID'] = $product_id;
2682
+					$tpl_component['VARIATION_PARENT_TYPE'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
2683
+					$tpl_component['VARIATION_CONTAINER_CLASS'] = ($is_required ? ' attribute_is_required_container attribute_is_required_container_' . $attribute_not_in_variation_but_user_defined->code : '') . ' wpshop_variation_' . $attribute_not_in_variation_but_user_defined->code . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . ' wpshop_variation_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_' . $product_id;
2684
+					$variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_not_in_variation_but_user_defined->code)] = ($attribute_output_def['type'] != 'hidden') ? wpshop_display::display_template_element('product_variation_item', $tpl_component) : wpshop_form::check_input_type($attribute_output_def, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION . '[free]') . $attribute_output_def['options'];
2685
+					$variation_attribute_ordered[$output_order[$attribute_not_in_variation_but_user_defined->code]] = $variation_tpl['VARIATION_COMPLETE_OUTPUT_' . strtoupper($attribute_not_in_variation_but_user_defined->code)];
2686
+				}
2687
+			}
2688
+		}
2689
+		$variation_tpl['VARIATION_FORM_VARIATION_LIST'] = '';
2690
+		if (!empty($variation_attribute_ordered) && is_array($variation_attribute_ordered)) {
2691
+			ksort($variation_attribute_ordered);
2692
+			foreach ($variation_attribute_ordered as $attribute_variation_to_output) {
2693
+				$variation_tpl['VARIATION_FORM_VARIATION_LIST'] .= $attribute_variation_to_output;
2694
+			}
2695
+		}
2696
+		$variation_tpl['FROM_ADMIN_INDICATOR'] = $variation_tpl['ORDER_ID_INDICATOR'] = '';
2697
+		$variation_tpl['PRODUCT_ADDED_TO_CART_QTY'] = (!empty($qty)) ? $qty : 1;
2698
+		if ($from_admin && !empty($order_id)) {
2699
+			$variation_tpl['FROM_ADMIN_INDICATOR'] = '<input type="hidden" name="wps_orders_from_admin" value="1" />';
2700
+			$variation_tpl['ORDER_ID_INDICATOR'] = '<input type="hidden" name="wps_orders_order_id" value="' . $order_id . '" />';
2701
+		}
2702
+		$output = !empty($variation_tpl['VARIATION_FORM_VARIATION_LIST']) ? wpshop_display::display_template_element('product_variation_form', $variation_tpl) : '';
2703
+
2704
+		return $output;
2705
+	}
2706
+
2707
+	public static function get_parent_variation($variation_id)
2708
+	{
2709
+		$result = array();
2710
+		if (!empty($variation_id)) {
2711
+			$variation_post = get_post($variation_id);
2712
+			if (!empty($variation_post) && !empty($variation_post->post_parent)) {
2713
+				$result['parent_post'] = get_post($variation_post->post_parent);
2714
+				$result['parent_post_meta'] = get_post_meta($variation_post->post_parent, '_wpshop_product_metadata', true);
2715
+			}
2716
+		}
2717
+		return $result;
2718
+	}
2719
+
2720
+	/**
2721
+	 * Display the current configuration for a given product
2722
+	 * @param array $shortcode_attribute Some parameters given by the shortcode for display
2723
+	 */
2724
+	public function wpshop_product_variations_summary($shortcode_attribute)
2725
+	{
2726
+		$output = '';
2727
+
2728
+		$product_variations_selection_args = array(
2729
+			'CURRENCY_SELECTOR' => wpshop_attributes_unit::wpshop_shop_currency_list_field(),
2730
+			'PRODUCT_VARIATION_SELECTION_DISPLAY' => '',
2731
+		);
2732
+
2733
+		$current_user_id = get_current_user_id();
2734
+
2735
+		if (!empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items'])) {
2736
+			foreach ($_SESSION['cart']['order_items'] as $item_id => $item) {
2737
+				if (!empty($item) && !empty($item['item_id'])) {
2738
+
2739
+					$free_variations = array();
2740
+					if (!empty($item['item_meta']) && !empty($item['item_meta']['free_variation'])) {
2741
+						foreach ($item['item_meta']['free_variation'] as $attribute_code => $attribute_value) {
2742
+							$free_variations[] = $attribute_code . '-_variation_val_-' . $attribute_value;
2743
+						}
2744
+					}
2745
+
2746
+					$variations = array();
2747
+					if (!empty($item['item_meta']) && !empty($item['item_meta']['variations'])) {
2748
+						foreach ($item['item_meta']['variations'] as $variation_id => $variation_def) {
2749
+
2750
+							if (!empty($variation_def['item_meta']) && !empty($variation_def['item_meta']['variation_definition'])) {
2751
+								foreach ($variation_def['item_meta']['variation_definition'] as $attribute_code => $attribute_selected_data) {
2752
+									$variations[] = $attribute_code . '-_variation_val_-' . $attribute_selected_data['ID'];
2753
+								}
2754
+							}
2755
+						}
2756
+					}
2757
+					$product_variation_summary = self::wpshop_ajax_wpshop_variation_selection($item['item_id'], $variations, $free_variations, null, $item['item_qty']);
2758
+
2759
+					$product_variations_selection_args['PRODUCT_VARIATION_SELECTION_DISPLAY'] .= $product_variation_summary[1]['product_output'];
2760
+				}
2761
+			}
2762
+		}
2763
+
2764
+		$output .= wpshop_display::display_template_element('wpshop_product_configuration_summary', $product_variations_selection_args);
2765
+
2766
+		echo $output;
2767
+	}
2768
+
2769
+	/**
2770
+	 * Affichage du résumé du produit sélectionné avec le prix par option / Display a summary of selected product with the price per option
2771
+	 *
2772
+	 * @param integer $product_id L'identifiant du produit qui est ajouté au panier / The product identifier added to cart
2773
+	 * @param array $wpshop_variation_selected La liste des options ayant un prix sélectionnées par le client / Options list with price selected by the customer
2774
+	 * @param array $wpshop_free_variation La liste des options n'entrainant pas de modification du prix final sélectionnées par le client / Options list without price selected by the customer
2775
+	 * @param string $wpshop_current_for_display Inconnu / Unknown
2776
+	 * @param integer $product_qty La quantité commandée par le client / Quantity ordered by the customer
2777
+	 *
2778
+	 * @return array Le résultat du calcul pour l'affichage / Output result
2779
+	 */
2780
+	public static function wpshop_ajax_wpshop_variation_selection($product_id, $wpshop_variation_selected, $wpshop_free_variation, $wpshop_current_for_display, $product_qty = 1)
2781
+	{
2782
+		global $wpdb;
2783
+
2784
+		$wpshop_cart = new wps_cart();
2785
+		$wpshop_products = new wpshop_products();
2786
+
2787
+		$response = '';
2788
+		$response_status = $has_variation = false;
2789
+		$tpl_component = array();
2790
+
2791
+		// Check if variations exists
2792
+		if (!empty($wpshop_variation_selected) || !empty($wpshop_free_variation)) {
2793
+
2794
+			//Recover all selected variations
2795
+			$variations_selected = array();
2796
+			if (!empty($wpshop_variation_selected)) {
2797
+				foreach ($wpshop_variation_selected as $selected_variation) {
2798
+					$variation_definition = explode('-_variation_val_-', $selected_variation);
2799
+					$variations_selected[$variation_definition[0]] = $variation_definition[1];
2800
+				}
2801
+			}
2802
+
2803
+			// Check variations priority
2804
+			$product_with_variation = wpshop_products::get_variation_by_priority($variations_selected, $product_id);
2805
+
2806
+			// Check if $product_with_variation have variations
2807
+			if (!empty($product_with_variation[$product_id]['variations']) || !empty($wpshop_free_variation)) {
2808
+
2809
+				$formatted_product = $wpshop_cart->prepare_product_to_add_to_cart($product_id, $product_qty, $variations_selected);
2810
+				$product_to_add_to_cart = $formatted_product[0];
2811
+				foreach ($formatted_product[0] as $pid => $product_more_content) {
2812
+					$order_items[$pid]['product_id'] = $product_more_content['id'];
2813
+
2814
+					/** For product with variation    */
2815
+					$order_items[$pid]['product_variation_type'] = !empty($product_more_content['variation_priority']) ? $product_more_content['variation_priority'] : '';
2816
+					$order_items[$pid]['free_variation'] = !empty($product_more_content['free_variation']) ? $product_more_content['free_variation'] : '';
2817
+					$order_items[$pid]['product_variation'] = '';
2818
+					if (!empty($product_more_content['variations'])) {
2819
+						foreach ($product_more_content['variations'] as $variation_id) {
2820
+							$order_items[$pid]['product_variation'][] = $variation_id;
2821
+						}
2822
+					}
2823
+				}
2824
+
2825
+				// If Product list is not empty, add products to order
2826
+				if (!empty($order_items)) {
2827
+					foreach ($order_items as $product_id => $d) {
2828
+						$product_key = $product_id;
2829
+
2830
+						// Formate datas
2831
+						$product_id = $head_product_id = $d['product_id'];
2832
+						$product_variation = !empty($d['product_variation']) ? $d['product_variation'] : null;
2833
+
2834
+						// If product is a single variation product
2835
+						if (!empty($product_variation) && (count($product_variation) == 1)) {
2836
+							$product_id = $product_variation[0];
2837
+						}
2838
+
2839
+						// Construct final product
2840
+						$product = wpshop_products::get_product_data($d['product_id'], true);
2841
+						$the_product = array_merge(array('product_id' => $d['product_id'], 'product_qty' => 1), $product);
2842
+
2843
+						//    Add variation to product into cart for storage
2844
+						if (!empty($product_variation)) {
2845
+							$the_product = wpshop_products::get_variation_price_behaviour($the_product, $product_variation, $head_product_id, array('type' => $d['product_variation_type']));
2846
+						}
2847
+
2848
+						// Free Variations Checking
2849
+						if (!empty($d['free_variation'])) {
2850
+							$the_product['item_meta']['free_variation'] = $d['free_variation'];
2851
+							$head_product_id = $the_product['product_id'];
2852
+						}
2853
+
2854
+						// If product is a variation, we check parent product general
2855
+						if (get_post_type($the_product['product_id']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
2856
+							$parent_def = wpshop_products::get_parent_variation($the_product['product_id']);
2857
+							if (!empty($parent_def) && !empty($parent_def['parent_post'])) {
2858
+								$variation_def = get_post_meta($parent_def['parent_post']->ID, '_wpshop_variation_defining', true);
2859
+								$parent_meta = $parent_def['parent_post_meta'];
2860
+								if (!empty($variation_def) && !empty($variation_def['options']) && !empty($variation_def['options']['priority']) && in_array('combined', $variation_def['options']['priority']) && !empty($variation_def['options']['price_behaviour']) && in_array('addition', $variation_def['options']['price_behaviour']) && !empty($variation_def['attributes']) && count($variation_def['attributes']) > 1) {
2861
+									$the_product['product_price'] += number_format(str_replace(',', '.', $parent_meta['product_price']), 2, '.', '');
2862
+									$the_product['price_ht'] += number_format(str_replace(',', '.', $parent_meta['price_ht']), 2, '.', '');
2863
+									$the_product['tva'] += number_format(str_replace(',', '.', $parent_meta['tva']), 2, '.', '');
2864
+								}
2865
+							}
2866
+						}
2867
+					}
2868
+				}
2869
+
2870
+				if (!empty($the_product) && empty($the_product['price_ttc_before_discount']) && empty($the_product['price_ht_before_discount'])) {
2871
+					$price_infos = wpshop_prices::check_product_price($the_product, true);
2872
+					if (!empty($price_infos['discount']['discount_exist'])) {
2873
+						$the_product['price_ttc_before_discount'] = $the_product['product_price'];
2874
+						$the_product['price_ht_before_discount'] = $the_product['price_ht'];
2875
+					}
2876
+					$the_product['price_ht'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_et_price'] : $price_infos['et'];
2877
+					$the_product['product_price'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_ati_price'] : $price_infos['ati'];
2878
+					$the_product['tva'] = (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist']) ? $price_infos['discount']['discount_tva'] : $price_infos['tva'];
2879
+				}
2880
+
2881
+				$product = wpshop_products::get_product_data($product_id, true, '"publish", "draft"');
2882
+				// Add free variations to product
2883
+				if (!empty($wpshop_free_variation)) {
2884
+					$the_product['item_meta']['free_variation'] = $wpshop_free_variation;
2885
+				}
2886
+				// Change picture if have a selected variation
2887
+				$the_selected_variation = !empty($product_with_variation) && !empty($product_with_variation[$head_product_id]) && !empty($product_with_variation[$head_product_id]['variations']) ? $product_with_variation[$head_product_id]['variations'] : null;
2888
+				$response['wps_product_image'] = $wpshop_products->wps_selected_variation_picture($head_product_id, $the_selected_variation);
2889
+
2890
+				// Price Display
2891
+				$price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
2892
+				$price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front, $product['custom_display'], 'attribute', 'product_price', 'complete_sheet');
2893
+				$productPrice = '';
2894
+				if ($price_display) {
2895
+					$response['product_price_output'] = wpshop_prices::get_product_price($the_product, 'price_display', 'complete_sheet', false, true);
2896
+				}
2897
+
2898
+				//Get Summary cart
2899
+				$response['product_output'] = $wpshop_products->wps_get_summary_variations_product($product_id, $the_product, (!empty($wpshop_variation_selected) || !empty($wpshop_free_variation) ? true : false));
2900
+				$response_status = true;
2901
+			} else {
2902
+				//Product without variations
2903
+				$product_data = wpshop_products::get_product_data($product_id);
2904
+				$response['product_price_output'] = wpshop_prices::get_product_price($product_data, 'price_display', 'complete_sheet');
2905
+			}
2906
+		}
2907
+
2908
+		return array($response_status, $response);
2909
+	}
2910
+
2911
+	/**
2912
+	 * Display information for a given value of an attribute defined as an entity, when attribute option for detail view is set as true
2913
+	 *
2914
+	 * @param array $shortcode_attribute Some parameters given by the shortcode for display
2915
+	 */
2916
+	public function wpshop_product_variation_value_detail($shortcode_attribute)
2917
+	{
2918
+		echo wpshop_display::display_template_element('wpshop_product_variation_value_detail_container', array());
2919
+	}
2920
+
2921
+	/**
2922
+	 * Build the product structure with variation for product choosed by the user into frontend sheet
2923
+	 *
2924
+	 * @param array $selected_variation THe list of variation choosed by the user in product frontend sheet
2925
+	 * @param integer $product_id The basic product choose by the user in frontend
2926
+	 *
2927
+	 * @return array The product list for adding to the cart build by variation priority
2928
+	 */
2929
+	public static function get_variation_by_priority($selected_variation, $product_id, $add_to_cart_action = false)
2930
+	{
2931
+		global $wpdb;
2932
+		$all_required_variations_selected = $no_selected_variation = true;
2933
+		$single_variations = $combined_variations = $product_to_add_to_cart = array();
2934
+
2935
+		// Check if all required variations are selected
2936
+		$required_attributes_list = wpshop_prices::check_required_attributes($product_id);
2937
+		foreach ($selected_variation as $k => $value) {
2938
+			if ($value == 0 && in_array($k, $required_attributes_list)) {
2939
+				$all_required_variations_selected = false;
2940
+			}
2941
+			if ($value != 0) {
2942
+				$no_selected_variation = false;
2943
+			}
2944
+		}
2945
+		if (!empty($selected_variation)) {
2946
+			//Check variations configuration
2947
+			$product_variation_configuration = get_post_meta($product_id, '_wpshop_variation_defining', true);
2948
+			// Check variations type
2949
+			$product_variation_type = (!empty($product_variation_configuration) && !empty($product_variation_configuration['variation_type'])) ? $product_variation_configuration['variation_type'] : 'single';
2950
+			$product_to_add_to_cart[$product_id]['variation_priority'] = $product_variation_type;
2951
+
2952
+			//Check defined variation priority
2953
+			$priority = (!empty($product_variation_configuration['options']) && !empty($product_variation_configuration['options']['priority'][0])) ? $product_variation_configuration['options']['priority'][0] : 'combined';
2954
+			$product_to_add_to_cart[$product_id]['defined_variation_priority'] = $priority;
2955
+
2956
+			// Recover all product variations
2957
+			$query_variation = $selected_variation;
2958
+			// Delete free variations
2959
+			unset($query_variation['free']);
2960
+
2961
+			if ($product_variation_type == 'single') {
2962
+				// Get single variations
2963
+				$single_variations = array();
2964
+				foreach ($selected_variation as $attribute_code => $attribute_value) {
2965
+					if (isset($attribute_value) && $attribute_code != 'free') {
2966
+						$query = $wpdb->prepare("SELECT ID FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_value = '" . serialize(array($attribute_code => $attribute_value)) . "'", $product_id);
2967
+						$single_variation_id = $wpdb->get_var($query);
2968
+						if (!empty($single_variation_id)) {
2969
+							$single_variations[] = $single_variation_id;
2970
+							unset($query_variation[$attribute_code]);
2971
+						}
2972
+					}
2973
+				}
2974
+			} else {
2975
+				// Get combined variations
2976
+				$query = $wpdb->prepare("SELECT ID FROM " . $wpdb->postmeta . " AS P_META INNER JOIN " . $wpdb->posts . " as P ON ((P.ID = P_META.post_id) AND (P.post_parent = %d)) WHERE P_META.meta_key = '_wpshop_variations_attribute_def' AND P_META.meta_value = '" . serialize($query_variation) . "'", $product_id);
2977
+				$combined_variation_id = $wpdb->get_var($query);
2978
+				if (!empty($combined_variation_id)) {
2979
+					$combined_variations[] = $combined_variation_id;
2980
+				}
2981
+
2982
+			}
2983
+
2984
+			//If all required variations are not selected
2985
+			if (!$all_required_variations_selected || $no_selected_variation) {
2986
+				$product_to_add_to_cart['text_from'] = 'on';
2987
+				// If we choose to display lower price variation combinaison
2988
+				if (empty($product_variation_configuration) || (!empty($product_variation_configuration) && empty($product_variation_configuration['options'])) || (!empty($product_variation_configuration) && !empty($product_variation_configuration['options']) && !empty($product_variation_configuration['options']['price_display']) && !empty($product_variation_configuration['options']['price_display']['lower_price']))) {
2989
+					//Get lower price variation combinaison
2990
+					$lower_price_variations = wpshop_prices::check_product_lower_price($product_id);
2991
+					if (!empty($lower_price_variations['variations']) && is_array($lower_price_variations['variations'])) {
2992
+						foreach ($lower_price_variations['variations'] as $lower_price_variation) {
2993
+							$product_to_add_to_cart[$product_id]['variations'][] = $lower_price_variation;
2994
+						}
2995
+					}
2996
+					$product_to_add_to_cart['display_lower_price'] = true;
2997
+				} else {
2998
+					$product_to_add_to_cart[$product_id]['variations'] = array();
2999
+				}
3000
+			} else {
3001
+				$product_to_add_to_cart['text_from'] = '';
3002
+				$product_to_add_to_cart[$product_id]['variations'] = (!empty($product_variation_type) && $product_variation_type == 'single') ? $single_variations : $combined_variations;
3003
+			}
3004
+		}
3005
+
3006
+		return $product_to_add_to_cart;
3007
+	}
3008
+
3009
+	public static function get_variation_price_behaviour($product_into_cart, $product_variations, $head_product_id, $variations_options)
3010
+	{
3011
+		global $wpdb;
3012
+
3013
+		if (!empty($product_variations)) {
3014
+			// Initialize variations total price datas
3015
+			$variations_total_price = array('price_et' => 0, 'vat_amount' => 0, 'price_ati' => 0);
3016
+			$variations_discount_total_price = array('price_et' => 0, 'vat_amount' => 0, 'price_ati' => 0);
3017
+			$vat_rate = 0;
3018
+			$discount_amount = $discount_rate = $special_price = 0;
3019
+			// Recover Head product metadata
3020
+			$head_product_metadata = get_post_meta($head_product_id, '_wpshop_product_metadata', true);
3021
+			$tva_rate_id = (!empty($head_product_metadata['tx_tva'])) ? $head_product_metadata['tx_tva'] : 0;
3022
+			if (!empty($tva_rate_id)) {
3023
+				// Recover VAT Rate of product
3024
+				$query = $wpdb->prepare('SELECT value FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $tva_rate_id);
3025
+				$vat_rate = $wpdb->get_var($query);
3026
+			}
3027
+			$price_piloting = get_option('wpshop_shop_price_piloting');
3028
+			foreach ($product_variations as $product_variation) {
3029
+				$variation_metadata['product_price'] = 0;
3030
+				$variation_metadata = get_post_meta($product_variation, '_wpshop_product_metadata', true);
3031
+				if (!empty($variation_metadata)) {
3032
+					$p_et = ((empty($price_piloting) || $price_piloting == 'TTC') ? ((!empty($variation_metadata['product_price']) ? $variation_metadata['product_price'] : 0) / (1 + ($vat_rate / 100))) : $variation_metadata['price_ht']);
3033
+					$p_ati = ((empty($price_piloting) || $price_piloting == 'TTC') ? (!empty($variation_metadata['product_price']) ? $variation_metadata['product_price'] : 0) : ($variation_metadata['price_ht'] * (1 + ($vat_rate / 100))));
3034
+
3035
+					$variations_total_price['price_et'] += $p_et;
3036
+					$variations_total_price['price_ati'] += $p_ati;
3037
+					$variations_total_price['vat_amount'] += $p_ati - $p_et;
3038
+
3039
+					$variation_metadata['tx_tva'] = $product_into_cart['tx_tva'] = $vat_rate;
3040
+
3041
+					// Check discount
3042
+					$discount_config = wpshop_prices::check_discount_for_product($product_variation);
3043
+					if (!empty($discount_config) && !empty($discount_config['value'])) {
3044
+						$variation_discount_prices = wpshop_prices::calcul_discounted_price($variation_metadata, $discount_config);
3045
+						$variations_discount_total_price['price_et'] += $variation_discount_prices['price_ht'];
3046
+						$variations_discount_total_price['price_ati'] += $variation_discount_prices['product_price'];
3047
+						$variations_discount_total_price['vat_amount'] += $variation_discount_prices['tva'];
3048
+
3049
+						if (!empty($discount_config['type']) && $discount_config['type'] == 'discount_amount') {
3050
+							$product_into_cart['discount_amount'] = $discount_amount + $product_into_cart['discount_amount'];
3051
+						} elseif (!empty($discount_config['type']) && $discount_config['type'] == 'discount_rate') {
3052
+							$product_into_cart['discount_rate'] = $discount_rate + $discount_config['value'];
3053
+						} elseif (!empty($discount_config['type']) && $discount_config['type'] == 'special_price') {
3054
+							$product_into_cart['special_price'] = $special_price + $discount_config['value'];
3055
+						}
3056
+					}
3057
+
3058
+					$product_variation_def = wpshop_products::get_product_data($product_variation, true, '"publish", "draft"');
3059
+					$product_into_cart['item_meta']['variations'][$product_variation] = $product_variation_def;
3060
+				}
3061
+			}
3062
+
3063
+			// Check if add or replace variation price to head product
3064
+			$product_variation_defining = get_post_meta($head_product_id, '_wpshop_variation_defining', true);
3065
+			if ((!empty($product_variation_def['price_behaviour'])) || empty($product_variation_defining) || (!empty($product_variation_defining) && empty($product_variation_defining['options'])) || ((!empty($product_variation_defining)) && !empty($product_variation_defining['options']) && !empty($product_variation_defining['options']['price_behaviour']) && !empty($product_variation_defining['options']['price_behaviour'][0]) && $product_variation_defining['options']['price_behaviour'][0] == 'replacement')) {
3066
+				//Replace the product price
3067
+				if (!empty($variations_discount_total_price) && !empty($variations_discount_total_price['price_ati'])) {
3068
+					$product_into_cart['price_ttc_before_discount'] = $variations_total_price['price_ati'];
3069
+					$product_into_cart['price_ht_before_discount'] = $variations_total_price['price_et'];
3070
+
3071
+					$product_into_cart['product_price'] = $variations_discount_total_price['price_ati'];
3072
+					$product_into_cart['price_ht'] = $variations_discount_total_price['price_et'];
3073
+					$product_into_cart['tva'] = $variations_discount_total_price['vat_amount'];
3074
+				} else {
3075
+					$product_into_cart['product_price'] = $variations_total_price['price_ati'];
3076
+					$product_into_cart['price_ht'] = $variations_total_price['price_et'];
3077
+					$product_into_cart['tva'] = $variations_total_price['vat_amount'];
3078
+				}
3079
+			} else {
3080
+				// Add variations price to product price
3081
+				if (!empty($variations_discount_total_price) && !empty($variations_discount_total_price['price_ati'])) {
3082
+					$product_into_cart['price_ttc_before_discount'] = ($product_into_cart['product_price'] + $variations_total_price['price_ati']);
3083
+					$product_into_cart['price_ht_before_discount'] = ($product_into_cart['price_ht'] + $variations_total_price['price_et']);
3084
+
3085
+					$product_into_cart['product_price'] += $variations_discount_total_price['price_ati'];
3086
+					$product_into_cart['price_ht'] += $variations_discount_total_price['price_et'];
3087
+					$product_into_cart['tva'] += $variations_discount_total_price['vat_amount'];
3088
+				} else {
3089
+					$product_into_cart['product_price'] += $variations_total_price['price_ati'];
3090
+					$product_into_cart['price_ht'] += $variations_total_price['price_et'];
3091
+					$product_into_cart['tva'] += $variations_total_price['vat_amount'];
3092
+				}
3093
+				// Check parent discount
3094
+				$parent_discount_config = wpshop_prices::check_discount_for_product($head_product_id);
3095
+				if (!empty($parent_discount_config)) {
3096
+					$product_into_cart['price_ttc_before_discount'] = (!empty($product_into_cart['price_ttc_before_discount'])) ? $product_into_cart['price_ttc_before_discount'] : $product_into_cart['product_price'];
3097
+					$product_into_cart['price_ht_before_discount'] = (!empty($product_into_cart['price_ht_before_discount'])) ? $product_into_cart['price_ht_before_discount'] : $product_into_cart['price_ht'];
3098
+					$product_into_cart = wpshop_prices::calcul_discounted_price($product_into_cart, $parent_discount_config);
3099
+
3100
+					if (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'discount_amount') {
3101
+						$product_into_cart['discount_amount'] = $discount_amount + $parent_discount_config['value'];
3102
+					} elseif (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'discount_rate') {
3103
+						$product_into_cart['discount_rate'] = $discount_rate + $parent_discount_config['value'];
3104
+					} elseif (!empty($parent_discount_config['type']) && $parent_discount_config['type'] == 'special_price') {
3105
+						$product_into_cart['special_price'] = $special_price + $parent_discount_config['value'];
3106
+					}
3107
+				}
3108
+			}
3109
+		} else {
3110
+			// If product have just Free variations
3111
+			$discount_config = wpshop_prices::check_discount_for_product($head_product_id);
3112
+			if (!empty($discount_config)) {
3113
+				$product_into_cart['price_ttc_before_discount'] = (!empty($product_into_cart['price_ttc_before_discount'])) ? $product_into_cart['price_ttc_before_discount'] : $product_into_cart['product_price'];
3114
+				$product_into_cart['price_ht_before_discount'] = (!empty($product_into_cart['price_ht_before_discount'])) ? $product_into_cart['price_ht_before_discount'] : $product_into_cart['price_ht'];
3115
+				$product_into_cart = wpshop_prices::calcul_discounted_price($product_into_cart, $discount_config);
3116
+			}
3117
+		}
3118
+
3119
+		// Text From indicator
3120
+		if (!empty($variations_options) && !empty($variations_options['text_from'])) {
3121
+			$product_into_cart['text_from'] = $variations_options['text_from'];
3122
+		}
3123
+		return $product_into_cart;
3124
+	}
3125
+
3126
+	/**
3127
+	 * Read an array with product options chosen by the customer, order into an array regarding admin definition
3128
+	 *
3129
+	 * @param array $product_definition_value The array with the selected product option to ordered
3130
+	 * @param array $output_order The good order for attribute defined by administrator
3131
+	 * @param dtring $from_page A string allowing to take a specific template regarding the current page
3132
+	 *
3133
+	 * @return array The array containing all product options ordered as the admin configure it
3134
+	 */
3135
+	public static function get_selected_variation_display($product_definition_value, $output_order, $from_page = null, $template_part = 'wpshop', $output_type = null)
3136
+	{
3137
+		$variation_attribute_ordered = array();
3138
+		$variation_attribute_ordered['prices'] = array();
3139
+		$variation_attribute_ordered['attribute_list'] = array();
3140
+
3141
+		if (!empty($product_definition_value['variation_definition']) && is_array($product_definition_value['variation_definition'])) {
3142
+			foreach ($product_definition_value['variation_definition'] as $variation_attribute_code => $variation_attribute_detail) {
3143
+				$variation_tpl_component = array();
3144
+				foreach ($variation_attribute_detail as $info_name => $info_value) {
3145
+					$variation_tpl_component['VARIATION_' . strtoupper($info_name)] = in_array($info_name, unserialize(WPSHOP_ATTRIBUTE_PRICES)) ? wpshop_display::format_field_output('wpshop_product_price', $info_value) : stripslashes($info_value);
3146
+				}
3147
+				$variation_tpl_component['VARIATION_ID'] = $variation_attribute_code;
3148
+				$variation_tpl_component['VARIATION_ATT_CODE'] = $variation_attribute_code;
3149
+				if (!empty($output_order[$variation_attribute_code])) {
3150
+					$display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3151
+					if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3152
+						$variation_attribute_ordered['attribute_list'][$output_order[$variation_attribute_code]] = $display_data;
3153
+					}
3154
+				} else {
3155
+					$display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3156
+					if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3157
+						$variation_attribute_ordered['attribute_list'][] = $display_data;
3158
+					}
3159
+				}
3160
+				unset($variation_tpl_component);
3161
+			}
3162
+		}
3163
+
3164
+		if (!empty($product_definition_value['variations']) && is_array($product_definition_value['variations'])) {
3165
+			foreach ($product_definition_value['variations'] as $variation_id => $variation_details) {
3166
+				$variation_tpl_component = array();
3167
+				foreach ($variation_details as $info_name => $info_value) {
3168
+					if ($info_name != 'item_meta') {
3169
+						$variation_tpl_component['VARIATION_DETAIL_' . strtoupper($info_name)] = in_array($info_name, unserialize(WPSHOP_ATTRIBUTE_PRICES)) ? wpshop_display::format_field_output('wpshop_product_price', $info_value) : stripslashes($info_value);
3170
+					}
3171
+				}
3172
+				foreach ($variation_details['item_meta']['variation_definition'] as $variation_attribute_code => $variation_attribute_def) {
3173
+					$variation_tpl_component['VARIATION_NAME'] = stripslashes($variation_attribute_def['NAME']);
3174
+					$variation_tpl_component['VARIATION_VALUE'] = stripslashes($variation_attribute_def['VALUE']);
3175
+					$variation_tpl_component['VARIATION_ID'] = $variation_id;
3176
+					$variation_tpl_component['VARIATION_ATT_CODE'] = $variation_attribute_code;
3177
+
3178
+					$variation_attribute_ordered['prices'][$variation_attribute_code] = $variation_tpl_component['VARIATION_DETAIL_PRODUCT_PRICE'];
3179
+				}
3180
+				if (!empty($output_order[$variation_attribute_code])) {
3181
+					$variation_attribute_ordered['attribute_list'][$output_order[$variation_attribute_code]] = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $variation_attribute_code), $template_part);
3182
+				}
3183
+				unset($variation_tpl_component);
3184
+			}
3185
+		}
3186
+
3187
+		/**    Free Variation part    */
3188
+		if (!empty($product_definition_value['free_variation']) && is_array($product_definition_value['free_variation'])) {
3189
+			foreach ($product_definition_value['free_variation'] as $build_variation_key => $build_variation) {
3190
+				if (strpos($build_variation, '-_variation_val_-')) {
3191
+					$variation_definition = explode('-_variation_val_-', $build_variation);
3192
+					$attribute_code = $variation_definition[0];
3193
+					$attribute_selected_value = $variation_definition[1];
3194
+				} else {
3195
+					$attribute_code = $build_variation_key;
3196
+					$attribute_selected_value = $build_variation;
3197
+				}
3198
+
3199
+				$free_variation_attribute_def = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
3200
+				$variation_tpl_component['VARIATION_NAME'] = stripslashes($free_variation_attribute_def->frontend_label);
3201
+				$value_to_outut = $attribute_selected_value;
3202
+				switch ($free_variation_attribute_def->data_type) {
3203
+					case 'datetime':
3204
+						$value_to_outut = mysql2date(get_option('date_format'), $attribute_selected_value, true);
3205
+						break;
3206
+				}
3207
+
3208
+				if (in_array($free_variation_attribute_def->backend_input, array('select', 'multiple-select'))) {
3209
+					switch ($free_variation_attribute_def->data_type_to_use) {
3210
+						case 'custom':
3211
+						case 'internal':
3212
+							$possible_values = wpshop_attributes::get_select_output($free_variation_attribute_def);
3213
+							$value_to_outut = $possible_values['possible_value'][$attribute_selected_value];
3214
+							break;
3215
+					}
3216
+				}
3217
+				$variation_tpl_component['VARIATION_VALUE'] = stripslashes($value_to_outut);
3218
+				$variation_tpl_component['VARIATION_ID'] = $attribute_code;
3219
+				$variation_tpl_component['VARIATION_ATT_CODE'] = $attribute_code;
3220
+				if (!empty($value_to_outut) && !empty($output_order[$free_variation_attribute_def->code])) {
3221
+					$display_data = wpshop_display::display_template_element('cart_variation_detail', $variation_tpl_component, array('page' => $from_page, 'type' => WPSHOP_DBT_ATTRIBUTE, 'id' => $attribute_code), $template_part);
3222
+					if (!in_array($display_data, $variation_attribute_ordered['attribute_list'])) {
3223
+						$variation_attribute_ordered['attribute_list'][$output_order[$free_variation_attribute_def->code]] = $display_data;
3224
+					}
3225
+				}
3226
+				unset($variation_tpl_component);
3227
+			}
3228
+		}
3229
+		return $variation_attribute_ordered;
3230
+	}
3231
+
3232
+	public function wps_selected_variation_picture($head_product_id, $variations)
3233
+	{
3234
+		$response = array();
3235
+		/** Selected Product image **/
3236
+		$post_thumbnail_id = get_post_thumbnail_id($head_product_id);
3237
+		if (!empty($post_thumbnail_id)) {
3238
+			$response['img_id'] = $post_thumbnail_id;
3239
+			$response['img'] = wp_get_attachment_image($post_thumbnail_id, 'wpshop-product-galery');
3240
+			$response['img_url'] = wp_get_attachment_url($post_thumbnail_id);
3241
+		}
3242
+
3243
+		/** check if have one variation **/
3244
+		if (!empty($variations) && count($variations) == 1) {
3245
+			$variation_attached_image_id = get_post_meta($variations[0], '_wps_variation_attached_picture', true);
3246
+			if (!empty($variation_attached_image_id)) {
3247
+				$response['img_id'] = $variation_attached_image_id;
3248
+				$response['img'] = wp_get_attachment_image($variation_attached_image_id, 'wpshop-product-galery');
3249
+				$response['img_url'] = wp_get_attachment_url($variation_attached_image_id);
3250
+			}
3251
+		}
3252
+		return $response;
3253
+	}
3254
+
3255
+	/** Add a meta box of product sales history in product administration panel **/
3256
+	public function meta_box_product_sale_informations()
3257
+	{
3258
+		global $post;
3259
+		$product_id = $post->ID;
3260
+
3261
+		$variations = self::get_variation($product_id);
3262
+
3263
+		$sales_informations = array();
3264
+		/** Query **/
3265
+		$data_to_compare = '"item_id";s:' . strlen($product_id) . ':"' . $product_id . '";';
3266
+		$query_args = array('posts_per_page' => -1, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ORDER, 'meta_query' => array(array('key' => '_order_postmeta', 'value' => $data_to_compare, 'compare' => 'LIKE')));
3267
+		$orders = new WP_Query($query_args);
3268
+		if (!empty($orders) && !empty($orders->posts)) {
3269
+			foreach ($orders->posts as $order) {
3270
+				$order_meta = get_post_meta($order->ID, '_order_postmeta', true);
3271
+				$order_info = get_post_meta($order->ID, '_order_info', true);
3272
+				$sales_informations[] = array(
3273
+					'order_key' => (!empty($order_meta) && !empty($order_meta['order_key'])) ? $order_meta['order_key'] : '',
3274
+					'order_date' => (!empty($order_meta) && !empty($order_meta['order_date'])) ? $order_meta['order_date'] : '',
3275
+					'customer_firstname' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_first_name'])) ? $order_info['billing']['address']['address_first_name'] : '',
3276
+					'customer_name' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_last_name'])) ? $order_info['billing']['address']['address_last_name'] : '',
3277
+					'customer_email' => (!empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']) && !empty($order_info['billing']['address']['address_user_email'])) ? $order_info['billing']['address']['address_user_email'] : '',
3278
+				);
3279
+			}
3280
+		}
3281
+
3282
+		/** If product has been ordered **/
3283
+		$output = '';
3284
+		if (!empty($sales_informations)) {
3285
+			$output .= '<p>' . __('This product has been ordered', 'wpshop') . ' :</p>';
3286
+			$output .= '<ul>';
3287
+			foreach ($sales_informations as $sales_information) {
3288
+				$output .= '<li>' . sprintf(__('Ordered by %s %s (%s) on %s (Order ref. : %s)', 'wpshop'), $sales_information['customer_name'], $sales_information['customer_firstname'], $sales_information['customer_email'], $sales_information['order_date'], $sales_information['order_key']) . '</li>';
3289
+			}
3290
+			$output .= '</ul>';
3291
+		} else {
3292
+			$output .= __('This product has never been ordered', 'wpshop');
3293
+		}
3294
+
3295
+		echo $output;
3296
+	}
3297
+
3298
+	public function wps_get_summary_variations_product($product_id, $the_product, $has_variation)
3299
+	{
3300
+		global $wpdb, $wpshop_payment;
3301
+		$output = '';
3302
+		$tpl_component = array();
3303
+		/**    Get attribute order for current product    */
3304
+		$product_attribute_order_detail = wpshop_attributes_set::getAttributeSetDetails(get_post_meta($product_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
3305
+		$output_order = array();
3306
+		if (count($product_attribute_order_detail) > 0) {
3307
+			foreach ($product_attribute_order_detail as $product_attr_group_id => $product_attr_group_detail) {
3308
+				foreach ($product_attr_group_detail['attribut'] as $position => $attribute_def) {
3309
+					if (!empty($attribute_def->code)) {
3310
+						$output_order[$attribute_def->code] = $position;
3311
+					}
3312
+
3313
+				}
3314
+			}
3315
+		}
3316
+		$variation_attribute_ordered = array();
3317
+
3318
+		/** Check if product is a variation and change his name **/
3319
+		$product_post_type = get_post_type($the_product['product_id']);
3320
+		if (!empty($product_post_type) && $product_post_type == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
3321
+			$parent_infos = wpshop_products::get_parent_variation($the_product['product_id']);
3322
+			$parent_post = (!empty($parent_infos) && !empty($parent_infos['parent_post'])) ? $parent_infos['parent_post'] : array();
3323
+			$the_product['product_name'] = $the_product['post_title'] = $parent_post->post_title;
3324
+		}
3325
+
3326
+		foreach ($the_product as $product_definition_key => $product_definition_value) {
3327
+			if ($product_definition_key != 'item_meta') {
3328
+				$tpl_component['PRODUCT_MAIN_INFO_' . strtoupper($product_definition_key)] = $product_definition_value;
3329
+				if (!empty($wpshop_current_for_display) && in_array($product_definition_key, unserialize(WPSHOP_ATTRIBUTE_PRICES))) {
3330
+					$tpl_component['PRODUCT_MAIN_INFO_' . strtoupper($product_definition_key)] = $product_definition_value;
3331
+				}
3332
+			} else {
3333
+				$variation_attribute_ordered = wpshop_products::get_selected_variation_display($product_definition_value, $output_order, 'selection_summary');
3334
+			}
3335
+		}
3336
+
3337
+		ksort($variation_attribute_ordered['attribute_list']);
3338
+		$tpl_component['PRODUCT_VARIATION_SUMMARY_DETAILS'] = '';
3339
+		foreach ($variation_attribute_ordered['attribute_list'] as $attribute_variation_to_output) {
3340
+			$tpl_component['PRODUCT_VARIATION_SUMMARY_DETAILS'] .= $attribute_variation_to_output;
3341
+		}
3342
+
3343
+		/**    For security get all attributes defined as user defined or used in variation in order to set default value to empty    */
3344
+		$attribute_list = wpshop_attributes::getElement('yes', "'valid'", "is_used_for_variation", true);
3345
+		if (!empty($attribute_list)) {
3346
+			foreach ($attribute_list as $attribute_def) {
3347
+				$tpl_component['VARIATION_SUMMARY_ATTRIBUTE_PER_PRICE_' . strtoupper($attribute_def->code)] = '-';
3348
+			}
3349
+		}
3350
+
3351
+		/**    Fill the array with all prices for different variations    */
3352
+		foreach ($variation_attribute_ordered['prices'] as $attribute => $prices) {
3353
+			$tpl_component['VARIATION_SUMMARY_ATTRIBUTE_PER_PRICE_' . strtoupper($attribute)] = $prices;
3354
+		}
3355
+
3356
+		$tpl_component['PRODUCT_VARIATION_SUMMARY_MORE_CONTENT'] = '';
3357
+		$query = $wpdb->prepare("SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = %s ", '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_options');
3358
+		$post_list_with_options = $wpdb->get_results($query);
3359
+		if (!empty($post_list_with_options)) {
3360
+			$additionnal_price = 0;
3361
+			foreach ($post_list_with_options as $product_info) {
3362
+				$product_meta = unserialize($product_info->meta_value);
3363
+				if (!empty($product_meta['cart']) && !empty($product_meta['cart']['auto_add']) && ($product_meta['cart']['auto_add'] == 'yes')) {
3364
+					$product = wpshop_products::get_product_data($product_info->post_id, true, '"publish", "draft"');
3365
+
3366
+					$the_product = array_merge(array(
3367
+						'product_id' => $product_info->post_id,
3368
+						'product_qty' => 1,
3369
+					), $product);
3370
+
3371
+					$additionnal_price += (!$different_currency || ($change_rate == 1)) ? $the_product['product_price'] : ($the_product['product_price'] * $change_rate);
3372
+					$tpl_component['AUTO_PRODUCT_NAME'] = $the_product['product_name'];
3373
+
3374
+					$tpl_component['AUTO_PRODUCT_PRODUCT_PRICE'] = wpshop_display::format_field_output('wpshop_product_price', (!$different_currency || ($change_rate == 1)) ? $the_product['product_price'] : ($the_product['product_price'] * $change_rate));
3375
+					$tpl_component['PRODUCT_VARIATION_SUMMARY_MORE_CONTENT'] = wpshop_display::display_template_element('wpshop_product_configuration_summary_detail_auto_product', $tpl_component);
3376
+				}
3377
+			}
3378
+		}
3379
+
3380
+		$tpl_component['PRODUCT_VARIATION_SUMMARY_GRAND_TOTAL'] = '';
3381
+		$tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT'] = '';
3382
+		if (!empty($additionnal_price)) {
3383
+			$tpl_component['SUMMARY_FINAL_RESULT_PRICE'] = wpshop_display::format_field_output('wpshop_product_price', $tpl_component['PRODUCT_MAIN_INFO_PRODUCT_PRICE'] + $additionnal_price);
3384
+			$tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT'] = ($tpl_component['PRODUCT_MAIN_INFO_PRODUCT_PRICE'] + $additionnal_price);
3385
+			$tpl_component['PRODUCT_VARIATION_SUMMARY_GRAND_TOTAL'] = wpshop_display::display_template_element('wpshop_product_configuration_summary_detail_final_result', $tpl_component);
3386
+		}
3387
+
3388
+		/**    Call informtion for partial payment    */
3389
+		$partial_payment = $wpshop_payment->partial_payment_calcul($tpl_component['SUMMARY_FINAL_RESULT_PRICE_NO_FORMAT']);
3390
+		$tpl_component['PARTIAL_PAYMENT_INFO'] = !empty($partial_payment['amount_to_pay']) ? $partial_payment['display'] : '';
3391
+
3392
+		/**    Define the current selected currency for the order summary    */
3393 3393
 //         $response['product_output'] = $has_variation ? wpshop_display::display_template_element('wpshop_product_configuration_summary_detail', $tpl_component) : '';
3394
-        $output = $has_variation ? wpshop_display::display_template_element('wpshop_product_configuration_summary_detail', $tpl_component) : '';
3395
-        return $output;
3396
-    }
3397
-
3398
-    /**
3399
-     * Get product ID if ID = "id-parent__id-variation" or "id-variation" or "id-parent"
3400
-     * @param string $ID ID product variations or not
3401
-     * @return string Return ID
3402
-     */
3403
-    public static function get_id_variation($ID)
3404
-    {
3405
-        $result = explode('__', $ID);
3406
-        return end($result);
3407
-    }
3408
-
3409
-    /**
3410
-     * Update all variations definitions who not modified
3411
-     * @param array $new_value
3412
-     * @param array $old_value
3413
-     * @return array $new_value
3414
-     */
3415
-    public static function update_wpshop_catalog_product_option($new_value, $old_value)
3416
-    {
3417
-        global $wpdb;
3418
-        $query = $wpdb->prepare('SELECT post_id, meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE %s AND meta_value LIKE %s', '_wpshop_variation_defining', '%' . serialize('follow_general_config') . '%');
3419
-        foreach ($wpdb->get_results($query) as $wpshop_variation_defining) {
3420
-            $wpshop_variation_defining_value = unserialize($wpshop_variation_defining->meta_value);
3421
-            $wpshop_variation_defining_value['options']['price_display'] = $new_value['price_display'];
3422
-            update_post_meta($wpshop_variation_defining->post_id, '_wpshop_variation_defining', $wpshop_variation_defining_value);
3423
-        }
3424
-        return $new_value;
3425
-    }
3426
-
3427
-    public static function variation_parameters_save($current_post_id, $options)
3428
-    {
3429
-        $variation_post_meta = get_post_meta($current_post_id, '_wpshop_variation_defining', true);
3430
-        $variation_post_meta['options'] = $options;
3431
-        unset($variation_post_meta['follow_general_config']);
3432
-        update_post_meta($current_post_id, '_wpshop_variation_defining', $variation_post_meta);
3433
-    }
3394
+		$output = $has_variation ? wpshop_display::display_template_element('wpshop_product_configuration_summary_detail', $tpl_component) : '';
3395
+		return $output;
3396
+	}
3397
+
3398
+	/**
3399
+	 * Get product ID if ID = "id-parent__id-variation" or "id-variation" or "id-parent"
3400
+	 * @param string $ID ID product variations or not
3401
+	 * @return string Return ID
3402
+	 */
3403
+	public static function get_id_variation($ID)
3404
+	{
3405
+		$result = explode('__', $ID);
3406
+		return end($result);
3407
+	}
3408
+
3409
+	/**
3410
+	 * Update all variations definitions who not modified
3411
+	 * @param array $new_value
3412
+	 * @param array $old_value
3413
+	 * @return array $new_value
3414
+	 */
3415
+	public static function update_wpshop_catalog_product_option($new_value, $old_value)
3416
+	{
3417
+		global $wpdb;
3418
+		$query = $wpdb->prepare('SELECT post_id, meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE %s AND meta_value LIKE %s', '_wpshop_variation_defining', '%' . serialize('follow_general_config') . '%');
3419
+		foreach ($wpdb->get_results($query) as $wpshop_variation_defining) {
3420
+			$wpshop_variation_defining_value = unserialize($wpshop_variation_defining->meta_value);
3421
+			$wpshop_variation_defining_value['options']['price_display'] = $new_value['price_display'];
3422
+			update_post_meta($wpshop_variation_defining->post_id, '_wpshop_variation_defining', $wpshop_variation_defining_value);
3423
+		}
3424
+		return $new_value;
3425
+	}
3426
+
3427
+	public static function variation_parameters_save($current_post_id, $options)
3428
+	{
3429
+		$variation_post_meta = get_post_meta($current_post_id, '_wpshop_variation_defining', true);
3430
+		$variation_post_meta['options'] = $options;
3431
+		unset($variation_post_meta['follow_general_config']);
3432
+		update_post_meta($current_post_id, '_wpshop_variation_defining', $variation_post_meta);
3433
+	}
3434 3434
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -530,12 +530,12 @@  discard block
 block discarded – undo
530 530
 
531 531
         $products = array();
532 532
         $query = "SELECT * FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code=%s";
533
-        $data = (array) $wpdb->get_row($wpdb->prepare($query, $attr_name));
533
+        $data = (array)$wpdb->get_row($wpdb->prepare($query, $attr_name));
534 534
 
535 535
         if (!empty($data)) {
536 536
             if ($data['data_type_to_use'] == 'custom') {
537 537
                 // Find which table to take
538
-                if ($data['data_type'] == 'datetime') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME;} elseif ($data['data_type'] == 'decimal') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL;} elseif ($data['data_type'] == 'integer') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER;} elseif ($data['data_type'] == 'options') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;} elseif ($data['data_type'] == 'text') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT;} elseif ($data['data_type'] == 'varchar') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR;}
538
+                if ($data['data_type'] == 'datetime') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME; } elseif ($data['data_type'] == 'decimal') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL; } elseif ($data['data_type'] == 'integer') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER; } elseif ($data['data_type'] == 'options') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS; } elseif ($data['data_type'] == 'text') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT; } elseif ($data['data_type'] == 'varchar') {$table_name = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR; }
539 539
 
540 540
                 if (isset($table_name)) {
541 541
                     // If the value is an id of a select, radio or checkbox
@@ -685,11 +685,11 @@  discard block
 block discarded – undo
685 685
         if (!empty($atts['product_type'])) {
686 686
             switch ($atts['product_type']) {
687 687
                 case 'related':
688
-                    $product_id = !empty($atts['pid']) ? (int) $atts['pid'] : get_the_ID();
688
+                    $product_id = !empty($atts['pid']) ? (int)$atts['pid'] : get_the_ID();
689 689
                     $type = !empty($atts['display_mode']) && in_array($atts['display_mode'], array('list', 'grid')) ? $atts['display_mode'] : WPSHOP_DISPLAY_LIST_TYPE;
690 690
                     $grid_element_nb_per_line = !empty($atts['grid_element_nb_per_line']) ? $atts['grid_element_nb_per_line'] : WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE;
691 691
 
692
-                    $pids = get_post_meta((int) $product_id, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
692
+                    $pids = get_post_meta((int)$product_id, WPSHOP_PRODUCT_RELATED_PRODUCTS, true);
693 693
                     if (!empty($pids) && !empty($pids[0])) {
694 694
                         $pid = implode(',', $pids);
695 695
                     }
@@ -1204,7 +1204,7 @@  discard block
 block discarded – undo
1204 1204
     {
1205 1205
         global $wpdb;
1206 1206
 
1207
-        $data_to_save = (!empty($data_to_save)) ? $data_to_save : (array) $_REQUEST;
1207
+        $data_to_save = (!empty($data_to_save)) ? $data_to_save : (array)$_REQUEST;
1208 1208
         // Apply a filter to extra actions
1209 1209
         $data_to_save = apply_filters('wps_save_product_extra_filter', $data_to_save);
1210 1210
 
@@ -1260,7 +1260,7 @@  discard block
 block discarded – undo
1260 1260
                             $attributeValue = str_replace(',', '.', $attributeValue);
1261 1261
                         }
1262 1262
                         if (($attributeType == 'integer') && !is_array($attributeValue)) {
1263
-                            $attributeValue = (int) $attributeValue;
1263
+                            $attributeValue = (int)$attributeValue;
1264 1264
                         }
1265 1265
                         $productMetaDatas[$attributeCode] = $attributeValue;
1266 1266
                     }
@@ -1310,7 +1310,7 @@  discard block
 block discarded – undo
1310 1310
                                     $attributeValue = str_replace(',', '.', $attribute->$value_key);
1311 1311
                                 }
1312 1312
                                 if (($attribute->data_type == 'integer') && !is_array($attributeValue)) {
1313
-                                    $attributeValue = (int) $attribute->$value_key;
1313
+                                    $attributeValue = (int)$attribute->$value_key;
1314 1314
                                 }
1315 1315
                                 $variation_metadata[$attribute->code] = $attribute->$value_key;
1316 1316
                             }
@@ -1323,7 +1323,7 @@  discard block
 block discarded – undo
1323 1323
                                 $attributeValue = str_replace(',', '.', $attributeValue);
1324 1324
                             }
1325 1325
                             if (($attributeType == 'integer') && !is_array($attributeValue)) {
1326
-                                $attributeValue = (int) $attributeValue;
1326
+                                $attributeValue = (int)$attributeValue;
1327 1327
                             }
1328 1328
                             $variation_metadata[$attributeCode] = $attributeValue;
1329 1329
                         }
@@ -2103,7 +2103,7 @@  discard block
 block discarded – undo
2103 2103
             }
2104 2104
 
2105 2105
             $template_part = ($variations_list && ($output_type == 'mini')) ? 'configure_product_button' : (!empty($productStock) ? 'add_to_cart_button' : 'unavailable_product_button');
2106
-						$template_part = apply_filters( 'wps_custom_add_to_cart_button', $template_part, $product_id );
2106
+						$template_part = apply_filters('wps_custom_add_to_cart_button', $template_part, $product_id);
2107 2107
 
2108 2108
             $tpl_component = array();
2109 2109
             $tpl_component['PRODUCT_ID'] = $product_id;
Please login to merge, or discard this patch.