-
-
Save OscarAbadFolgueira/719f22c2e8f15e9e3da45ef38b0234d8 to your computer and use it in GitHub Desktop.
[WooCommerce] How to add Custom Fields to Products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); | |
function woo_add_custom_general_fields() { | |
global $woocommerce, $post; | |
echo '<div class="options_group">'; | |
woocommerce_wp_checkbox( | |
array( | |
'id' => '_no_free_shipping_checkbox', | |
'wrapper_class' => '', | |
'label' => __('Exclude From Free Shipping', 'woocommerce' ), | |
'description' => __( 'Dis-allow Free Shipping', 'woocommerce' ) | |
) | |
); | |
woocommerce_wp_checkbox( | |
array( | |
'id' => '_discontinued_product_checkbox', | |
'wrapper_class' => '', | |
'label' => __('Discontinued Product', 'woocommerce' ), | |
'description' => __( 'No longer in Production', 'woocommerce' ) | |
) | |
); | |
woocommerce_wp_checkbox( | |
array( | |
'id' => '_custom_product', | |
'wrapper_class' => '', | |
'label' => __('Custom Order Product', 'woocommerce' ), | |
'description' => __( 'Product can be customized', 'woocommerce' ) | |
) | |
); | |
echo '</div>'; | |
} | |
// Save Fields | |
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); | |
function woo_add_custom_general_fields_save( $post_id ){ | |
// Checkbox | |
$woocommerce_checkbox = isset( $_POST['_no_free_shipping_checkbox'] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, '_no_free_shipping_checkbox', $woocommerce_checkbox ); | |
$woocommerce_product_checkbox = isset( $_POST['_discontinued_product_checkbox'] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, '_discontinued_product_checkbox', $woocommerce_product_checkbox ); | |
$woocommerce_custom_product_checkbox = isset( $_POST['_custom_product'] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, '_custom_product', $woocommerce_custom_product_checkbox ); | |
} | |
add_filter('woocommerce_is_purchasable', 'ar_custom_is_purchasable', 10, 2); | |
function ar_custom_is_purchasable( $is_purchasable, $object ) { | |
// get the product id first | |
$product_id = $object->get_id(); | |
// get the product meta data | |
$is_custom = get_post_meta($product_id, '_custom_product', true); | |
if ($is_custom == "yes"){ | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'ar_custom_product_cta', 60); | |
function ar_custom_product_cta() | |
{ | |
global $product; | |
// get the product id first | |
$product_id = $product->get_id(); | |
// get the product meta data | |
$is_custom = get_post_meta($product_id, '_custom_product', true); | |
// Show the Form if product is Custom | |
if ($is_custom == "yes"){ | |
echo '<h5> Questions? We respond in minutes! </h5>'; | |
echo do_shortcode( '[contact-form-7 id="19502" title="Custom Product Inquiry"]' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment