【发布时间】:2018-06-25 11:38:25
【问题描述】:
对于 Woocommerce,我找到了这段代码,但我需要以产品 ID 为条件:
add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 0);
function custom_thankyou_text(){
echo '<p class="thankyou-custom-text">If you do not see the download button(s) below, please refresh the page. Processing can take a few minutes.</p>';
}
如何根据他们购买的产品显示特定的 WooCommerce 感谢页面文本?
我还找到了适合的这个条件示例(没有重定向,我不需要):
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
if ( ! is_wc_endpoint_url( 'order-received' ) ) return;
// Define the product IDs in this array
$product_ids = array( 37, 25, 50 ); // or an empty array if not used
// Define the product categories (can be IDs, slugs or names)
$product_categories = array( 'clothing' ); // or an empty array if not used
$redirection = false;
global $wp;
$order_id = intval( str_replace( 'checkout/order-received/', '', $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;
}
}
// Make the custom redirection when a targeted product has been found in the order
if( $redirection ){
wp_redirect( home_url( '/your-page/' ) );
exit;
}
}
有没有办法将两者结合起来以获得所需的结果?
【问题讨论】:
标签: php wordpress woocommerce categories product