隐藏价格,“添加到购物车”按钮,并显示消息“请登录购物”

时间:2015-12-19 作者:aagjalpankaj

我的问题很简单,我只是不想让用户购物,除非用户没有登录。我还想在单个产品页面上显示特定消息“请先登录”,而不是“价格”或“添加到购物车”按钮。

我可以使用隐藏价格并添加到购物车功能。。。

if (!is_user_logged_in()) {
        //Remove single product add to cart            
        remove_action(\'woocommerce_single_product_summary\', \'woocommerce_template_single_add_to_cart\', 30);

        //Remove single product price
        remove_action(\'woocommerce_single_product_summary\', \'woocommerce_template_single_price\', 10);

        //Remove loop add to cart
        remove_action(\'woocommerce_after_shop_loop_item\', \'woocommerce_template_loop_add_to_cart\');

        //Remove loop price
        remove_action(\'woocommerce_after_shop_loop_item_title\', \'woocommerce_template_loop_price\', 10);
    }
如何在单个产品页面上显示消息“请先登录”,而不是“添加到购物车”按钮?

3 个回复
最合适的回答,由SO网友:aagjalpankaj 整理而成

这太容易了。我正在寻找这样的解决方案。。。

add_action(\'woocommerce_single_product_summary\', \'wl8ShowCustomMsgToGuest\', 10);

function wl8ShowCustomMsgToGuest(){
        //
        if (!is_user_logged_in()) {
            $wc_ac_url = get_permalink(get_option(\'woocommerce_myaccount_page_id\'));
            $msg = __(\'Please log in to see the price.\', \'wdmacspc\');
            echo "<a href=\'$wc_ac_url\' class=\'button alt wl8-custom-btn\'>$msg</a>";
        }
}

SO网友:jgraup

只需在渲染按钮的地方添加一些条件。你还需要弄清楚你在哪一页。

if($is_single_product_page) {

    if( ! is_user_logged_in()) { ?>

        <div class="message">Please log in first</div>

    <?php } else { // end is_user_logged_in/false 
    ?>

        <button class="btn">add to cart</button>

    <?php } // end is_user_logged_in/true
}

SO网友:Tony T.

我会先这样做:在你的网站管理中,进入WooCommerce>Settings>Checkout ex:WooCommerce require registration to buy

然后我会补充@jgraup的建议。

相关推荐