newlogowp512

World of WP is for sale!
Interested? Please contact us here.

Add External URL to Product Variations for WooCommerce

Author:

Date:

This code adds a custom field to each variation enabling the add to cart button to link to any URL on variation selection and also change the button text to Buy Now.

// 1. Add External URL field to each variation in admin add_action(‘woocommerce_product_after_variable_attributes’, ‘add_external_url_field_to_variations’, 10, 3); function add_external_url_field_to_variations($loop, $variation_data, $variation) { woocommerce_wp_text_input([ ‘id’ => “_external_url[$loop]”, ‘label’ => ‘External URL’, ‘desc_tip’ => true, ‘description’ => ‘Enter the external URL for this variation’, ‘value’ => get_post_meta($variation->ID, ‘_external_url’, true), ]); } // 2. Save the External URL field value add_action(‘woocommerce_save_product_variation’, ‘save_external_url_variation_field’, 10, 2); function save_external_url_variation_field($variation_id, $i) { if (isset($_POST[‘_external_url’][$i])) { update_post_meta($variation_id, ‘_external_url’, esc_url_raw($_POST[‘_external_url’][$i]));

This is the first part of the article

Add External URL to Product Variations for WooCommerce

wpforms banner