如果有人购买特定产品,有人知道如何将客户重定向到特定URL吗?
产品ID为24251
,我尝试使其与以下代码一起使用(但不能)
结帐页面的网址为:theladybox.dk/checkout-page
“感谢页面”的网址现在(对于所有产品都是:theladybox.dk/tak-for-dit-medlemskab
)
有人知道错误在哪里吗?
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
if ( ! is_wc_endpoint_url( 'checkout-page' ) ) return;
// Define the product IDs in this array
$product_ids = array( 24251 ); // or an empty array if not used
// Define the product categories (can be IDs, slugs or names)
$product_categories = array(24251); // or an empty array if not used
$redirection = false;
global $wp;
$order_id = intval( str_replace( 'checkout-page', '', $wp->request ) ); // Order ID
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
// Iterating through order items and finding targeted products
foreach( $order->get_items() as $item ){
if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
$redirection = true;
break;
}
}
$url = 'https://mailchi.mp/theladybox/din-ladybox-profil';
// Make the custom redirection when a targeted product has been found in the order
if( $redirection ){
wp_redirect($url);
exit;
}
}
答案 0 :(得分:0)
我会尝试使用woocommerce_payment_successful_result
过滤器来更改重定向。您仍然可以使用所拥有的代码来检测产品是否属于订单,但我可能建议您在结帐时在订单本身上保存一个meta字段,因为这样一来,在此处更容易检查。
/**
* Redirect after order paid.
*
* @param string $result - The redirect URL
* @param int $order_id
*
* @return string
*/
function kia_check_for_mnm_edit( $result, $order_id ) {
// Define the product IDs in this array
$product_ids = array( 24251 ); // or an empty array if not used
// Define the product categories (can be IDs, slugs or names)
$product_categories = array(24251); // or an empty array if not used
// Get an instance of the WC_Order Object.
$order = wc_get_order( $order_id );
// Iterating through order items and finding targeted products
foreach( $order->get_items() as $item ){
if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
$result = 'https://mailchi.mp/theladybox/din-ladybox-profil';
break;
}
}
return $result;
}
add_filter( 'woocommerce_payment_successful_result', 'kia_redirect_successful_order', 10, 2 );
*完全未经测试