Last active
December 11, 2022 07:30
-
-
Save idokd/09129723b0f775cc64694715f6807db1 to your computer and use it in GitHub Desktop.
Woocommerce Product Vendors - Simple using Post Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Product Vendors */ | |
| add_action( 'init', function() { | |
| add_post_type_support( 'product', [ 'author' ] ); | |
| add_post_type_support( 'shop_order', [ 'author' ] ); | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) register_taxonomy_for_object_type( WC_PRODUCT_VENDORS_TAXONOMY, 'shop_order' ); | |
| }, 12 ); | |
| if ( is_admin() ) { | |
| add_action( 'wp_after_insert_post', 'wcpv_product_vendors_save_order_vendor' ); | |
| function wcpv_product_vendors_save_order_vendor( $post_id ) { | |
| if ( !current_user_can( 'manage_woocommerce' ) || 'shop_order' !== get_post_type( $post_id ) ) { | |
| return( true ); | |
| } | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) { | |
| $term = !empty( $_POST['wcpv_product_term'] ) ? absint( $_POST['wcpv_product_term'] ) : ''; | |
| if ( !$term ) return( true ); | |
| wp_set_object_terms( $post_id, $term, WC_PRODUCT_VENDORS_TAXONOMY ); | |
| update_post_meta( $post_id, '_wcpv_vendor', $term ); | |
| $data = get_term_meta( $term, 'vendor_data', true ); | |
| $user_id = $data[ 'admins' ][ 0 ]; | |
| wp_update_post( [ | |
| 'ID' => $post_id, | |
| 'post_author' => $user_id, | |
| ], false, false ); | |
| } | |
| // _wcpv_vendor_user_vendors(); | |
| return( true ); | |
| } | |
| function _wcpv_vendor_user_vendors( $user_id = null ) { | |
| if ( !defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) return( null ); | |
| $terms = get_terms( [ | |
| 'taxonomy' => WC_PRODUCT_VENDORS_TAXONOMY, | |
| 'hide_empty' => false, | |
| 'meta_key' => 'vendor_data', | |
| 'fields' => 'ids' | |
| ] ); | |
| $user_id = $user_id ? : get_current_user_id(); | |
| $vendors = []; | |
| foreach ( $terms as $term_id ) { | |
| $data = get_term_meta( $term_id, 'vendor_data', true ); | |
| if ( in_array( $user_id, $data[ 'admins' ] ) ) $vendors[] = $term_id; | |
| } | |
| return( $vendors ); | |
| } | |
| add_action( 'restrict_manage_posts', function () { | |
| $screen = get_current_screen(); | |
| global $post_type; | |
| if ( !in_array( $screen->id, [ 'edit-product', 'edit-shop_order' ] ) ) return; | |
| $args = [ | |
| 'show_option_all' => __( 'All Vendors', 'hello-elementor-child' ), | |
| 'orderby' => 'display_name', | |
| 'order' => 'ASC', | |
| 'name' => 'author', | |
| 'who' => 'any', | |
| 'include_selected' => true | |
| ]; | |
| if( isset( $_GET[ 'author' ] ) ){ | |
| $args[ 'selected' ] = (int) sanitize_text_field( $_GET[ 'author' ] ); | |
| } | |
| wp_dropdown_users( $args ); | |
| } ); | |
| add_filter( 'manage_edit-shop_order_columns', function ( $columns ){ | |
| if ( !defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) $columns[ 'vendor' ] = __( 'Vendor', 'hmeonot' ); | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) $columns[ 'vendors' ] = __( 'Vendors', 'hmeonot' ); | |
| return( $columns ); | |
| }, 9999 ); | |
| add_action( 'manage_shop_order_posts_custom_column', function ( $column, $order_id ) { | |
| $vendor = get_the_author_meta( 'display_name', get_post_field( 'post_author', $order_id ) ); | |
| if ( $column == 'vendor' ) { | |
| echo $vendor; | |
| } | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) && $column == 'vendors' ) { | |
| $vendors = wp_get_post_terms( $order_id, 'wcpv_product_vendors', [ 'fields' => 'names' ] ); | |
| $vendors = count( $vendors ) ? $vendors : get_post_meta( $order_id, '_wcpv_vendor' ); | |
| echo implode( ',', $vendors );// ? : $vendor; | |
| } | |
| }, 10, 2 ); | |
| add_filter( 'woocommerce_order_query_args', function( $args ) { | |
| if ( current_user_can( 'vendor' ) ) { | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) { | |
| $args[ 'meta_key' ] = '_wcpv_vendor'; | |
| $args[ 'meta_value' ] = _wcpv_vendor_user_vendors(); | |
| } else { | |
| $args[ 'post_author' ] = get_current_user_id(); | |
| } | |
| } | |
| return( $args ); | |
| } ); | |
| add_action( 'pre_get_posts', function( &$wp_query ) { | |
| if ( current_user_can( 'vendor' ) ) { | |
| if ( defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ) { | |
| $wp_query->query_vars[ 'meta_key' ] = '_wcpv_vendor'; | |
| $wp_query->query_vars[ 'meta_value' ] = _wcpv_vendor_user_vendors(); | |
| } else { | |
| $wp_query->query_vars[ 'author' ] = get_current_user_id(); | |
| } | |
| } | |
| } ); | |
| add_filter( 'wp_dropdown_users_args', function( $query_args, $parsed_args ) { | |
| global $post; | |
| if ( $post->post_type != 'product' ) return( $query_args ); | |
| $post_type_object = get_post_type_object( $post->post_type ); | |
| $query_args[ 'capability' ] = ''; | |
| $query_args[ 'capability__in' ] = [ 'manage_woocommerce', 'vendor', 'subscriber', $post_type_object->cap->edit_posts ]; | |
| return( $query_args ); | |
| }, 100, 2 ); | |
| } | |
| function _woocommerce_order_assign_vendor( $orders, $parent = null ) { | |
| foreach ( $orders as $order_id ) { | |
| $mode = defined( 'WC_PRODUCT_VENDORS_TAXONOMY' ) ? WC_PRODUCT_VENDORS_TAXONOMY : 'author'; | |
| $vendors = []; $vendor_id = null; $author_id = null; | |
| $order = wc_get_order( $order_id ); | |
| $items = $order->get_items(); | |
| foreach( $items as $item ) { | |
| $product_id = $item->get_product_id(); | |
| $vendor_id = $author_id = get_post_field( 'post_author', $product_id ); | |
| switch ( $mode ) { | |
| case WC_PRODUCT_VENDORS_TAXONOMY: | |
| $terms = wp_get_object_terms( $product_id, WC_PRODUCT_VENDORS_TAXONOMY, [ 'fields' => 'ids' ] ); | |
| $vendor_id = count( $terms ) ? $terms[ 0 ] : null; | |
| break; | |
| case 'author': | |
| default: | |
| break; | |
| } | |
| if ( isset( $vendors[ $vendor_id ] ) ) $vendors[ $vendor_id ][] = $product_id; | |
| else $vendors[ $vendor_id ] = [ $product_id ]; | |
| } | |
| if ( count( $vendors ) == 1 && count( $vendors[ $vendor_id ] ) == count( $items ) ) { | |
| wp_update_post( [ | |
| 'ID' => $order_id, | |
| 'post_author' => $author_id, | |
| ] ); | |
| if ( $mode == WC_PRODUCT_VENDORS_TAXONOMY ) { | |
| wp_set_object_terms( $order_id, $vendor_id, WC_PRODUCT_VENDORS_TAXONOMY ); | |
| } | |
| } | |
| return( $author_id ); | |
| } | |
| } | |
| // Sending an email notification when order get 'sent' status to vendor | |
| // woocommerce_payment_complete | |
| // | |
| add_filter( 'woocommerce_email_recipient_new_order', function( $recipient, $order, $email ) { | |
| $order = $order instanceof WC_Order ? $order : wc_get_order( $order ); | |
| if ( !$order ) return( $recipient ); | |
| $author_id = get_post_field( 'post_author', $order->get_id() ); | |
| if ( user_can( $author_id, 'administrator' ) ) return( $recipient ); | |
| $email = get_the_author_meta( 'user_email', $author_id ); | |
| $recipient .= ',' . $email; | |
| $order->add_order_note( sprintf( __( 'Adding vendor e-mail to new order email: %s', 'hello-elementor-child' ), $email ) ); | |
| update_post_meta( $order_id, '_new_order_email_vendor_added', $email ); | |
| return( $recipient ); | |
| }, 100, 3 ); | |
| add_action( 'woocommerce_order_status_processing', '_woocommerce_order_status_processing' ); | |
| function _woocommerce_order_status_processing( $order ) { | |
| $order = $order instanceof WC_Order ? $order : wc_get_order( $order ); | |
| if ( !$order ) return; | |
| $order_id = $order->get_id(); | |
| $author_id = _woocommerce_order_assign_vendor( [ $order_id ] ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment