這個想法是更改 WooCommerce 商店和存檔頁面以及單個產品頁面上的添加到購物車按鈕文本 url。
如果客戶之前購買過產品,則添加到購物車 url 應指向查看訂單詳細資訊頁面。
基于自定義添加到購物車按鈕,如果客戶之前購買了產品答案代碼,我可以撰寫以下代碼:
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" >%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;
}
目前有效的方法:
- 代碼檢查登錄用戶購買的產品。
- 在添加到購物車按鈕上的文字“現在查看訂單”變更為
我需要幫助:
- 獲取正確的 url,以便用戶能夠通過我的帳戶訂單查看頁面下載檔案,而不必擔心在我的帳戶部分的訂單串列中查找。
我從 WooCommerce 帳戶端點獲得幫助 - 查看訂單,但我不知道如何撰寫。有什么建議嗎?
uj5u.com熱心網友回復:
要從我的帳戶訂單視圖頁面獲取 url,您可以使用get_view_order_url()函式。但是,由于您隨后需要訪問該$order物件,因此我撰寫了一個自定義函式,該函式通過產品 ID 獲取訂單 ID
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;
}
- 此功能的優點是您不必處理所有訂單,因此更輕更快
- 此函式假設產品在每個客戶的訂單中僅出現 1 次,因此也使用
LIMIT 1 - 適用于有狀態的訂單
wc-completed
在 WooCommerce 商店和檔案頁面上,您可以使用woocommerce_loop_add_to_cart_link過濾器掛鉤。這允許您根據特定條件重寫添加到購物車按鈕的輸出。一旦我們通過自定義函式知道訂單 ID,我們將創建我們的 URL,該 URL 將參考我的帳戶訂單視圖頁面
// 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" >%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 . '" >' . $button_text_view_order . '</a>';
}, 30, 0 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
此代碼適用于“單一”型別的產品,但可以進一步擴展以滿足您的需求
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411699.html
標籤:
