这可以在下面的示例中轻松完成,您必须定义目标产品ID和产品类别(用于测试)。因此,此示例将显示一条自定义消息:
-
对于特定产品ID
-
针对特定产品类别
-
对于所有其他情况
代码:
add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 1);
function custom_thankyou_text( $order_id ){
// HERE Define Your product IDs below
$product_id1 = 30;
$product_id2 = 40;
// HERE Define Your product category (ID, slug or name)
$category = array('clothing');
// Get the WC_Order object (an instance)
$order = wc_get_order( $order_id );
$product_ids = array();
$has_category = false;
// Loop through the order items
foreach( $order->get_items() as $item ){
// PRODUCT ID: Store the product ID in an array
$product_ids[] = $item->get_product_id();
// PRODUCT CATEGORY
if( has_term( $category, 'product_cat', $item->get_product_id() ) )
$has_category = true;
}
// For first product ID
if( in_array( $product_id1, $product_ids ) ){
echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
}
// For Second product ID
elseif( in_array( $product_id2, $product_ids ) ){
echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
}
// For product category
elseif( $has_category ){
echo '<p class="thankyou-custom-text">Custom message for Product Category.</p>';
}
// For all other cases
else {
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>';
}
}
代码进入功能。活动子主题(或活动主题)的php文件。
测试和工作