Skip to content

Instantly share code, notes, and snippets.

@fervous
Last active April 18, 2019 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fervous/ffac7ed154a4287414ea2da675cbdc17 to your computer and use it in GitHub Desktop.
Save fervous/ffac7ed154a4287414ea2da675cbdc17 to your computer and use it in GitHub Desktop.
wc vendors pro new add a custom text field for message on product page
function add_custom_info_field() {
WCVendors_Pro_Form_Helper::input ( array(
'id' => 'custom_info_message',
'placeholder' => __( 'add a message ...'),
'value' => $value
)
);
}
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_info_field' );
/* Saves field data */
function save_add_custom_info_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['custom_info_message'] ) ) {
$cart_item_data[ 'custom_info_message' ] = $_REQUEST['custom_info_message'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_add_custom_info_field', 10, 2 );
/* Renders field entry on cart and checkout */
function render_mssg_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['custom_info_message'] ) ) {
$custom_items[] = array( "name" => 'Custom Info or Message', "value" => $cart_item['custom_info_message'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_mssg_meta_on_cart_and_checkout', 10, 2 );
/* Renders field info onto orders pages and emails */
function custom_info_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_info_message'] ) ) {
wc_add_order_item_meta( $item_id, "Customer Message", $values['custom_info_message'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'custom_info_order_meta_handler', 1, 3 );
@canyasa
Copy link

canyasa commented Apr 10, 2019

This breaks the "Sold Individually" function. When Sold Individually is checked customers can add more than 1 product to their carts if this function in their functions.php

@canyasa
Copy link

canyasa commented Apr 18, 2019

Could you update this function to work with the most updated version of pro please? I really need to use this with sold individually option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment