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

前端 未结 1 521
难免孤独
难免孤独 2021-01-23 09:17

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 Gein

相关标签:
1条回答
  • 2021-01-23 10:07

    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:

    0 讨论(0)
提交回复
热议问题