loggedin user bought product. the "add to cart"button text change to"view now"and view now button link changed that will open order detail in endpoint [closed]
-
problem - currently add to cart button not showing button"view order now" after order status complete in product page also in the shop archive page, so that user can download the product file by clicking on "view order now" button. which will direct to account endpoint view order detail.
-
below is steps needed to solve -
- code check the logged-in user bought product.
- the add to cart button text changed to "view order now"
- this "view order now" button and also button link changed that will open view order detail in order section in my account of the logged-in user (of bought product)so that user able to download the file there and don't worry by finding in the list of orders in my account section.
and also I tried to add step 4.- I need an option to add a custom link set by me not a customer of a specific product to send them in custom URL.
-
here is the image that I want to achieve. image
-
and here is below the incomplete code I tried to write. I took help from woocommerce account endpoint - view order. but I didn't figuredout how to write.
-
I need help this will appreciate thanks.
add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){
$bought = false;
if( is_user_logged_in() ){
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
// Going through each current customer order items
foreach($order->get_items() as $item_id => $item_values){
if($item_values['product_id'] == $product->id){
$bought = true;
break;
}
}
}
}
if($bought){
// ==> SET HERE YOUR
// CUSTOM ADD TO CART text and link
$add_to_cart_url = site_url('//*custom_link here i want set to open view order in my account/view order/{order_id} of bought product button view order now*/ /');
$button_text = __('View order now', 'woocommerce');
}
// for the product ID 45 (for example)
if( $product->id == 45 ){
$add_to_cart_url = site_url('/custom-link/product-45/');
$button_text = __('View now product 45', 'woocommerce');
}
else {
$add_to_cart_url = $product->add_to_cart_url();
$button_text = $product->add_to_cart_text();
}
$link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
esc_url( $add_to_cart_url ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->product_type ),
esc_html( $button_text )
);
return $link;
}
source https://stackoverflow.com/questions/70652515/loggedin-user-bought-product-the-add-to-cartbutton-text-change-toview-nowan
Comments
Post a Comment