我在跟踪Xatic的tutorial 帮助我向产品添加自定义数据。
我已经设法在购物车页面上显示了这些数据。但是,它不会显示在管理后端的订单页面中。
我能做些什么来解决这个问题?
这是我到目前为止得到的
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\'] );
}
最合适的回答,由SO网友:João Dias 整理而成
找到了答案
我在那里
if ( ! empty( $cart_item[\'presc_data\'] ) )
woocommerce_add_order_item_meta( $item_id, \'Olho Direito\', $cart_item[\'presc_right_sphere\'] );
我不得不改成
$data = $cart_item[\'presc_data\'];
if ( ! empty( $cart_item[\'presc_data\'] ) )
woocommerce_add_order_item_meta( $item_id, \'Olho Direito\', $data[\'presc_right_sphere\'] );