我需要在用户可以选择供应商的产品,在购物车中只能添加一个产品。
我需要在结帐页面上添加一个下拉列表选择供应商并更新产品post_author id,但是当用户下订单时使用此功能,新的供应商ID不会保存,您可以帮我纠正这个吗?
选择下拉列表是好的,我获得了价值ID供应商和显示名称但不保存任何工作,但我还不知道如何允许某些供应商按产品列入此列表。
add_action('woocommerce_before_order_notes', wps_add_select_checkout_field3');
function wps_add_select_checkout_field3( $checkout ) {
global $product;
function wcfm_get_vendor_list( $all = false, $offset = '', $number = '', $search = '' ) {global $WCFM;
$is_marketplace = wcfm_is_marketplace();
$vendor_arr = array();
if( $is_marketplace ) {
if( !wcfm_is_vendor() ) {
if( $all ) {
$vendor_arr = array( 0 => __('All', 'wc-frontend-manager' ) );
} else {
$vendor_arr = array( '' => __('Choose Vendor ...', 'wc-frontend-manager' ) );
}
$wcfm_allow_vendors_list = apply_filters( 'wcfm_allow_vendors_list', '', $is_marketplace );
$args = array(
'role__in' => array( 'dc_vendor', 'vendor', 'seller' ),
'orderby' => 'login',
'order' => 'ASC',
'include' => $wcfm_allow_vendors_list,
'count_total' => false,
'fields' => array( 'ID', 'display_name' )
);
if( $number ) {
$args['offset'] = $offset;
$args['number'] = $number;
}
if( $search ) {
$args['search'] = $search;
}
$all_users = get_users( $args );
if( !empty( $all_users ) ) {
foreach( $all_users as $all_user ) {
$vendor_arr[$all_user->ID] = $all_user->display_name; }
}
}
}
return $vendor_arr;
}
$author = wcfm_get_vendor_list();
foreach( WC()->cart->get_cart() as $cart_item ){
$product = $cart_item['data'];
$product_id = $cart_item['product_id'];
$post_author_id = get_post_field( 'post_author', $product_id );}
echo '<h4>'.__('Selectionner un établissement').'</h4>';
woocommerce_form_field( 'post_author', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Delivery options' ),
'options' => $author ),
$checkout->get_value( 'post_author' ));
}
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if ($_POST['post_author'] == "blank")
wc_add_notice( '<strong>Please select options</strong>', 'error' );
}
//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');function wps_select_checkout_field_update_order_meta( $post_id ) {
if ($_POST['post_author'])
update_post_meta( $post_id, 'post_author', esc_attr($_POST['post_author']));
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($post_id){
echo '<p><strong>'.__('Delivery option').':</strong> ' . get_post_meta( $post_id, 'post_author', true ) . '</p>';
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
$keys['post_author:'] = 'post_author';
return $keys;
}