Created
January 28, 2019 07:44
-
-
Save akshuvo/60628de6e7501ac7c861457b910b4ce9 to your computer and use it in GitHub Desktop.
Add a custom field and email it to below product name
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
<?php | |
/** | |
* Display the custom text field | |
* @since 1.0.0 | |
*/ | |
function cfwc_create_custom_field() { | |
$args = array( | |
'id' => 'custom_text_field_title', | |
'label' => __( 'Product Name In Thai', 'cfwc' ), | |
'class' => 'cfwc-custom-field', | |
'desc_tip' => false, | |
'description' => __( '', 'ctwc' ), | |
); | |
woocommerce_wp_text_input( $args ); | |
} | |
add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_field' ); | |
/** | |
* Save the custom field | |
* @since 1.0.0 | |
*/ | |
function cfwc_save_custom_field( $post_id ) { | |
$product = wc_get_product( $post_id ); | |
$title = isset( $_POST['custom_text_field_title'] ) ? $_POST['custom_text_field_title'] : ''; | |
$product->update_meta_data( 'custom_text_field_title', sanitize_text_field( $title ) ); | |
$product->save(); | |
} | |
add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' ); | |
// Setting the email_is as a global variable | |
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4); | |
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){ | |
$GLOBALS['email_id_str'] = $email->id; | |
} | |
// Displaying product description in new email notifications | |
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 4 ); | |
function product_description_in_new_email_notification( $item_id, $item, $order = null, $plain_text = false ){ | |
// Getting the email ID global variable | |
$refNameGlobalsVar = $GLOBALS; | |
$email_id = $refNameGlobalsVar['email_id_str']; | |
// If empty email ID we exit | |
if(empty($email_id)) return; | |
// Only for "New Order email notification" | |
if ( 'new_order' == $email_id ) { | |
if( version_compare( WC_VERSION, '3.0', '<' ) ) { | |
$product_id = $item['product_id']; // Get The product ID (for simple products) | |
$product = wc_get_product($item['product_id']); | |
} else { | |
$product = $item->get_product(); | |
if( $product->is_type('variation') ) { | |
$product = wc_get_product( $item->get_product_id() ); | |
} | |
} | |
// Get the Product description (WC version compatibility) | |
if ( method_exists( $item['product'], 'get_description' ) ) { | |
$thai_name = $product->get_meta( 'custom_text_field_title' ); // for WC 3.0+ (new) | |
} else { | |
$thai_name = $product->get_meta( 'custom_text_field_title' ); // for WC 2.6.x or older | |
} | |
// Display the product description | |
echo '<br><div class="product-description">Thai Name: ' . $thai_name . '</div>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment