低於該功能,您將不得不設置你的目標產品標識或產品類別,獲得這個項目時,自定義重定向它們的順序:
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;
}
}
代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。
在WooCommerce 3上測試並工作。
您將需要一個元屬性來保存您要在購買後重定向到的自定義頁面的ID – madalinivascu
什麼對您的代碼不起作用?你有什麼要求。就目前而言,我認爲這個問題過於寬泛。 –