Woocommerce Help
-
Hi,
I am developing a woocommerce site with the wc-vendors plugin.
I am trying to add a piece of functionality that is working, but then breaks.
When a user buys a specific product from my store, it starts by auto completing the order, next it changes the users role from subscriber to pending vendor and then lastly it redirects to it’s vendor page for the person to fill in details.
It all works fine and in the backend of WordPress the user role is changed along with the order being completed. The issue i have is that when i am on the frontend of the site, after about 30 seconds if i hard refresh the page the role changes in the backend from pending vendor back to subscriber? I am so confused by this. The functions are below, not the best developer in the world so trying to piece this functionality together from different forums etc. Any help would be incredible!
function lgbk_add_member( $order_id ) { $order = new WC_Order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product_name = $item['name']; $product_id = $item['product_id']; $product_variation_id = $item['variation_id']; if ( $order->user_id > 0 && $product_id == '272' ) { update_user_meta( $order->user_id, 'paying_customer', 1 ); $user = new WP_User( $order->user_id ); // Remove role $user->remove_role( 'subscriber' ); // Add role $user->add_role( 'pending_vendor' ); wp_redirect(bloginfo('url') . '/vendor_dashboard/'); } } } add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' ); function custom_woocommerce_auto_complete_order( $order_id ) { global $woocommerce; if ( !$order_id ) return; $order = new WC_Order( $order_id ); $order->update_status( 'completed' ); } add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
- The topic ‘Woocommerce Help’ is closed to new replies.