2016-12-19 2 views
3

사용자 등록 후 제품을 자동으로 생성 할 수 있습니까?사용자 등록 후 제품 생성

나는 시장의 종류를 만드는거야

, 그래서 사용자가 로그인이 공급 업체로, 나는 Woocommerce가 자동으로 이러한 사양을 가진 제품을 만들려면 최대 :

제품 이름 = 예약 - the_author_meta ('display_name')
제품 슬러그 = 일정 - the_author_meta ('user_login')

예를 들면. John Doe는 "username : johndoe"으로 공급 업체로 등록합니다. 등록이 완료되면 제품이 자동으로 생성됩니다.

제품 이름은 "예약 - John Doe"입니다. 제품 슬러그는 "calendar-johndoe"입니다. 그래서 제품의 예약

- 홍길동이 mysite.com/product/calendar-johndoe

에서 찾을 수는

답변

2

당신은 user_register 때문에에 후킹하여이를 달성 할 수있는 시간을 내 주셔서 감사합니다 WordPress 코어는 사용자 등록을 처리하고 사용자가 등록 된 직후 user_register 훅을 실행합니다. 및 에 코드 당신이 여기 post_type = product

와 포스트 삽입 wp_insert_post methord를 사용할 수있는 제품입니다 작성 :

add_action('user_register', 'myCustomProduct', 10, 1); 

function myCustomProduct($user_id) 
{ 
    $user_info = get_userdata($user_id); 
    $display_name = $user_info->display_name; 

    $user_full_name = get_user_meta($user_id, 'first_name', TRUE) . ' ' . get_user_meta($user_id, 'last_name', TRUE); 

    $my_product_name = 'Booking - ' . trim($user_full_name); 
    $my_slug = 'calendar-' . str_replace(array(' '), '', strip_tags($display_name)); 

    $post = array(
     'post_author' => $user_id, 
     'post_content' => '', 
     'post_status' => "publish", 
     'post_title' => wp_strip_all_tags($my_product_name), 
     'post_name'=> $my_slug, 
     'post_parent' => '', 
     'post_type' => "product", 
    ); 

    //Create Post 
    $post_id = wp_insert_post($post, $wp_error); 

    //set Product Category 
    //wp_set_object_terms($post_id, 'Your Category Name ', 'product_cat'); 
    //set product type 
    wp_set_object_terms($post_id, 'simple', 'product_type'); 

    update_post_meta($post_id, '_visibility', 'visible'); 
    update_post_meta($post_id, '_stock_status', 'instock'); 
    update_post_meta($post_id, 'total_sales', '0'); 
    update_post_meta($post_id, '_sku', ""); 
    update_post_meta($post_id, '_product_attributes', array()); 
    update_post_meta($post_id, '_manage_stock', "no"); 
    update_post_meta($post_id, '_backorders', "no"); 
    update_post_meta($post_id, '_stock', ""); 
    //update_post_meta($post_id, '_downloadable', 'yes'); 
    //update_post_meta($post_id, '_virtual', 'yes'); 
    //update_post_meta($post_id, '_regular_price', "1"); 
    //update_post_meta($post_id, '_sale_price', "1"); 
    //update_post_meta($post_id, '_purchase_note', ""); 
    //update_post_meta($post_id, '_featured', "no"); 
    //update_post_meta($post_id, '_weight', ""); 
    //update_post_meta($post_id, '_length', ""); 
    //update_post_meta($post_id, '_width', ""); 
    //update_post_meta($post_id, '_height', ""); 
    //update_post_meta($post_id, '_sale_price_dates_from', ""); 
    //update_post_meta($post_id, '_sale_price_dates_to', ""); 
    //update_post_meta($post_id, '_price', "1"); 
    //update_post_meta($post_id, '_sold_individually', ""); 
} 

코드 활성 자식 테마의 function.php 파일의 이동을 (또는 테마). 또는 모든 플러그인 PHP 파일에서.
코드는 테스트되었으며 완벽하게 작동합니다.

참고 : 내가 등록 양식을 갖는다 고 가정 한 first_name 그렇지 않으면 last_name 필드 기본적 WooCommerce 등록 양식을 사용하는 경우 다음에만 그럼 코드가 Booking -로 제품 이름을 생성합니다 위에 emailpassword 필드를 가지고.

희망이 도움이됩니다.

+0

정말 멋지다! 완벽하게 작동하고 올바른 공급 업체를 지정합니다. 나는 왜 그것이 모든 제품 부르기를 부르는 지 궁금하다 - 사용자 신임장 대신에 admin과 slug calendar-admin. 고맙습니다! –

+0

위의 코드를 테스트하지 않았기 때문에 효과가있어 기쁩니다. 그 모든 것이 나의 워드 프레스 지식을 형성합니다. 내가 집에 갈 때 시험 해보고 그러면 너에게 알려 줄거야. –

+0

예, 관리자 자격 증명이 필요합니다. 나는'$ user_info-> display_name;'을'$ user_info-> user_firstname;'으로 바꾸려고했지만 아무것도 변경하지 않았습니다. 아직도 고마워! –

관련 문제