【问题标题】:Disable the "bid" button from WooCommerce product pages for the author从 WooCommerce 产品页面为作者禁用“出价”按钮
【发布时间】:2018-12-05 00:53:34
【问题描述】:

我想从 WooCommerce 的产品页面中为帖子的帖子作者删除/禁用/隐藏“出价”按钮。

我正在使用 WC 供应商 pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction。

请在下面找到屏幕截图的链接:

直播Link to the product

请问我该怎么做?

【问题讨论】:

  • 添加了屏幕截图。 i.imgur.com/RtMaWRX.png
  • 您好,我今天试过了,之前因为我的网站坏了,所以无法尝试。该代码运行良好,但如果产品是通过 excel 上传的,则代码有效,但如果产品通过管理面板上传并且管理员将产品分配给供应商,则供应商可以出价,但该消息似乎不允许。

标签: php wordpress button woocommerce product


【解决方案1】:

由于此按钮已由您或某些插件自定义,我不确定 100% 是否对您有效,即使它在我的测试服务器上有效。

第一个函数是一个条件函数,用于检测当前用户是否是该产品的作者(供应商)。

然后在商店和存档页面上,添加到购物车按钮被替换为产品喜欢的自定义按钮。

要在单个产品页面上完成,该按钮将替换为带有自定义文本的假按钮(此处为“不允许”)...

代码如下:

// 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;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且可以工作。你会得到这个:

还有这个:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2016-10-02
    相关资源
    最近更新 更多