jigoshop /shortcodes/init.php

Language PHP Lines 438
MD5 Hash 4851d3385b19320e627f1c4bc69d1f1f
Repository https://github.com/ThemesWpFr/jigoshop.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
 * Jigoshop shortcodes
 *
 * DISCLAIMER
 *
 * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
 * versions in the future. If you wish to customise Jigoshop core for your needs,
 * please use our GitHub repository to publish essential changes for consideration.
 *
 * @package             Jigoshop
 * @category            Core
 * @author              Jigowatt
 * @copyright           Copyright Š 2011-2012 Jigowatt Ltd.
 * @license             http://jigoshop.com/license/commercial-edition
 */
include_once('cart.php');
include_once('checkout.php');
include_once('my_account.php');
include_once('order_tracking.php');
include_once('pay.php');
include_once('thankyou.php');

function jigoshop_shortcode_wrapper( $function, $atts = array() ) {
	// WordPress caching of shortcodes stripped out in version 1.4.9 for compatibility with Cache plugins on Cart and Checkout
	ob_start();
	call_user_func( $function, $atts );
	return ob_get_clean();
}

//### Recent Products #########################################################

function jigoshop_recent_products( $atts ) {

	global $columns, $per_page, $paged;
    $jigoshop_options = Jigoshop_Base::get_options();

	extract( shortcode_atts( array(
		'per_page' 	=> $jigoshop_options->get_option('jigoshop_catalog_per_page'),
		'columns' 	=> $jigoshop_options->get_option('jigoshop_catalog_columns'),
		'orderby'	=> 'date',
		'order'		=> 'desc',
		'pagination'=> false
	), $atts));

	$args = array(
		'post_type'          => 'product',
		'post_status'        => 'publish',
		'ignore_sticky_posts'=> 1,
		'posts_per_page'     => $per_page,
		'orderby'            => $orderby,
		'order'              => $order,
		'paged'              => $paged,
		'meta_query'         => array(
			array(
				'key'    => 'visibility',
				'value'  => array( 'catalog', 'visible' ),
				'compare'=> 'IN'
			)
		)
	);

	query_posts( $args );
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	if($pagination) do_action('jigoshop_pagination');
	wp_reset_query();

	return ob_get_clean();
}

//### Multiple Products #########################################################

function jigoshop_products( $atts ){
	global $columns, $paged;
	$jigoshop_options = Jigoshop_Base::get_options();

	if ( empty( $atts )) return;

	extract( shortcode_atts( array(
		'per_page' 	=> $jigoshop_options->get_option('jigoshop_catalog_per_page'),
		'columns' 	=> $jigoshop_options->get_option('jigoshop_catalog_columns'),
		'orderby'	=> $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
		'order'		=> $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
		'pagination'=> false
	), $atts));

	$args = array(
		'post_type'          => 'product',
		'post_status'        => 'publish',
		'ignore_sticky_posts'=> 1,
		'orderby'            => $orderby,
		'order'              => $order,
		'paged'              => $paged,
		'meta_query'         => array(
			array(
				'key'    => 'visibility',
				'value'  => array( 'catalog', 'visible' ),
				'compare'=> 'IN'
			)
		)
	);

	if ( isset( $atts['skus'] )){
		$skus = explode( ',', $atts['skus'] );
		array_walk( $skus, create_function('&$val', '$val = trim($val);') );
		$args['meta_query'][] = array(
			'key' => 'sku',
			'value' => $skus,
			'compare' => 'IN'
		);
	}

	if ( isset( $atts['ids'] )){
		$ids = explode( ',', $atts['ids'] );
		array_walk( $ids, create_function('&$val', '$val = trim($val);') );
		$args['post__in'] = $ids;
	}

	query_posts( $args );
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	if($pagination) do_action('jigoshop_pagination');
	wp_reset_query();

	return ob_get_clean();
}

//### Single Product ############################################################

function jigoshop_product( $atts ){

	if ( empty( $atts )) return;

	$args = array(
		'post_type'     => 'product',
		'posts_per_page'=> 1,
		'post_status'   => 'publish',
		'meta_query'    => array(
			array(
				'key'    => 'visibility',
				'value'  => array( 'catalog', 'visible' ),
				'compare'=> 'IN'
			)
		)
	);

	if ( isset( $atts['sku'] )){
		$args['meta_query'][] = array(
			'key'    => 'sku',
			'value'  => $atts['sku'],
			'compare'=> '='
		);
	}

	if ( isset( $atts['id'] )){
		$args['p'] = $atts['id'];
	}

	query_posts( $args );
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	wp_reset_query();

	return ob_get_clean();
}

//### Featured Products #########################################################

function jigoshop_featured_products( $atts ) {

	global $columns, $per_page, $paged;
	$jigoshop_options = Jigoshop_Base::get_options();

	extract( shortcode_atts( array(
		'per_page' 	=> $jigoshop_options->get_option('jigoshop_catalog_per_page'),
		'columns' 	=> $jigoshop_options->get_option('jigoshop_catalog_columns'),
		'orderby'	=> $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
		'order'		=> $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
		'pagination'=> false
	), $atts));

	$args = array(
		'post_type'          => 'product',
		'post_status'        => 'publish',
		'ignore_sticky_posts'=> 1,
		'posts_per_page'     => $per_page,
		'orderby'            => $orderby,
		'order'              => $order,
		'paged'              => $paged,
		'meta_query'         => array(
			array(
				'key'    => 'visibility',
				'value'  => array( 'catalog', 'visible' ),
				'compare'=> 'IN'
			),
			array(
				'key'   => 'featured',
				'value' => true
			)
		)
	);

	query_posts( $args );
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	if($pagination) do_action('jigoshop_pagination');
	wp_reset_query();

	return ob_get_clean();
}

//### Category #########################################################

function jigoshop_product_category( $atts ) {

	global $columns, $per_page, $paged;
    $jigoshop_options = Jigoshop_Base::get_options();

	if ( empty( $atts ) ) return;

	extract( shortcode_atts( array(
		'slug'            => '',
		'per_page'        => $jigoshop_options->get_option('jigoshop_catalog_per_page'),
		'columns' 	      => $jigoshop_options->get_option('jigoshop_catalog_columns'),
		'orderby'	      => $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
		'order'		      => $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
		'pagination'      => false,
		'tax_operator'    => 'IN'
	), $atts));

	if ( ! $slug ) return;

	/** Operator validation. */
	if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
		$tax_operator = 'IN';

	/** Multiple category values. */
	if ( !empty($slug) ) {
		$slug = explode( ',', esc_attr( $slug ) );
		$slug = array_map('trim', $slug);
	}

	$args = array(
		'post_type'              => 'product',
		'post_status'            => 'publish',
		'ignore_sticky_posts'    => 1,
		'posts_per_page'         => $per_page,
		'orderby'                => $orderby,
		'order'                  => $order,
		'paged'                  => $paged,
		'meta_query'             => array(
			array(
				'key'       => 'visibility',
				'value'     => array( 'catalog', 'visible' ),
				'compare'   => 'IN'
			)
		),
		'tax_query' => array(
			array(
				'taxonomy'    => 'product_cat',
				'field'       => 'slug',
				'terms'       => $slug,
				'operator'    => $tax_operator
			)
		)
	);

	query_posts( $args );
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	if($pagination) do_action('jigoshop_pagination');
	wp_reset_query();
	return ob_get_clean();
}

//### Add to cart URL for single product #########################################################

function jigoshop_product_add_to_cart_url( $atts ) {

	if ( empty( $atts ) ) return;

	global $wpdb;

	if ($atts['id']) :
		$product_meta = get_post( $atts['id'] );
	elseif ($atts['sku']) :
		$product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='sku' AND meta_value=%s LIMIT 1", $atts['sku']));
		$product_meta = get_post( $product_id );
	else :
		return;
	endif;

	if ($product_meta->post_type!=='product') return;

	$_product = new jigoshop_product( $product_meta->ID );

	return esc_url( $_product->add_to_cart_url() );
}

//### Cart button + optional price for single product #########################################################

function jigoshop_product_add_to_cart( $atts ) {

	if (empty($atts)) return;

	global $wpdb;

	if (!$atts['class']) $atts['class'] = 'product';

	if ($atts['id']) :
		$product_meta = get_post( $atts['id'] );
	elseif ($atts['sku']) :
		$product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='sku' AND meta_value=%s LIMIT 1", $atts['sku']));
		$product_meta = get_post( $product_id );
	else :
		return;
	endif;

	if ($product_meta->post_type!=='product') return;

	$_product = new jigoshop_product( $product_meta->ID );

	if (!$_product->is_visible()) return;

	ob_start();
	?>
	<p class="<?php echo esc_attr( $atts['class'] ); ?>">

		<?php if ($atts['price'] != 'no') echo $_product->get_price_html(); ?>

		<?php jigoshop_template_loop_add_to_cart( $product_meta, $_product ); ?>

	</p><?php

	return ob_get_clean();
}

//### Search shortcode #########################################################

function jigoshop_search_shortcode( $atts ) {

	// Extract the arguments
	extract( $args );

	// Construct the form
	$form = '<form role="search" method="get" id="searchform" action="' . home_url() . '">';
	$form .= '<div>';
		$form .= '<label class="assistive-text" for="s">' . __('Search for:', 'jigoshop') . '</label>';
		$form .= '<input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __('Search for products', 'jigoshop') . '" />';
		$form .= '<input type="submit" id="searchsubmit" value="' . __('Search', 'jigoshop') . '" />';
		$form .= '<input type="hidden" name="post_type" value="product" />';
	$form .= '</div>';
	$form .= '</form>';

	// Apply a filter to allow for additional fields
	echo apply_filters('jigoshop_product_search_shortcode', $form, $instance);

}

//### Sale products shortcode #########################################################

function jigoshop_sale_products( $atts ) {
	global $columns, $per_page, $paged;

	extract(shortcode_atts(array(
		'per_page'                  => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_per_page'),
		'columns'                   => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_columns'),
		'orderby'                   => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_sort_orderby'),
		'order'                     => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_sort_direction'),
		'pagination'                => false
	), $atts));

  	$today = strtotime(date('Y-m-d',mktime(0, 0, 0, date("m"), date("d"), date("Y")) ));
  	$tomorrow = strtotime(date('Y-m-d',mktime(0, 0, 0, date("m"), date("d")+1, date("Y")) ));

	$args = array(
		'post_type'                 => array( 'product' ),
		'post_status'               => 'publish',
		'ignore_sticky_posts'       => 1,
		'posts_per_page'            => $per_page,
		'orderby'                   => $orderby,
		'order'                     => $order,
		'paged'                     => $paged,
		'meta_query'                => array(
				array(
						'key'       => 'visibility',
						'value'     => array( 'catalog', 'visible' ),
						'compare'   => 'IN'
				),
				array(
						'key'       => 'sale_price',
						'value'     => '',
						'compare'   => '!=',
				),
				array(
						'key'       => 'sale_price_dates_from',
						'value'     => $today,
						'compare'   => '<=',
				),
				array(
						'key'       => 'sale_price_dates_to',
						'value'     => $tomorrow,
						'compare'   => '<=',
				),
		)
	);
	query_posts($args);
	ob_start();
	jigoshop_get_template_part( 'loop', 'shop' );
	if ( $pagination ) do_action( 'jigoshop_pagination' );
	wp_reset_query();
	return ob_get_clean();
}
add_shortcode('sale_products', 'jigoshop_sale_products');

//### Shortcodes #########################################################

add_shortcode('product'                 , 'jigoshop_product');
add_shortcode('products'                , 'jigoshop_products');
add_shortcode('add_to_cart'             , 'jigoshop_product_add_to_cart');
add_shortcode('add_to_cart_url'         , 'jigoshop_product_add_to_cart_url');
add_shortcode('product_search'          , 'jigoshop_search_shortcode');

add_shortcode('recent_products'         , 'jigoshop_recent_products');
add_shortcode('featured_products'       , 'jigoshop_featured_products');
add_shortcode('jigoshop_category'       , 'jigoshop_product_category');

add_shortcode('jigoshop_cart'           , 'get_jigoshop_cart');
add_shortcode('jigoshop_checkout'       , 'get_jigoshop_checkout');
add_shortcode('jigoshop_order_tracking' , 'get_jigoshop_order_tracking');
add_shortcode('jigoshop_my_account'     , 'get_jigoshop_my_account');
add_shortcode('jigoshop_edit_address'   , 'get_jigoshop_edit_address');
add_shortcode('jigoshop_change_password', 'get_jigoshop_change_password');
add_shortcode('jigoshop_view_order'     , 'get_jigoshop_view_order');
add_shortcode('jigoshop_pay'            , 'get_jigoshop_pay');
add_shortcode('jigoshop_thankyou'       , 'get_jigoshop_thankyou');
Back to Top