Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
0
Indexable
Never
add_action('template_redirect', function(){
	$store_url = get_query_var('vendorid');
	if (empty($store_url)){
		return;
	}
	$users = get_users(array(
		'meta_key'     => 'marketking_store_url',
		'meta_value'   => $store_url,
		'meta_compare' => '=',
	));

	if (!empty($users)){
		$vendor_id = $users[0]->ID;	
		
		$set = false;
		if(!isset($_COOKIE['woocommerce_recently_viewed_1'])){
			$set = true;
		} else {
			$productviewed = explode('%7C', $_COOKIE['woocommerce_recently_viewed_1']);
			if (marketking()->get_product_vendor($productviewed[0]) != $vendor_id){
				$set = true;
			}
		}
		if ($set){
			// get a random vendor product
			$products = get_posts( array( 
				'post_type' => 'product',
				'numberposts' => 5,
				'post_status'    => 'any',
				'fields'    => 'ids',
				'author'	=> $vendor_id
			));
			$setproduct = '';
			foreach ($products as $productid){
				if (get_post_meta($productid, 'marketking_is_product_standby', true) !== 'yes'){
					$setproduct = $productid;
					break;
				}
			}

			setcookie("woocommerce_recently_viewed_1", $setproduct, time()+31536000, '/'); 
			global $wp;
			wp_redirect(home_url( $wp->request ));
		}

	} 
});
Leave a Comment