• Resolved Beardy

    (@ryanbeardy)


    Hi

    I’m using your Woocommerce Pay Per Post plugin with Woocommerce Vendors.

    At the moment, when a Vendor goes to select the product(s) which are required to be purchased for accessing the page… how do I restrict the products the Vendor can select, so that it is only their own products which they can select from?

    At the moment, they seem to be able to select any product from the entire website, but I want to restrict it so that they can only choose from any products they’ve added themselves.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Matt Pramschufer

    (@mattpramschufer)

    You will have to do some custom logic yourself for determining which products are available to the logged in Vendor. But you could use the filter wc_pay_per_post_get_all_products which lists back an array of products, you would then have to loop through those and remove the ones that are only available for that vendor.

    For example:

    add_filter('wc_pay_per_post_get_all_products', 'vendor_only_products');
    
    function vendor_only_products($products){
    
    	$vendor_products = array();
    	
    	foreach($products as $product){
    		$vendor_has_access = false; //Check to see if logged in user can access product
    
    		if($vendor_has_access){
    			$vendor_products[] = $product;
    		}
    		
    	}
    	
    	return $vendor_products;
    }

    Now you just need to write the logic to see if the vendor actually has access. I do not have any experience with WooVendors so I can’t help anymore with that.

    Hope that helps and gets you in the right track.

    Thread Starter Beardy

    (@ryanbeardy)

    Ah fantastic. Thank you Matt. I’ll see if I can find someone to help but that should hopefully steer me in the right direction.

    Thank you

    Plugin Author Matt Pramschufer

    (@mattpramschufer)

    Ryan,
    No problem, I was actually curious so I decided to write it myself for you. 🙂 I am only assuming that when vendors create their own products that they are the AUTHOR of the post. If they are, then this should work. If they are NOT then this won’t work. So give it a whirl! Add this into your functions.php file in your theme.

    function vendor_has_access_to_product ($product_id){
    	global $current_user;
    	wp_get_current_user();
    	$post_author_id = get_post_field( 'post_author', $product_id );
    
    	if (is_user_logged_in() && $current_user->ID == $post_author_id) {
    		return true;
    	}
    
    	return false;
    
    }
    
    add_filter('wc_pay_per_post_get_all_products', 'vendor_only_products');
    
    function vendor_only_products($products){
    
    	$vendor_products = array();
    
    	foreach($products as $product){
    		$vendor_has_access = vendor_has_access_to_product($product['ID']); // Check to see if logged in user can access product
    
    		if($vendor_has_access){
    			$vendor_products[] = $product;
    		}
    
    	}
    
    	return $vendor_products;
    }
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Restricting available products’ is closed to new replies.