【问题标题】:Change "add to cart" text if a user has bought specific products in WooCommerce如果用户在 WooCommerce 中购买了特定产品,请更改“添加到购物车”文本
【发布时间】:2020-05-02 08:42:47
【问题描述】:

我想知道是否有一种方法可以将 woo-commerce 的“添加到购物车”按钮替换为特定产品的自定义文本和网址如果用户购买了该特定产品。

我希望它发生在任何地方(在商店页面、产品页面……)

【问题讨论】:

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

标签: php wordpress woocommerce product hook-woocommerce


【解决方案1】:

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

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 文件中。它应该工作。

一些related answers 线程:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2018-06-29
    • 2020-04-28
    • 2019-08-04
    • 2014-11-21
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多