Disable the “bid” button from WooCommerce product pages for the author

与世无争的帅哥 提交于 2019-12-02 07:04:42

问题


I would like to Removal/disable/hide of "bid" button from product pages in WooCommerce for post authors of the post.

I am using WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction.

Please find the link of the screen shot below:

The Live Link to the product

How can I do it please?


回答1:


As this buttons are already customized by you or some plugins, I am not sure at 100% that it will work for you, even if it works on my test server.

The first function is a conditional function that detects for a product if the current user is the author (the vendor) of this product.

Then on shop and archives pages the add to cart button is replace by a custom button liked to the product.

To finish on single product page the button is replace by a fake button with a custom text (here "Not Allowed")

Here is the code:

// Custom conditional function (detecting the vendor of a product)
if( ! function_exists( 'is_the_vendor' ) ){
    function is_the_vendor( $product ){

        $current_user_id = get_current_user_id();

        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        // Get the product post object to get the post author
        $post_obj = get_post( $product_id );
        $post_author = $post_obj->post_author;

        if( $post_author == $current_user_id ) return true;
        else return false;
    }
}

// Shop and archives pages: we replace the button add to cart by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
function custom_text_replace_button( $button, $product  ) {

    if( is_the_vendor( $product ) ){
        $button_text = __("View product", "woocommerce");
        return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    } else {
        return $button;
    }
}
// replacing add to cart button and quantities by a custom inactive button
add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0 );
function replacing_template_single_add_to_cart() {
    global $product;

    if( is_the_vendor( $product ) ):

        // Removing add to cart button and quantities
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

        // The text replacement
        add_action( 'woocommerce_single_product_summary', function(){

            // set below your custom text
            $text = __('Not allowed', 'woocommerce');

            // Temporary style CSS
            $style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"';

            // Output your custom text
            echo '<a class="button custom-button" style="background-color: grey !important;">'.$text.'</a>';
        }, 30 );

    endif;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works. you will get this:

And this:



来源:https://stackoverflow.com/questions/45435011/disable-the-bid-button-from-woocommerce-product-pages-for-the-author

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!