I’m following Xatic’s tutorial to help me add custom data to products.
I’ve managed to be able to have that data on the cart page. However, it doesn’t display in the order page in the admin backend.
What can I do to solve this?
Here’s what I’ve got so far
add_action( 'wp_enqueue_scripts','enqueue_wc_scripts' );
add_action('woocommerce_before_add_to_cart_button','extend_product_options');
add_filter('woocommerce_after_single_product_summary','prescription_form');
add_filter('woocommerce_add_cart_item_data', 'custom_add_item_data', 10, 2);
add_filter('woocommerce_get_cart_item_from_session','wc_get_cart_item_from_session',10,2);
add_filter('woocommerce_get_item_data', 'xatik_wc_get_item_data', 10, 2);
add_action('woocommerce_add_order_item_meta', 'xatik_wc_order_item_meta', 10, 2);
load_child_theme_textdomain('yit', get_stylesheet_directory().'/languages');
load_child_theme_textdomain('woocommerce', get_stylesheet_directory().'/languages');
function enqueue_wc_scripts(){
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri().'/js/custom.js' );
}
function extend_product_options(){
echo '<div id="prescription">';
echo '<input type="hidden" name ="presc_right_axis" value=""/>';
echo '<input type="hidden" name ="presc_right_sphere" id="rsphere" value=""/>';
echo '<input type="hidden" name ="presc_right_cilinder" value=""/>';
echo '</div>';
}
function prescription_form(){
echo '<div id="prescription_form" class="group span12">';
echo '<h3>Olho Direito</h3>
<label class="required"><em>*</em>Esfera</label>
<select name="right_eye" id="right_eye_select" class="required-entry product-custom-option validation-passed" title="" onchange="opConfig.reloadPrice()">
<option value="10">+10.00</option>
<option value="9.75">+9.75</option>
</select>
</div>';
}
function custom_add_item_data($cart_item_meta, $product_id){
//var_dump($_POST);
if(isset($_POST['presc_right_sphere'])){
$cart_item_meta['presc_data']['presc_right_sphere'] = $_POST['presc_right_sphere'];
}
return $cart_item_meta;
}
function wc_get_cart_item_from_session($cart_item, $values) {
//var_dump($values);
//var_dump($cart_item);
if (isset($values['presc_data'])) {
$cart_item['presc_data'] = $values['presc_data'];
}
return $cart_item;
}
function xatik_wc_get_item_data($other_data, $cart_item) {
if (isset($cart_item['presc_data'])) {
$data = $cart_item['presc_data'];
$other_data[] = array('name' => 'Olho Direito', 'value' => $data['presc_right_sphere']);
}
return $other_data;
}
function xatik_wc_order_item_meta($item_id, $cart_item) {
var_dump($item_id);
var_dump($cart_item);
if ( ! empty( $cart_item['presc_data'] ) )
woocommerce_add_order_item_meta( $item_id, 'Olho Direito', $cart_item['presc_right_sphere'] );
}
Figured it out
where I had
I had to change to