我在Woocommerce中为我的产品创建了一些自定义字段。对于单个产品部件,一切正常。
但是对于可变产品:元信息在产品页面上可见,然后在购物车,结帐和电子邮件中消失。如果有人可以检查我的代码,我将不胜感激:)。
非常感谢。
/**
Display info
**/
// Admin: Add custom field
add_action('woocommerce_product_options_sku', 'vp_add_pp_info' );
function vp_add_pp_info(){
woocommerce_wp_text_input( array(
'id' => '_venue',
'label' => __('Venue', 'woocommerce' ),
'placeholder' => __('Enter Venue here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the Venue of the product.', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => '_date',
'label' => __('Date', 'woocommerce' ),
'placeholder' => __('Enter Date here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the Date of the product.', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => '_time',
'label' => __('Time', 'woocommerce' ),
'placeholder' => __('Enter Time here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the time of the product.', 'woocommerce' ),
) );
}
// Admin: Save custom field value for simple product inventory options
add_action('woocommerce_admin_process_product_object', 'vp_product_save_pp_info', 10, 1 );
function vp_product_save_pp_info( $product ){
if( isset($_POST['_venue']) )
$product->update_meta_data( '_venue', sanitize_text_field($_POST['_venue']) );
if( isset($_POST['_date']) )
$product->update_meta_data( '_date', sanitize_text_field($_POST['_date']) );
if( isset($_POST['_time']) )
$product->update_meta_data( '_time', sanitize_text_field($_POST['_time']) );
}
// Frontend: Display PP Info on product
add_action( 'woocommerce_single_product_summary', 'vp_product_display_pp_info' );
function vp_product_display_pp_info() {
global $product;
if( $value = $product->get_meta( '_venue' ) ) {
echo '<div class="product-pp-info"><i>' . __("Venue", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
if( $value = $product->get_meta( '_date' ) ) {
echo '<div class="product-pp-info"><i>' . __("Date", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
if( $value = $product->get_meta( '_time' ) ) {
echo '<div class="product-pp-info"><i>' . __("Time", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
}
// Frontend: Display PP Info on product variations
add_filter( 'woocommerce_available_variation', 'vp_variation_display_pp_info', 10, 3 );
function vp_variation_display_pp_info( $data, $product, $variation ) {
if( $value = $variation->get_meta( '_venue' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Venue", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
if( $value = $variation->get_meta( '_date' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Date", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
if( $value = $variation->get_meta( '_time' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Time", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
return $data;
}
// Frontend: Display info on cart
add_filter( 'woocommerce_cart_item_name', 'vp_cart_display_pp_info', 10, 3 );
function vp_cart_display_pp_info( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Venue", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Date", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Time", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
return $item_name;
}
// Frontend: Display info on checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'vp_checkout_display_pp_info', 10, 3 );
function vp_checkout_display_pp_info( $item_qty, $cart_item, $cart_item_key ) {
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Venue", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Date", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Time", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
return $item_qty;
}
// Save PP Info to order items (and display it on admin orders)
add_filter( 'woocommerce_checkout_create_order_line_item', 'vp_order_item_save_pp_info', 10, 4 );
function vp_order_item_save_pp_info( $item, $cart_item_key, $cart_item, $order ) {
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item->update_meta_data( '_venue', esc_attr( $value ) );
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item->update_meta_data( '_date', esc_attr( $value ) );
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item->update_meta_data( '_time', esc_attr( $value ) );
}
return $item_qty;
}
// Frontend & emails: Display PP Info on orders
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_pp_info', 10, 4 );
function vp_order_item_display_pp_info( $item_id, $item, $order, $plain_text ) {
// Not on admin
//if( is_admin() ) return;
if( $value = $item->get_meta('_venue') ) {
$value = '<i>' . __("Venue", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
if( $value = $item->get_meta('_date') ) {
$value = '<i>' . __("Date", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
if( $value = $item->get_meta('_time') ) {
$value = '<i>' . __("Time", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
}
答案 0 :(得分:0)
上面的挂钩并添加产品元数据,以便进一步操作才能在购物车和结帐页面中查看,然后在订单查看页面中必须使用下面的挂钩。
1)用于添加元
add_filter( 'woocommerce_add_cart_item_data', array($this,'addProductToCart' ), 10, 2 );
2)在会话中设置元
add_filter( 'woocommerce_get_cart_item_from_session', array($this, 'get_cart_item_from_session' ), 10, 2);
3)在结帐和购物车页面中检索并显示元数据
add_filter( 'woocommerce_get_item_data', array($this,'getting_car_item_data' ), 10, 2);
如果您仍有任何问题,请发表评论
答案 1 :(得分:0)
非常感谢您抽出宝贵的时间和您的答案。我是PHP的新手,所以我努力了解解决方案。也许使用所有代码对您来说会更容易。谢谢玛丽
/**
Display info
**/
// Admin: Add custom field
add_action('woocommerce_product_options_sku', 'vp_add_pp_info' );
function vp_add_pp_info(){
woocommerce_wp_text_input( array(
'id' => '_venue',
'label' => __('Venue', 'woocommerce' ),
'placeholder' => __('Enter Venue here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the Venue of the product.', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => '_date',
'label' => __('Date', 'woocommerce' ),
'placeholder' => __('Enter Date here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the Date of the product.', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => '_time',
'label' => __('Time', 'woocommerce' ),
'placeholder' => __('Enter Time here', 'woocommerce' ),
'desc_tip' => true,
'description' => __('This field is for the time of the product.', 'woocommerce' ),
) );
}
// Admin: Save custom field value for simple product inventory options
add_action('woocommerce_admin_process_product_object', 'vp_product_save_pp_info', 10, 1 );
function vp_product_save_pp_info( $product ){
if( isset($_POST['_venue']) )
$product->update_meta_data( '_venue', sanitize_text_field($_POST['_venue']) );
if( isset($_POST['_date']) )
$product->update_meta_data( '_date', sanitize_text_field($_POST['_date']) );
if( isset($_POST['_time']) )
$product->update_meta_data( '_time', sanitize_text_field($_POST['_time']) );
}
// Frontend: Display PP Info on product
add_action( 'woocommerce_single_product_summary', 'vp_product_display_pp_info' );
function vp_product_display_pp_info() {
global $product;
if( $value = $product->get_meta( '_venue' ) ) {
echo '<div class="product-pp-info"><i>' . __("Venue", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
if( $value = $product->get_meta( '_date' ) ) {
echo '<div class="product-pp-info"><i>' . __("Date", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
if( $value = $product->get_meta( '_time' ) ) {
echo '<div class="product-pp-info"><i>' . __("Time", "woocommerce") .
': </i>'.esc_html( $value ).'</div>';
}
}
// Frontend: Display PP Info on product variations
add_filter( 'woocommerce_available_variation', 'vp_variation_display_pp_info', 10, 3 );
function vp_variation_display_pp_info( $data, $product, $variation ) {
if( $value = $variation->get_meta( '_venue' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Venue", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
if( $value = $variation->get_meta( '_date' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Date", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
if( $value = $variation->get_meta( '_time' ) ) {
$data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Time", "woocommerce") .
': </strong>'.esc_html( $value ).'</small></p>';
}
return $data;
}
// Frontend: Display info on cart
add_filter( 'woocommerce_cart_item_name', 'vp_cart_display_pp_info', 10, 3 );
function vp_cart_display_pp_info( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Venue", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Date", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item_name .= '<p class="product-pp-info"><i>' . __("Time", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
return $item_name;
}
// Frontend: Display info on checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'vp_checkout_display_pp_info', 10, 3 );
function vp_checkout_display_pp_info( $item_qty, $cart_item, $cart_item_key ) {
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Venue", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Date", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item_qty .= '<p class="product-pp-info"><i>' . __("Time", "woocommerce") .
':</i> ' . esc_html( $value ) . '</p>';
}
return $item_qty;
}
// Save PP Info to order items (and display it on admin orders)
add_filter( 'woocommerce_checkout_create_order_line_item', 'vp_order_item_save_pp_info', 10, 4 );
function vp_order_item_save_pp_info( $item, $cart_item_key, $cart_item, $order ) {
if( $value = $cart_item['data']->get_meta('_venue') ) {
$item->update_meta_data( '_venue', esc_attr( $value ) );
}
if( $value = $cart_item['data']->get_meta('_date') ) {
$item->update_meta_data( '_date', esc_attr( $value ) );
}
if( $value = $cart_item['data']->get_meta('_time') ) {
$item->update_meta_data( '_time', esc_attr( $value ) );
}
return $item_qty;
}
// Frontend & emails: Display PP Info on orders
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_pp_info', 10, 4 );
function vp_order_item_display_pp_info( $item_id, $item, $order, $plain_text ) {
// Not on admin
//if( is_admin() ) return;
if( $value = $item->get_meta('_venue') ) {
$value = '<i>' . __("Venue", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
if( $value = $item->get_meta('_date') ) {
$value = '<i>' . __("Date", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
if( $value = $item->get_meta('_time') ) {
$value = '<i>' . __("Time", "woocommerce") . ':</i> ' . esc_attr( $value );
// On orders
if( is_wc_endpoint_url() )
echo '<div class="pp-info"><p>' . $value . '</p></div>';
// On Emails
else
echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
}
}