Skip to content
Snippets Groups Projects

Fakturownia - dodanie kolumny z rabatem procentowym

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Mateusz Okła
    Edited
    functions.php 976 B
    /**
     * This adds the new weight unit to WooCommerce
     */
    
    add_filter( 'fakturownia/invoice/data', 'wpdesk_fakturownia_set_discount', 10, 2 );
    function wpdesk_fakturownia_set_discount( array $data, WPDesk\WooCommerceFakturownia\Data\DocumentData $document_data ) {
    	$order_items = $document_data->getOrder()->get_items();
    	foreach ( $data['positions'] as $data_item_id => $data_item ) {
    		foreach ( $order_items as $item_id => $item ) {
    			if ( $item instanceof WC_Order_Item_Product && $data_item['name'] === $item->get_name() ) {
    				$discount_price = ( $item->get_subtotal() - $item->get_total() ) / $item->get_quantity();
    				$discount_percent = 100 - ($item->get_total() / $item->get_subtotal() * 100);
    
    				$data['positions'][ $data_item_id ]['discount_percent']  = $discount_percent;
    				$data['positions'][ $data_item_id ]['total_price_gross'] = $item->get_subtotal();
    			}
    		}
    	}
    
    	$data['show_discount'] = 1;
    	$data['discount_kind'] = 'percent_unit';
    
    	return $data;
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment