• Resolved Dani

    (@danicasati)


    Hi.
    I’d like to display specific text to vendor in single product.
    When logged vendor open a single product, is it possible to determine if this product is owned by him?

    So, if single product is owned by logged vendor, then display text.
    If not, esc to function.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Support Alessio Torrisi

    (@alessio91)

    Hi there,
    with following code you get the vendor for the product.

    $vendor = yith_get_vendor( $product_id, 'product' );

    Then you can get the user id associated to the vendor in this way.

    $vendor_user_id = $vendor->get_owner();

    At the end you have to compare the vendor user id with the current user id.

    if( get_current_user_id() === $vendor_user_id  ){
     // DO CODE
    }

    I hope be helpful for you.

    Thread Starter Dani

    (@danicasati)

    It seems that is not working.
    Using this function the text doesn’t appear:

    function add_content_after_addtocart() {
    $vendor = yith_get_vendor( $product_id, 'product' );
    $vendor_user_id = $vendor->get_owner();
    
        if( get_current_user_id() === $vendor_user_id  ){
            echo 'Something';
        }
    }
    add_action( 'woocommerce_product_meta_start', 'add_content_after_addtocart' );
    Plugin Support Alessio Torrisi

    (@alessio91)

    Hi there,
    your code can’t work because you didn’t get the product id.
    Please use this code, instead.

    function add_content_after_addtocart(){	
    	global $product;
    	$vendor = yith_get_vendor( $product->get_id(), 'product' );
    	
    	$vendor_user_id = $vendor->get_owner();
    
    	if (get_current_user_id() == $vendor_user_id) {
    		echo 'Something';
    	}
    }
    
    add_action('woocommerce_product_meta_start', 'add_content_after_addtocart');
    Thread Starter Dani

    (@danicasati)

    It doesn’t work.

    Plugin Support Alessio Torrisi

    (@alessio91)

    Hi there,
    it’s so strange, I tried it in my installation and it worked. I was able to see the text only as vendor of the product that I saw.
    No issue in the code.

    Thread Starter Dani

    (@danicasati)

    Ok, I changed order of the function and now it’s working.

    Just a question: Is it possibile to add admin in the function? So, if user is vendor OR admin, then do something.

    Plugin Support Giuseppe Madaudo

    (@askmagic)

    Hi there,
    could you explain me your idea? what do you need to add the admin for in this case?

    Thread Starter Dani

    (@danicasati)

    Admin could see ALL actions by vendors.

    Plugin Support Andrea Grillo

    (@agengineering)

    Hi Dani,

    I hopw o understand your idea. Try to replace the previous code with this instead:

    function add_content_after_addtocart(){
    	global $product;
    	$vendor = yith_get_vendor( $product->get_id(), 'product' );
    
    	$vendor_user_id = $vendor->get_owner();
    
    	if (get_current_user_id() == $vendor_user_id || current_user_can( 'administrator' ) ) {
    		echo 'Something';
    	}
    }
    
    add_action('woocommerce_product_meta_start', 'add_content_after_addtocart');

    Let me know if works.

    Thread Starter Dani

    (@danicasati)

    It works.
    Thank you for support.

    Plugin Support Andrea Grillo

    (@agengineering)

    Thanks to you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Check if product is owned by logged vendor’ is closed to new replies.