user20065248
user20065248

Reputation: 1

sending sms to dokan vendors on order processing status

This snippet hooks into the WooCommerce order status change event and sends an SMS to each vendor associated with the products in the order. The SMS will notify the vendor of the new order and prompt them to check their account for processing.

*// Hook into WooCommerce order status change
add_action( 'woocommerce_order_status_processing', 'send_sms_to_vendors_on_order_processing', 10, 1 );

function send_sms_to_vendors_on_order_processing( $order_id ) {
    // SMS API endpoint and credentials
    $api_url = '';
    $api_key = ''; // Replace with your API key
    $sender_id = ''; // Replace with your sender ID

    // Get the order object
    $order = wc_get_order( $order_id );

    // Initialize an empty array to hold vendor phone numbers
    $store_phones = array();

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get product object
        $product = $item->get_product();
        
        // Author ID
        $author_id = $product->post->post_author;
        
        // Get vendor details
        $vendor = dokan()->vendor->get( $author_id );
        $store_phone = $vendor->get_phone();
        
        // Check if phone number is available and not already added
        if ( ! empty( $store_phone ) && ! in_array( $store_phone, $store_phones ) ) {
            $store_phones[] = $store_phone;
        }
    }

    // Prepare SMS message
    $message = 'NETMART: You have a new order. Please check your account to process it.';

    // Send SMS to each vendor
    foreach ( $store_phones as $phone_number ) {
        $response = wp_remote_post( $api_url, array(
            'method'    => 'POST',
            'body'      => array(
                'api_key'   => $api_key,
                'sender_id' => $sender_id,
                'to'        => $phone_number,
                'message'   => $message,
            ),
        ) );

        // Optional: Check the response status
        if ( is_wp_error( $response ) ) {
            error_log( 'SMS sending failed: ' . $response->get_error_message() );
        } else {
            $response_body = wp_remote_retrieve_body( $response );
            // Log or process the response if needed
        }
    }
}

*

have tried sending sms to vendor it is not working only sending sms to customers that works

Upvotes: 0

Views: 17

Answers (0)

Related Questions