如果产品出现在 WooCommerce 完成的订单中,则为某些产品 ID 自定义添加到购物车按钮

Custom add to cart button for certain product IDs if product appears in WooCommerce completed orders

我之前的问题已经得到解答,我想进一步扩展

如何为特定产品添加自定义 url(我认为产品是数字或物理类型很常见)

以下是我尝试为特定产品编写的示例代码的一部分:

// for the product ID 45 (for example) 
if( $product->id == 45 ){ 
    $add_to_cart_url = site_url('/custom-link/product-45/');
    $button_text = __('View order now', 'woocommerce');
}

我不知道如何 add/write 这个。有什么建议吗?

其实你的问题很简单,就是加个if/else条件。您可以使用 in_array() 为 1 个或多个 productID 执行 if 条件。

注意: 因为 WooCommerce 3 使用 $product->get_id() 而不是 $product->id

对于 WooCommerce 商店和档案页面:

替换

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Setting
    $button_text_view_order = __( 'View order now', 'woocommerce' );
    
    // Get view order url
    $view_order_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    
    // New link + text
    $sprintf = sprintf(
        '<a href="%s" class="%s">%s</a>',
        esc_url( $view_order_url ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        esc_html( $button_text_view_order )
    );
}

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Products IDs, several can be added, separated by a comma
    $product_ids = array( 45 );
    
    // Checks if a value exists in an array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        // Button text
        $button_text = __( 'For certain productIDs', 'woocommerce' );
        
        // Button url
        $button_url = site_url( '/custom-link/' );          
    } else {    
        // Button text - view order
        $button_text = __( 'View order now', 'woocommerce' );
        
        // Button url - get view order url
        $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    }
    
    // New link + text
    $sprintf = sprintf(
        '<a href="%s" class="%s">%s</a>',
        esc_url( $button_url ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        esc_html( $button_text )
    );
}

单个产品页面:

替换

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Remove default add to cart button and add a custom one
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    // Add action, priority 30 and pass data to add action function
    add_action( 'woocommerce_single_product_summary', function() use ( $order_id ) {
        // Setting
        $button_text_view_order = __( 'View order now', 'woocommerce' );
        
        // Get view order url
        $view_order_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        
        echo '<a href="' . $view_order_url . '" class="button">' . $button_text_view_order . '</a>';
    }, 30, 0 );
}

// When NOT empty
if ( ! empty( $order_id ) ) {
    // Remove default add to cart button and add a custom one
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    // Products IDs, several can be added, separated by a comma
    $product_ids = array( 30 );
    
    // Checks if a value exists in an array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        // Button text
        $button_text = __( 'For certain productIDs', 'woocommerce' );
        
        // Button url
        $button_url = site_url( '/custom-link/' );          
    } else {    
        // Button text - view order
        $button_text = __( 'View order now', 'woocommerce' );
        
        // Button url - get view order url
        $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
    }
    
    // Add action, priority 30 and pass data to add action function
    add_action( 'woocommerce_single_product_summary', function() use ( $button_text, $button_url ) {            
        // New custom button, adjust classes if necessary (theme related)
        echo '<a href="' . $button_url . '" class="single_add_to_cart_button button alt">' . $button_text . '</a>';
    }, 30, 0 );
}

完整的最终结果:

function get_order_id_by_product_id( $product_id ) {
    global $wpdb;
    
    // Get user ID
    $user_id = get_current_user_id();

    // Get order ID by product ID
    $order_id = $wpdb->get_var( "
        SELECT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-completed' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = '$user_id'
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value = '$product_id'
        LIMIT 1
    " );

    // Return
    return $order_id;
}

// On WooCommerce shop and archives pages
function filter_woocommerce_loop_add_to_cart_link( $sprintf, $product, $args ) {
    // Only for logged in users
    if ( ! is_user_logged_in() ) return $sprintf;
    
    // Only for single type products
    if ( ! $product->is_type( 'simple' ) ) return $sprintf;
    
    // Call fuction and get order ID
    $order_id = get_order_id_by_product_id( $product->get_id() );
    
    // When NOT empty
    if ( ! empty( $order_id ) ) {
        // Products IDs, several can be added, separated by a comma
        $product_ids = array( 30 );
        
        // Checks if a value exists in an array
        if ( in_array( $product->get_id(), $product_ids ) ) {
            // Button text
            $button_text = __( 'For certain productIDs', 'woocommerce' );
            
            // Button url
            $button_url = site_url( '/custom-link/' );          
        } else {    
            // Button text - view order
            $button_text = __( 'View order now', 'woocommerce' );
            
            // Button url - get view order url
            $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        }
        
        // New link + text
        $sprintf = sprintf(
            '<a href="%s" class="%s">%s</a>',
            esc_url( $button_url ),
            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
            esc_html( $button_text )
        );
    }
    
    return $sprintf;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_woocommerce_loop_add_to_cart_link', 10, 3 );

// On single product page, replacing the single add to cart product button by a custom button
function action_woocommerce_single_product_summary() {
    global $product;

    // Only for logged in users
    if ( ! is_user_logged_in() ) return;
    
    // Only for single type products
    if ( ! $product->is_type( 'simple' ) ) return;
    
    // Call fuction and get order ID
    $order_id = get_order_id_by_product_id( $product->get_id() );
    
    // When NOT empty
    if ( ! empty( $order_id ) ) {
        // Remove default add to cart button and add a custom one
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        
        // Products IDs, several can be added, separated by a comma
        $product_ids = array( 30 );
        
        // Checks if a value exists in an array
        if ( in_array( $product->get_id(), $product_ids ) ) {
            // Button text
            $button_text = __( 'For certain productIDs', 'woocommerce' );
            
            // Button url
            $button_url = site_url( '/custom-link/' );          
        } else {    
            // Button text - view order
            $button_text = __( 'View order now', 'woocommerce' );
            
            // Button url - get view order url
            $button_url = wc_get_endpoint_url( 'view-order', $order_id, wc_get_page_permalink( 'myaccount' ) );
        }
        
        // Add action, priority 30 and pass data to add action function
        add_action( 'woocommerce_single_product_summary', function() use ( $button_text, $button_url ) {            
            // New custom button, adjust classes if necessary (theme related)
            echo '<a href="' . $button_url . '" class="single_add_to_cart_button button alt">' . $button_text . '</a>';
        }, 30, 0 );
    }
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );