ﺖﺳﺍ ﻩﺪﺷ ﻩﺪﻫﺎﺸﻣ ﺍًﺮﯿﺧﺍ WooCommerce ﺕﻻﻮﺼﺤﻣ ﻩﺎﺗﻮﮐ ﺪﮐ


.ﺪﯿﻫﺩ ﺭﺍﺮﻗ ﺩﻮﺧ ﺖﯾﺎﺳ ﺏﻭ ﯼﺎﺟ ﻪﻤﻫ ﺭﺩ ﺍﺭ ﻥﺁ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺍﺮﯾﺯ ﺖﺳﺍ ﯽﻟﺎﻋ ﺭﺎﯿﺴﺑ ﻩﺪﺷ ﻩﺪﻫﺎ

.ﻢﻫﺩ ﻪﺋﺍﺭﺍ ﺎﻤﺷ ﻪﺑ ﺪﮐ ﺭﺩ ﺎًﻤﯿﻘﺘﺴﻣ ﺕﺍﺮﻈﻧ ﺎﺑ ﺍﺭ ﻞﻣﺎﮐ ﺪﮐ ﺲﭙﺳ ﻭ ﻢﻫﺩ ﺢﯿﺿﻮﺗ ﺍﺭ ﺪﻨﯾﺁﺮﻓ

ﻩﺎﺗﻮﮐ ﺪﮐ ﮏﯾ ﺭﺩ ﻥﺁ ﻡﺎﺠﻧﺍ

.ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ WooCommerce ﯼﺎﻫ ﻞﯾﺎﻓ ﺪﯾﺎﺒﻧ ﺰﮔﺮﻫ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ ﺮﮕﯾﺩ ﻢﻬﻣ ﺭﺎﯿﺴﺑ ﻪﺘﮑﻧ

!ﻡﺪﯿﻣ ﻡﺎﺠﻧﺍ ﺎﻤﺘﺣ ﻦﻣ ؟ﺪﯾﺭﺍﺩ ﺖﺳﻭﺩ ﺍﺭ ﺎﻫ ﯽﮐﻮﮐ ﺎﯾﺁ

.ﻢﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ «meta_query» ﻮﺟ ﻭ ﺱﺮﭘ ﯽﮔﮋﯾﻭ ﺭﺩ $woocomm

ﻪﻧﻭﺰﻓﺍ ﻞﻣﺎﮐ ﺪﮐ

!ﺪﮐ ﺯﺍ ﯽﺸﺨﺑ .ﻢﻫﺩ ﺢﯿﺿﻮﺗ ﺎﻤﺷ ﯼﺍﺮﺑ ﺍﺭ ﻡﺍﺪﮐ ﺮﻫ ﻪﮐ ﻡﻮﺷ ﯽﻣ ﻝﺎﺤﺷﻮﺧ ﻦﻣ ﻭ ﺪﯿﺴﯾﻮﻨﺑ ﺍﺭ ﺩﻮ

<?php
/*
Plugin Name: WooCommerce - Recently Viewed Products
Plugin URL: http://remicorson.com/
Description: Adds a "recently viewed products" shortcode
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_wc_rvp
Domain Path: languages
*/

/**
 * Register the [woocommerce_recently_viewed_products per_page="5"] shortcode
 *
 * This shortcode displays recently viewed products using WooCommerce default cookie
 * It only has one parameter "per_page" to choose number of items to show
 *
 * @access      public
 * @since       1.0 
 * @return      $content
*/
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {

	// Get shortcode parameters
	extract(shortcode_atts(array(
		"per_page" => '5'
	), $atts));

	// Get WooCommerce Global
	global $woocommerce;

	// Get recently viewed product cookies data
	$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
	$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );

	// If no data, quit
	if ( empty( $viewed_products ) )
		return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );

	// Create the object
	ob_start();

	// Get products per page
	if( !isset( $per_page ) ? $number = 5 : $number = $per_page )

	// Create query arguments array
    $query_args = array(
    				'posts_per_page' => $number, 
    				'no_found_rows'  => 1, 
    				'post_status'    => 'publish', 
    				'post_type'      => 'product', 
    				'post__in'       => $viewed_products, 
    				'orderby'        => 'rand'
    				);

	// Add meta_query to query args
	$query_args['meta_query'] = array();

    // Check products stock status
    $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

	// Create a new query
	$r = new WP_Query($query_args);

	// If query return results
	if ( $r->have_posts() ) {

		$content = '<ul class="rc_wc_rvp_product_list_widget">';

		// Start the loop
		while ( $r->have_posts()) {
			$r->the_post();
			global $product;

			$content .= '<li>
				<a href="' . get_permalink() . '">
					' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
				</a> ' . $product->get_price_html() . '
			</li>';
		}

		$content .= '</ul>';

	}

	// Get clean object
	$content .= ob_get_clean();
	
	// Return whole content
	return $content;
}

// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products");