Replace add to cart button for unlogged users in WooCommerce(替换WooCommerce中未登录用户的添加到购物车按钮)
本文介绍了替换WooCommerce中未登录用户的添加到购物车按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WooCommerce中,我不想为未登录的用户显示产品添加到购物车按钮。我正在使用下面的代码更改添加到购物车按钮:
// Replace add to cart button with link for users who aren't logged in
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Logged in users see add to cart button
if( is_user_logged_in() ) return;
$button_text = __( "Sign up for pricing", "woocommerce" );
$button = '<div><a class="button" href="https://example.com/link">' . $button_text . '</a></div>';
return $button;
}
它适用于所有页面,但在单个产品页面上除外。产品页面将继续显示默认的Add to Cart按钮。我错过了什么?
推荐答案
更新:将单个产品上的按钮链接添加到我的帐户登录URL
以下代码将以更好的方式完成您想要的所有操作(替换您的代码):
// Replacing the button add to cart by a link to the product page in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Only for unlogged user
if( ! is_user_logged_in() ){
$button_text = __( "Sign up for pricing", "woocommerce" );
// $button_link = get_permalink( wc_get_page_id( 'myaccount' ) ); // Login Url
$button_link = $product->get_permalink(); // Single product Url
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// Replacing the single product button add to cart by a custom button
add_action( 'woocommerce_single_product_summary', 'disabled_single_add_to_cart_button', 1 );
function disabled_single_add_to_cart_button() {
global $product;
// Only for unlogged user
if( ! is_user_logged_in() ){
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
}
}
}
// The custom replacement button function inked to loggin page
function custom_product_button(){
$login_url = get_permalink( wc_get_page_id( 'myaccount' ) );
echo '<a class="button" href="'.$login_url.'">' . __( "Sign up for pricing", "woocommerce" ) . '</a>';
}
代码放在活动子主题(活动主题)的函数.php文件中。已测试并正常工作。
要在单个产品页面上禁用按钮,请改用:
// The custom replacement button function with a disabled button function custom_product_button(){ echo '<a class="button disabled">' . __( "Sign up for pricing", "woocommerce" ) . '</a>'; }
隐藏商店和档案页面上的价格:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
// Only for unlogged user
if( ! is_user_logged_in() )
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
要从单一产品页面中删除产品价格:
add_filter( 'woocommerce_single_product_summary', 'remove_woocommerce_single_price', 2 );
function remove_woocommerce_single_price() {
// Only for unlogged user
if( ! is_user_logged_in() )
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
// Hide variations price for variable products
add_filter( 'woocommerce_available_variation', 'hide_variations_price_html', 10, 3) ;
function hide_variations_price_html( $data, $product, $variation ) {
// Only for unlogged user
if( ! is_user_logged_in() )
$data['price_html'] = ' ';
return $data;
}
代码放在活动子主题(活动主题)的函数.php文件中。已测试并正常工作。
这篇关于替换WooCommerce中未登录用户的添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:替换WooCommerce中未登录用户的添加到购物车按钮
基础教程推荐
猜你喜欢
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01