php - 将所有购买重定向到自定义感谢页面 - 特定页面 ID 除外
问题描述
我有两个相互干扰的脚本。我对这个网站需要的是:所有成功的购买都应该转到页面https://aefcoaching.com/gracias-por-tu-compra,除了成功购买产品 ID1813
应该转到https://aefcoaching.com /gracias-gimnasio-mental/
/* @ Redirect to the Thank You page after successful payment in WooCommerce */
if( !function_exists('sc_custom_redirect_after_purchase') ):
function sc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty($wp->query_vars['order-received']) ) :
$order_id = absint($wp->query_vars['order-received']);
$order_key = wc_clean($_GET['key']);
$th_page_url = 'https://aefcoaching.com/gracias-por-tu-compra';
$redirect = add_query_arg(
array(
'order' => $order_id,
'key' => $order_key,
), $th_page_url);
wp_safe_redirect($redirect);
exit;
endif;
}
add_action('template_redirect', 'sc_custom_redirect_after_purchase');
endif;
/* @ Redirect Gimnasio Mental purchases to a specific Thank You Page */
function action_woocommerce_thankyou( $order_id ) {
if( ! $order_id ) {
return;
}
// Instannce of the WC_Order Object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// False
$redirection = false;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Product ID(s)
$product_ids = array( $item->get_product_id(), $item->get_variation_id() );
// Product ID in array
if ( in_array( 1813, $product_ids ) ) {
$redirection = true;
}
}
}
// Make the custom redirection when a targeted product has been found in the order
if ( $redirection ) {
wp_safe_redirect( home_url( '/gracias-gimnasio-mental/' ) );
exit;
}
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
第一段效果很好。第二个不是因为第一个。我不确定如何整合这两个而不是有两个单独的代码。你能帮助我吗?
解决方案
请通过将 sc_custom_redirect_after_purchase 函数更改为以下内容,将这两个逻辑合并为一个:
function sc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty($wp->query_vars['order-received']) ) :
$order_id = absint($wp->query_vars['order-received']);
$order_key = wc_clean($_GET['key']);
$th_page_url = 'https://aefcoaching.com/gracias-por-tu-compra';
//////////////////////
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item_key => $item ) {
// Product ID(s)
$product_ids = array( $item->get_product_id(), $item->get_variation_id() );
// Product ID in array
if ( in_array( 1813, $product_ids ) ) {
// $redirection = true;
$th_page_url = 'https://aefcoaching.com/gracias-gimnasio-mental/';
}
}
/////////////////////
$redirect = add_query_arg(
array(
'order' => $order_id,
'key' => $order_key,
), $th_page_url);
wp_safe_redirect($redirect);
exit;
endif;
}
推荐阅读
- r - 在 R 中重新编码并在组间进行比较
- flutter - PageView 内的 SingleChildScrollView
- amazon-web-services - 为什么 CloudFormation 在添加资源标签时会替换资源?
- airflow - 如何将 SQS JSON 消息字符串解析为 Airflow 电子邮件运算符 HTML 正文?
- angular - 无法在子组件上调用函数
- reporting-services - 如何在汇总功能中包含多个表 - 报告生成器
- mongodb - 在栅栏上:MongoDB 的 $graphLookup 与非图聚合以及如何最好地设置 Mongoose 模式?
- flutter - Flutter中如何将CachedNetworkImageProvider的list方法调用到carousel pro中
- javascript - 向 D3.js 中的嵌套数据添加虚拟零记录
- node.js - 关于设置变量的困惑