【问题标题】:Woocommerce custom redirection based on purchased product after payment付款后基于购买产品的 Woocommerce 自定义重定向
【发布时间】:2020-07-29 09:20:02
【问题描述】:
我希望将用户重定向到因产品而异的特定页面。因此,一旦用户完成对产品 A 的付款,他将被重定向到链接 A ,这是一个外部链接。
如果他购买了产品 B,他将被重定向到产品 B。
如果没有,至少我想在用户完成付款后根据产品在结帐页面上显示动态 URL。
此功能的任何输入?
我在 woo commerce 中尝试了附属链接/虚拟产品,但它是另一回事 ..
【问题讨论】:
标签:
php
wordpress
redirect
woocommerce
orders
【解决方案1】:
我使用下面的代码创建了一个简单的插件来完成类似的事情......
/*
插件名称:每个产品的 Woocommerce 自定义感谢页面
描述:此插件将允许您为 woocommerce 的每个产品设置自定义感谢页面。
作者:加布里埃尔·科利农
*/
add_action( 'woocommerce_thankyou', 'redirect_product_based', 1 );
function redirect_product_based ( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
// Add whatever product id you want below here
if ( $item['product_id'] == 181 ) {
// URL
wp_redirect( 'https://www....' );
} else {
// The other URL
wp_redirect( 'https://www....' );
}
}
}
【解决方案2】:
一旦客户完成付款并且订单处于“处理中”或“已完成”状态,以下内容将根据订单项目(产品 ID)重定向到具体网址:
add_action( 'template_redirect', 'thankyou_custom_redirection', 10, 1 );
function thankyou_custom_redirection( $order_id ){
global $wp;
$order_id = get_query_var('order-received');
if( is_wc_endpoint_url('order-received') && ! empty($order_id) && $order_id > 0 ){
// Get the WC_Order Object instance
$order = wc_get_order( absint($order_id) );
// Allowed order statuses
$order_statuses = array('processing', 'completed');
if ( is_a($order, 'WC_Order') && in_array($order->get_status(), $order_statuses) ) {
$settings = product_ids_for_links_settings(); // Load settings
// Loop through order items
foreach( $order->get_items() as $item ) {
$product = $item->get_product();
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
print_pr($product_id);
if( in_array( $product_id, array_keys($settings) ) ){
$link_redirect = esc_url($settings[$product_id]);
break;
}
}
}
if( isset($link_redirect) && ! empty($link_redirect) ) {
// (optional) passing variables to URL
$link_redirect .= '?order_id='.$order_id.'&product_id='.$product_id;
// Redirection
wp_redirect( $link_redirect );
exit();
}
}
}
代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
相关:Redirect automatically from Woocommerce thankyou to an external link passing variables
【解决方案3】:
你的算法:
- 从您的订单中获取 ID 产品。
- 获取永久链接查找产品。
- 使用 wp_redirect(永久链接);