我之前的問題已經回答了,我想進一步擴展答案
如何為特定產品添加自定義網址(產品是數字或物理型別,我認為這很常見)
這是我嘗試為特定產品撰寫的示例代碼的一部分:
// 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');
}
我不知道如何添加/撰寫這個。有什么建議嗎?
uj5u.com熱心網友回復:
實際上,您的問題很簡單,只需添加 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" >%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" >%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 . '" >' . $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 . '" >' . $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" >%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 . '" >' . $button_text . '</a>';
}, 30, 0 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411697.html
標籤:
