这个想法是更改 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" 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;
}
目前有效:
我需要帮助:
我从 WooCommerce 帐户端点 - 查看订单获得了帮助,但我不知道如何编写。有什么建议吗?
要从“我的帐户订单”视图页面获取 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;
}
LIMIT 1
wc-completed
在 WooCommerce 商店和档案页面上,您可以使用
woocommerce_loop_add_to_cart_link
过滤器挂钩。这允许您根据某些条件重写“添加到购物车”按钮的输出。一旦我们通过自定义函数知道了订单 ID,我们将创建我们的 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" 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="single_add_to_cart_button button alt">' . $button_text_view_order . '</a>';
}, 30, 0 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
此代码适用于“单一”类型的产品,但可以进一步扩展以满足您的需求