skip to Main Content

I have a WooCommerce webshop with different kind of beers from all over the world. If you return a empty bottle you get a deposit of 0,10 or something like that. I made an ACF (Advanced Custom Field) where I can add the deposit price for an empty bottle. I managed to add this to my cart page and display it under the single price of a bottle (0,10) and under the subtotal if there are more bottles (0,40).

This is working in cart and checkout but I want to make a sum off the total deposit money if you return the bottles to the store.

How can I create a function that calculate the sum of all bottles in the cart that has a deposit?

I managed to get the first this working with this functions:

add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );


/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
function add_deposit_value_to_cart_single( $product_price, $values ) {
    $statiegeld = get_field( "statiegeld", $values['product_id']);
    $dep_total = $statiegeld;
    
    if( ! empty( $dep_total ) )
        return $product_price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
    
    return $product_price;
}

/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
function add_deposit_value_to_cart_total( $product_price, $values ) {
    $statiegeld = get_field( "statiegeld", $values['product_id']);
    $dep_total = $statiegeld * $values['quantity'];
    
    if( ! empty( $dep_total ) )
        
        return $product_price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
    
    return $product_price;
}

If someone has an idea I am very grateful because I can’t figure it out.

2

Answers


  1. Chosen as BEST ANSWER

    I changed my whole code to this and this is working. This wil show a deposit in the cart summary under the price and a total deposit if it are more items under the subtotal.

    enter image description here

    Also in the cart total summary it is displaying the depost total of all items and in checkout too.

    enter image description here

    add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
    add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
    
    
    /**
    * Statiegeld toevoegen aan single product
    */
    function add_deposit_value_to_cart_single( $product_price, $values, $cart_item_key ) {
        // Get your custom fields data
        $custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
        //$statiegeld = get_field( "statiegeld", $values['product_id']);
        $dep_total = $custom_field1;
        
        if( ! empty( $dep_total ) )
            return $product_price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
        
        return $product_price;
    }
    
    /**
    * Statiegeld toevoegen aan totaal
    */
    function add_deposit_value_to_cart_total( $product_price, $values, $cart_item_key ) {
        // Get your custom fields data
        $custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
        
        //$custom_field1 = get_field( "statiegeld", $values['product_id']);
        $dep_total = $custom_field1 * $values['quantity'];
        
        if( ! empty( $dep_total ) )
            return $product_price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
        
        return $product_price;
    }
    
    
        // Display Fields using WooCommerce Action Hook
    add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
    
    function woocom_general_product_data_custom_field() {
      // Create a custom text field
        // Number Field
      woocommerce_wp_text_input( 
        array( 
          'id' => '_aantal_statiegeld', 
          'label' => __( 'Statiegeld', 'woocommerce' ), 
          'placeholder' => '', 
          'description' => __( 'Vul het statiegeld hier in', 'woocommerce' ),
          'type' => 'number', 
          'custom_attributes' => array(
             'step' => 'any',
             'min' => '0,10'
          ) 
        )
      );
    
    }
    
    // Hook to save the data value from the custom fields
    add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
    
    /** Hook callback function to save custom fields information */
    function woocom_save_general_proddata_custom_field( $post_id ) {
    
      // Save Number Field
      $number_field = $_POST['_aantal_statiegeld'];
      if( ! empty( $number_field ) ) {
         update_post_meta( $post_id, '_aantal_statiegeld', esc_attr( $number_field ) );
      }
    
    }
    
    add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
    function custom_data_in_product_add_info_tab( $product ) {
    
        //Product ID - WooCommerce compatibility
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        // Get your custom fields data
        $custom_field1 = get_post_meta( $product_id, '_aantal_statiegeld', true );
    
    
        // Set your custom fields labels (or names)
        $label1 = __( 'Statiegeld', 'woocommerce');
    
    
        // The Output
        echo '<h3>'. __('Statiegeld totaal', 'woocommerce') .'</h3>
        <table class="custom-fields-data">
            <tbody>
                <tr class="custom-field1">
                    <th>'. $label1 .'</th>
                    <td>'. $custom_field1 .'</td>
                </tr>
            </tbody>
        </table>';
    }
    
    add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
    add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
    function display_cart_volume_total() {
        $total_volume = 0;
    
        // Loop through cart items and calculate total volume
        foreach( WC()->cart->get_cart() as $cart_item ){
            $product_volume = (float) get_post_meta( $cart_item['product_id'], '_aantal_statiegeld', true );
            $total_volume  += $product_volume * $cart_item['quantity'];
        }
    
        if( $total_volume > 0 ){
    
            // The Output
            echo ' <tr class="cart-total-volume">
                <th>' . __( "Statiegeld totaal", "woocommerce" ) . '</th>
                <td data-title="total-statiegeld">€' . number_format($total_volume, 2, ',', '.') . '</td>
            </tr>';
        }
    }
    

  2. There are some errors with the arguments of both functions.

    Try this:

    /**
    * Statiegeld toevoegen onder single prijs product winkelwagen
    */
    add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
    function add_deposit_value_to_cart_single( $price, $cart_item, $cart_item_key ) {
        $statiegeld = get_field( "statiegeld", $cart_item['product_id']);
        $dep_total = $statiegeld;
        
        if( ! empty( $dep_total ) )
            return $price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
        
        return $price;
    }
    
    /**
    * Statiegeld toevoegen aan subtotaal in winkelwagen
    */
    add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
    function add_deposit_value_to_cart_total( $price, $cart_item, $cart_item_key ) {
        $statiegeld = get_field( "statiegeld", $cart_item['product_id']);
        $dep_total = $statiegeld * $cart_item['quantity'];
    
        if ( ! empty( $dep_total ) ) {
            return $price . '<br /><small class="deposit_label">' .  sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
        }
    
        return $price;
    }
    

    I have tested the code and it works fine. Here is the result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search