add_action( 'woocommerce_product_options_general_product_data', 'cwpai_woo_add_custom_general_fields' );
function cwpai_woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_cwpai_woo_redirect_url',
'label' => __( 'Redirect URL', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the URL the customer will be redirected to after purchasing this product.', 'woocommerce' )
)
);
echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'cwpai_woo_add_custom_general_fields_save' );
function cwpai_woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_cwpai_woo_redirect_url'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_cwpai_woo_redirect_url', esc_attr( $woocommerce_text_field ) );
}
add_action( 'woocommerce_thankyou', 'cwpai_woo_redirectcustom');
function cwpai_woo_redirectcustom( $order_id ){
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
$redirect = get_post_meta( $product_id, '_cwpai_woo_redirect_url', true );
if ( ! empty( $redirect ) ) {
wp_redirect( $redirect );
exit;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter