- This is an extension question of this question :
- need to solve:
how can I add a custom url for a specific product in this below answered code Change add to cart button text + url to My account Order view page when customer has previously bought the product:
(product is digital or physical type it’s common I think).
- here is part of the example code I tried to write for a specific product:
// 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’); }
I didn’t figured out how to write add . Any advice?
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 ) ) {
// 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 )
);
}
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 );
// 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 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );