getdokan / dokan

Multivendor marketplace platform
https://wordpress.org/plugins/dokan-lite/
254 stars 200 forks source link

Save information from checkout custom fields in suborder #2263

Closed riannarcizo closed 4 months ago

riannarcizo commented 5 months ago

Is your feature request related to a problem? Please describe.

Hello!

I'm trying to override the create_sub_order function to add information from 3 custom fields that we have at checkout so that sellers can invoice orders in Brazil.

Below is the code that I inserted into the functions.php of my child theme in an attempt to add the billing_cpf, billing_cnpj and billing_ie information to the subrequests, but unfortunately it is not working.

Dokan Version 3.10.2 Dokan Version Pro 3.10.2 Woocommerce Version 8.8.2

Can anyone help me with this?

// Sobrescreva a função create_sub_order do Dokan add_action( 'plugins_loaded', 'dokan_custom_override_functions', 15 ); function dokan_custom_override_functions() { if ( ! class_exists( 'Dokan_Pro' ) ) { require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); }

if ( is_plugin_active( 'dokan-lite/dokan.php' ) || is_plugin_active( 'dokan-pro/dokan.php' ) ) {
    // Substitua a função create_sub_order com sua versão personalizada
    if ( ! function_exists( 'create_sub_order' ) ) {
        function create_sub_order( $parent_order, $seller_id, $seller_products ) {
            dokan_log( 'Creating sub order for vendor: #' . $seller_id );

            $bill_ship = [
                'billing_country',
                'billing_first_name',
                'billing_last_name',
                'billing_company',
                'billing_address_1',
                'billing_address_2',
                'billing_city',
                'billing_state',
                'billing_postcode',
                'billing_email',
                'billing_phone',
                'billing_cpf', // Adicionando billing_cpf
                'billing_cnpj', // Adicionando billing_cnpj
                'billing_ie', // Adicionando billing_ie
            ];

            try {
                $order = new WC_Order();

                // save other details
                $order->set_created_via( 'dokan' );
                $order->set_cart_hash( $parent_order->get_cart_hash() );
                $order->set_customer_id( $parent_order->get_customer_id() );
                $order->set_currency( $parent_order->get_currency() );
                $order->set_prices_include_tax( $parent_order->get_prices_include_tax() );
                $order->set_customer_ip_address( $parent_order->get_customer_ip_address() );
                $order->set_customer_user_agent( $parent_order->get_customer_user_agent() );
                $order->set_customer_note( $parent_order->get_customer_note() );
                $order->set_payment_method( $parent_order->get_payment_method() );
                $order->set_payment_method_title( $parent_order->get_payment_method_title() );
                $order->update_meta_data( '_dokan_vendor_id', $seller_id );

                // save billing and shipping address
                foreach ( $bill_ship as $key ) {
                    if ( is_callable( [ $order, "set_{$key}" ] ) ) {
                        $value = $parent_order->{"get_{$key}"}(); // Obtenha o valor do pedido principal
                        if ($key === 'billing_company') { // Condição para billing_company
                            $value = $parent_order->{"get_{$key}"}(); // Obtenha o valor do pedido principal
                            if (empty($value)) { // Se billing_company no pedido principal estiver vazio, use o valor do campo billing_company do subpedido
                                $value = $parent_order->{"get_billing_company"}();
                            }
                        } elseif (strpos($key, 'billing_') !== false) {
                            $value = $parent_order->get_meta($key, true); // Obtenha os valores personalizados do pedido principal
                        }
                        $order->{"set_{$key}"}( $value ); // Defina o valor no subpedido
                    }
                }

                // save other meta data
                $order->save(); // need to save order data before passing it to a hook

                // now insert line items
                $this->create_line_items( $order, $seller_products );

                // do shipping
                $this->create_shipping( $order, $parent_order );

                // do tax
                $this->create_taxes( $order, $parent_order, $seller_products );

                // add coupons if any
                $this->create_coupons( $order, $parent_order, $seller_products );

                $order->save(); // need to save order data before passing it to a hook

                do_action( 'dokan_create_sub_order_before_calculate_totals', $order, $parent_order, $seller_products );

                // finally, let the order re-calculate itself and save
                $order->calculate_totals();
                $order->set_status( $parent_order->get_status() );
                $order->set_parent_id( $parent_order->get_id() );
                $order->save();

                // update total_sales count for sub-order
                wc_update_total_sales_counts( $order->get_id() );

                dokan_log( 'Created sub order : #' . $order->get_id() );

                do_action( 'dokan_checkout_update_order_meta', $order->get_id(), $seller_id );
            } catch ( Exception $e ) {
                return new WP_Error( 'dokan-suborder-error', $e->getMessage() );
            }
        }
    }
}

}

Describe the solution you'd like

I need to save these three pieces of information in suborders so that the seller can view them within the orders and bill the customer.

Describe alternatives you've considered

No response

Additional context

No response

shohag121 commented 4 months ago

Hello @riannarcizo,

You can utilize this hook dokan_checkout_update_order_meta to add additional metadata to your order.

Thank you.

YeasinArafat1998 commented 4 months ago

Hello @riannarcizo,

I hope you've found the solution you're looking for. I'm closing this issue now, but please don't hesitate to reopen it if you require further assistance.

All the best,

fredroo commented 2 months ago

@riannarcizo how you solved your problem with cpf and cnpj?