Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改“;“添加到购物车”;如果用户在WooCommerce中购买了特定产品,则显示文本_Php_Wordpress_Woocommerce_Product_Hook Woocommerce - Fatal编程技术网

Php 更改“;“添加到购物车”;如果用户在WooCommerce中购买了特定产品,则显示文本

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

我想知道,如果用户购买了特定产品,是否有办法用特定产品的自定义文本和url替换woo commerce的“添加到购物车”按钮。


我希望它无处不在(在商店页面、产品页面等)

如果客户已经在单个产品页面和归档页面上购买了该产品,请尝试以下更改产品“添加到购物车”文本的方法

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文件。它应该会起作用

一些线程:


“如果用户购买了该特定产品。”是在以前的订单中还是在当前订单中?到目前为止,您尝试了什么?@7uc1f3r在以前的订单中,我只有一个功能来检查用户是否购买了产品。