• Resolved niikk

    (@niikk)


    Hello,

    We know that dokan saves the earnings per product in meta value _dokan_commission_type and _dokan_commission_rate. We want to display them under each product row in the admin backend order overview.

    This is our code, but it displays nothing.

    How we have to do that?

    function display_product_vendor_earnings( $item, $cart_item_key, $values, $order ) {
      if( isset( $values['_dokan_commission_rate'] ) ) {
        $item->add_meta_data(
          __( 'Earning per product', 'WooCommerce' ),
          $values['_dokan_commission_rate'],
          true
        );
      }
    }
    
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_product_vendor_earnings', 10, 4 );
Viewing 10 replies - 1 through 10 (of 10 total)
  • Hello @niikk ,

    It’s always great to see that you are trying to extend Dokan functionality.

    I do not have an exact example code for you on this requirement. If I understand correctly, Dokan uses wc_add_order_item_meta to set the commission. So, you can use wc_get_order_item_meta to retrieve that information.

    I have not tested it so, I am unable to guarantee this. However, this should work to retrieve any metadata saved with the ordered item.

    Thank you 🙂

    Thread Starter niikk

    (@niikk)

    Hello @rur165

    Yes, So we want to display under each product in the order the commission typ and commission rate. With wc_add_order_item_met we will display the commission from the whole order right?

    Like in that example here the “name your shirt row”: https://rudrastyh.com/wp-content/uploads/2018/02/woocommerce-order-items-in-admin.png

    I’m not able to display the needed information with this code here. Hoe do I have to access this _dokan_commission_rate and _dokan_commission_type?

    add_action( 'woocommerce_checkout_create_order_line_item', display_product_vendor_earnings', 10, 4 );
     
    function display_product_vendor_earnings( $item, $cart_item_key, $values, $order ) {
     
    	if ( !empty( $values['_dokan_commission_rate'] ) ) {
    		$item->add_meta_data( 'Product commission:', $values['_dokan_commission_rate'] );
    	}
    }

    Or with that one:

    function display_product_commissions($order){
        echo "<p><strong>Name of pickup person:</strong> " . $order->order_custom_fields['_dokan_commission_type'][0] . "</p>";
    }
    
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_product_commissions', 10, 1 );

    You should be able to copy that in your function.php and then it should be displayed IF the code works 😉 Because those values are already stored in the order right?

    • This reply was modified 4 years, 3 months ago by niikk.
    Thread Starter niikk

    (@niikk)

    Hi @rur165

    Now I also tried to access the order product meta array… But same result. Nothing is displayed. Can it be that hard just to display those values?! hehe 😂

    function display_product_commissions($order){
        
    global $post;
    $order = wc_get_order( $post->ID );
    $items = $order->get_items(); 
    
    foreach ( $order->get_items() as $item_id => $item ) {
    
        // Here you get your data
        $custom_field = wc_get_order_item_meta( $item_id, '_dokan_commission_rate', true ); 
    
        // To test data output (uncomment the line below)
        print_r($custom_field);
    
    		// Iterating in an array of keys/values
    		foreach( $custom_field as $key => $value ){
    			echo '<p>key: '.$key.' | value: '.$value.'</p>';
    		} 
    
    	}
    	
    }
    
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_product_commissions', 10, 1 );

    I saw, that in dokan-lite commission.php you guys use a function to hide those values:

    /**
         * Hide extra meta data
         *
         * @since  2.9.21
         *
         * @param  array
         *
         * @return array
         */
        public function hide_extra_data( $formated_meta ) {
            $meta_to_hide   = [ '_dokan_commission_rate', '_dokan_commission_type', '_dokan_additional_fee' ];
            $meta_to_return = [];
    
            foreach ( $formated_meta as $key => $meta ) {
                if ( ! in_array( $meta->key, $meta_to_hide ) ) {
                    array_push( $meta_to_return, $meta );
                }
            }
    
            return $meta_to_return;
        }
    • This reply was modified 4 years, 3 months ago by niikk.

    Hello @niikk ,

    Again, it’s wonderful that you share the updates of your work here.

    All these require time to debug and according to our support policy I am unable to go through the custom codes hence it is hard to make a comment on this.

    However, you have actually found the solution already. The function hide_extra_data is actually stopping you to access this metadata. You can comment out the function from line no. 38 on this file – \dokan-lite\includes\Commission.php.

    Then you will see the metadata is showing automatically in the ordered item – https://prnt.sc/10dccrc.

    I do not have an example code to remove the filter using a child theme so can’t share that which would have been good to have.

    I hope you understand the limitation.

    Thank you 🙂

    Thread Starter niikk

    (@niikk)

    @rur165

    Thanks. Yeah.. now its working. But always change this after a update is stupid. 😄
    But I guess it should not be that hard so create a filter to unset that via function.php file?

    I tried with that.. but does not work.. any ideas why? 🙂

    add_action( 'plugins_loaded', 'webearth_remove_plugin_filter' );
    function webearth_remove_plugin_filter() {
        remove_filter( 'woocommerce_order_item_get_formatted_meta_data',
        array('constructor', 'hide_extra_data'),
        10 );
    }
    • This reply was modified 4 years, 3 months ago by niikk.
    Thread Starter niikk

    (@niikk)

    @rur165 I guess the code above should work. But I thins the class from the array is wrong. Where is this class defined?

    Thread Starter niikk

    (@niikk)

    @rur165

    Hello Rashed,

    I’m still trying to get this to work. I think I always get the wrong class name. I’m not sure if constructor is the right class name. After I have the right class name, I think the snipped should work.. 😉

    add_action( 'plugins_loaded', 'webearth_remove_plugin_filter' );
    
    function webearth_remove_plugin_filter() {
    	
    	global $constructor;
    
        remove_filter( 'woocommerce_order_item_get_formatted_meta_data',
        array('$constructor', 'hide_extra_data'),
        10 );
    }

    Hello @niikk ,

    This version of the code seems to be working for me –

    add_action( 'init', function () {
                remove_filter( 'woocommerce_order_item_get_formatted_meta_data', [ dokan()->commission, 'hide_extra_data' ] );
            }, 20 );

    Thank you 🙂

    Thread Starter niikk

    (@niikk)

    @rur165

    Works! Cool!

    Just one last question: Those values are stored in the order right? So if I change a commission type or amount from product A, it will not change the values there in orders where product A was ordered by the customer?

    Hello @niikk ,

    You are right that these are saved with the order. So changing the commission will not affect these values.

    If you make a new order while having a different commission then that will be saved. Consider it as a product price. You can change the price any time but that does not reflect on all orders.

    I hope this clears the confusion.

    Thank you 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display product earnings in admin order backend’ is closed to new replies.