编辑:已解决。我用解决方案编辑了帖子。 =)
我正在尝试使用自定义产品字段中的值填充结帐页面上的选择字段。其中大多数都像魅力一样,但在结帐页面上检索值时存在问题。
创建并保存自定义字段
// Display Fields in Backend
add_action( 'woocommerce_product_options_general_product_data', 'srd_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'srd_add_custom_general_fields_save' );
// Create Fields
function srd_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_textarea_input(
array(
'id' => '_einstiegsorte',
'label' => __( 'Einstiegsorte', 'woocommerce' ),
'placeholder' => '',
'description' => __( '', 'woocommerce' )
)
);
}
保存数据
function srd_add_custom_general_fields_save( $post_id ){
$woocommerce_textarea = $_POST['_einstiegsorte'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_einstiegsorte', esc_html( $woocommerce_textarea ) );
单品及产品的输出数据存档
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_fields_ausgabe_archive', 2 );
add_action( 'woocommerce_single_product_summary', 'custom_fields_ausgabe', 6);
function custom_fields_ausgabe(){
$list_items = get_post_meta(get_the_ID(), '_einstiegsorte', true);
if($list_items){?>
$list_items = explode("\n", $list_items);
echo '<ul>';
foreach($list_items as $list_item) {
echo '<li>' . $list_item . '</li>';
}
echo '</ul>';
}
}
将选择字段添加到结帐页面并填充数据'_einstiegsorte'
add_action('woocommerce_before_checkout_form', 'einstiegswahl_select');
function einstiegswahl_select() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item = $cart_item['data'];
if(!empty($item)){
$product = new WC_product($item->id);
//$pd_numbers = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true );
$list_items['choices'] = array();
$list_items = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true );
if($list_items){
$list_items = explode("\n", $list_items);
echo '<select name=einstiegswahl>';
foreach($list_items as $list_item) {
echo '<option>' . $list_item . '</option>';
}
echo '</select>';
}}}}
答案 0 :(得分:1)
请尝试这样做:
add_action('woocommerce_before_checkout_form', 'display_einstieg_meta');
function display_einstieg_meta() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
echo get_post_meta( $cart_item['data']->get_id(), '_einstiegsorte', true );
}
}
现在应该可以了。注意:$cart_item['data']
已经是WC_Product
对象实例...
所以因为购物车中有很多商品,您的选择字段代码将为:
add_action( 'woocommerce_before_checkout_billing_form', 'custom_einstiegswahl');
function custom_einstiegswahl(){
echo '<select name=einstiegswahl>';
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get the custom field data
$einstiegsorte = get_post_meta( $cart_item['data']->get_id(), '_einstiegsorte', true );
if( ! empty($einstiegsorte) ){
// if it's multiline we split it in an array
$select_field_items = explode( '\n', $einstiegsorte );
// If the array has more than one item
if( sizeof( $select_field_items ) > 1 ){
foreach( $select_field_items as $value )
echo '<option value="'. $value .'">' . $value . '</option>';
}
// If there is only one line
else {
// we clean it
$value = str_replace('\n', '', $einstiegsorte);
echo '<option value="'. $value .'">' . $value . '</option>';
}
}
}
echo '</select>';
}