我正在设置ACF,以便在单个产品页面中选择选项。我已经在单个产品页面中添加了自定义字段。如何保存多个字段? 这段代码有效,但是我需要保存多个字段,例如在添加的字段option-color上。
/** Output custom fields field. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** Add custom fields to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
if ( empty( $option_multi_trip_one_way ) ) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/**Display custom fields in the cart. */
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) ) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add custom fields to order. */
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) { if ( empty( $values['option-multi-trip-one-way'] ) ) {
return; } $item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );}add_action( woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );
http://prntscr.com/n42nig http://prntscr.com/n42nz0 http://prntscr.com/n42oj5
答案 0 :(得分:0)
/**Output ACF in single product. */
function product_options_output_field() {
$html = "";
if( have_rows('select_options') ):
$html .= "<div class='select-options'>";
$html .= "<h4>Select Options</h4>";
$html .= "<select class='option-multi-trip-one-way' id='option-multi-trip-one-way' name='option-multi-trip-one-way'>";
$html .= "<option value='N/A'>Multi-trip/One-way **</option>";
while( have_rows('select_options') ) : the_row();
$multi_trip = get_sub_field('multi-tripone-way');
$html .= "<option value=".$multi_trip.">".$multi_trip."</option>";
endwhile;
$html .= "</select>";
$html .= "<br/>";
$html .= "<select class='option-color' id='option-color' name='option-color'>";
$html .= "<option value='N/A'>Color</option>";
while( have_rows('select_options') ) : the_row();
$color = get_sub_field('color');
$html .= "<option value=".$color.">".$color."</option>";
endwhile;
$html .= "</select>";
$html .= "</div>";
endif;
echo $html;
}
add_action( 'woocommerce_before_add_to_cart_button', 'product_options_output_field', 10 );
/** * Add ACF to cart item. */
function product_options_add_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$option_multi_trip_one_way = filter_input( INPUT_POST, 'option-multi-trip-one-way');
$option_color = filter_input( INPUT_POST, 'option-color');
if ( empty( $option_multi_trip_one_way) || empty( $option_color)) {
return $cart_item_data;
}
$cart_item_data['option-multi-trip-one-way'] = $option_multi_trip_one_way;
$cart_item_data['option-color'] = $option_color;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'product_options_add_to_cart_item', 10, 3 );
/** Display ACF in the cart.*/
function product_options_display_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['option-multi-trip-one-way'] ) || empty( $cart_item['option-color'] )) {
return $item_data;
}
$item_data[] = array(
'key' => __( 'Multi-trip/One-way **' ),
'value' => wc_clean( $cart_item['option-multi-trip-one-way'] ),
'display' => '',
);
$item_data[] = array(
'key' => __( 'Color' ),
'value' => wc_clean( $cart_item['option-color'] ),
'display' => '',
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'product_options_display_cart', 10, 2 );
/** Add ACF to order.*/
function product_options_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['option-multi-trip-one-way'] ) || empty( $values['option-color'] )) {
return;
}
$item->add_meta_data( __( 'Multi-trip/One-way **' ), $values['option-multi-trip-one-way'] );
$item->add_meta_data( __( 'Color' ), $values['option-color'] );
}
add_action( 'woocommerce_checkout_create_order_line_item', 'product_options_to_order_items', 10, 4 );