更改“添加到购物车"如果用户在 WooCommerce 中购买了特定产品,则为文本 [英] Change "add to cart" text if a user has bought specific products in WooCommerce
问题描述
我想知道是否有办法用特定产品的自定义文本和网址替换 woo-commerce 的添加到购物车"按钮如果用户购买了该特定产品.
而且我希望它无处不在(在商店页面、产品页面、...)
如果客户已经在单个产品页面和存档页面上购买了该产品,请尝试以下操作以更改产品添加到购物车"文本
>
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 );//归档产品页面add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2);//单品页面函数 custom_add_to_cart_text( $button_text, $product ) {$current_user = wp_get_current_user();if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {$button_text = __("自定义文本", "woocommerce");}返回 $button_text;}
代码位于活动子主题(或活动主题)的 functions.php 文件中.它应该可以工作.
<小时>现在您还可以将其限制为特定的产品 ID,例如:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 );//归档产品页面add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2);//单品页面函数 custom_loop_add_to_cart_button( $button_text, $product ) {$current_user = wp_get_current_user();//在此数组中定义您的特定产品 ID$specific_ids = array(37, 40, 53);if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() )&&in_array( $product->get_id(), $specific_ids ) ) {$button_text = __("自定义文本", "woocommerce");}返回 $button_text;}
代码位于活动子主题(或活动主题)的 functions.php 文件中.它应该可以工作.
一些相关答案:
I wanted to know if there is a way to replace the woo-commerce "add to cart" button with custom text and url for a specific product if the user has bought that specific product.
and I want it happens everywhere (on shop page,product page , ...)
Try the following that will change the product "add to cart" text if that product has already been bought by the customer on single product pages and archive pages
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Archive product pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Single product pages
function custom_add_to_cart_text( $button_text, $product ) {
$current_user = wp_get_current_user();
if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
$button_text = __("Custom text", "woocommerce");
}
return $button_text;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
Now you can also restrict that to specific product ids like:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Archive product pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Single product pages
function custom_loop_add_to_cart_button( $button_text, $product ) {
$current_user = wp_get_current_user();
// HERE define your specific product IDs in this array
$specific_ids = array(37, 40, 53);
if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() )
&& in_array( $product->get_id(), $specific_ids ) ) {
$button_text = __("Custom text", "woocommerce");
}
return $button_text;
}
Code goes in functions.php file of your active child theme (or active theme). it should work.
Some related answers threads:
- Change "add to cart" button for purchased products in Woocommerce
- Custom add to cart button, if the customer has bought previously the product
- Checking if customer has already bought something in WooCommerce
这篇关于更改“添加到购物车"如果用户在 WooCommerce 中购买了特定产品,则为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!