Php 更改“;“添加到购物车”;如果用户在WooCommerce中购买了特定产品,则显示文本
我想知道,如果用户购买了特定产品,是否有办法用特定产品的自定义文本和url替换woo commerce的“添加到购物车”按钮。Php 更改“;“添加到购物车”;如果用户在WooCommerce中购买了特定产品,则显示文本,php,wordpress,woocommerce,product,hook-woocommerce,Php,Wordpress,Woocommerce,Product,Hook Woocommerce,我想知道,如果用户购买了特定产品,是否有办法用特定产品的自定义文本和url替换woo commerce的“添加到购物车”按钮。 我希望它无处不在(在商店页面、产品页面等)如果客户已经在单个产品页面和归档页面上购买了该产品,请尝试以下更改产品“添加到购物车”文本的方法 add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 900, 2 ); // Archive product pages ad
我希望它无处不在(在商店页面、产品页面等)如果客户已经在单个产品页面和归档页面上购买了该产品,请尝试以下更改产品“添加到购物车”文本的方法
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;
}
代码进入活动子主题(或活动主题)的functions.php文件。它应该会起作用
现在,您还可以将其限制为特定的产品ID,如:
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;
}
代码进入活动子主题(或活动主题)的functions.php文件。它应该会起作用
一些线程: