Please submit a private ticket if you need to share sensitive information, such as license details and admin credentials.

Okay
  Public Ticket #3099847
Show total price in lists and product page
Closed

Comments

  • Stéphane MIDROUET started the conversation

    Hi,

    I'm using your plugin to add mandatory products to other regular ones in my shop. But I don't know how to show the total price (main product price + "children products" prices) in the list, the product's page or the search results list. Is there a way to do it ?

    Thanks in advance.

  •  767
    Janilyn replied

    Hi Stéphane,

    Thank you for contacting WPClever Support Forum. 

    We’ve received your ticket and assigned one of our developers to help you with that. 

    He’ll be looking into your question and responding with specific instructions if that is possible. 

    Please stay patient as we're having very heavy workloads, it might take some time for our developers to reach back.

    Best regards.

    Janilyn T. - WPClever Support Agent

  •  153
    Henry replied

    Hi,

    We’re receiving too many requests at the moment so it’s a bit overloaded for us. Could you please provide more details (preferably images and links to your published products) to clarify your request/ question?

    Best regards,
    Henry N.

  • Stéphane MIDROUET replied

    Hi,

    Here are the links. The example is "J’APPRENDS LE PIANO TOUT SIMPLEMENT VOL 2 + CD".

    Product page : 
    https://www.feelingmusique.com/categorie/edition-musicale/piano/methodes-piano/japprends-le-piano-tout-simplement-vol-2-cd/

    Category page : 
    https://www.feelingmusique.com/categorie-produit/edition-musicale/piano/methodes-piano/

    Search result : 
    https://www.feelingmusique.com/?s=j%27apprends&post_type=product&product_cat=0

    I'm searching for a way to show to total amount (main product + linked products) on these different pages instead of only the main product's price.

    In this example, the main product's price is 26,40€.
    The linked product's price is 5,00€.
    I want to show the total (31,40€) in the product page, the category list and the search results.

    Note : some products may have more than just one linked products.

  •  153
    Henry replied

    Hi,

    Please add the below custom code (How to add custom code?):

    // Wpclever team support ticket-3099847
    add_action( 'woocommerce_get_price_html', 'woofs_custom_get_price_html', 99, 2 );
    function woofs_custom_get_price_html($price, $product){
        $product_id = $product->get_id();
        if ( $ids = get_post_meta( $product_id, 'woofs_ids', true ) ) {
            $woofs_items = array();
            if ( ! empty( $ids ) ) {
                $items = explode( ',', $ids );
                if ( is_array( $items ) && count( $items ) > 0 ) {
                    foreach ( $items as $item ) {
                        $item_arr      = explode( '/', $item );
                        $woofs_items[] = array(
                            'id'    => apply_filters( 'woofs_item_id', absint( isset( $item_arr[0] ) ? $item_arr[0] : 0 ) ),
                            'price' => isset( $item_arr[1] ) ? preg_replace( '/[^.%0-9]/', '', $item_arr[1] ) : '100%',
                            'qty'   => (float) ( isset( $item_arr[2] ) ? $item_arr[2] : 1 ),
                            'attrs' => isset( $item_arr[3] ) ? (array) json_decode( rawurldecode( $item_arr[3] ) ) : array()
                        );
                    }
                }
            }
            if ( count( $woofs_items ) > 0 ) {
                $regular_price = $product->get_regular_price();
                $sale_price    = $product->get_sale_price();
                foreach ( $woofs_items as $woofs_item ) {
                    $woofs_item_id      = $woofs_item['id'];
                    $woofs_item_price   = $woofs_item['price'];
                    $woofs_item_qty     = $woofs_item['qty'];
                    $woofs_item_product = wc_get_product( $woofs_item_id );
                    if ( ! $woofs_item_product ) {
                        continue;
                    }
                    
                    if ( get_post_meta( $product_id, 'woofs_separately', true ) !== 'on' ) {
                        $woofs_product_price  = $woofs_item_product->get_price();
                        // new price
                        if ( strpos( $woofs_product_price, '%' ) !== false ) {
                            $sale_price += ( ((float) $woofs_product_price * $woofs_item_price ) / 100 ) * (float)$woofs_item_qty;
                        } else {
                            $regular_price += $woofs_product_price * (float)$woofs_item_qty;
                        }
                    }
                }
                $product->set_price( $product->get_price() + $regular_price );
                
                $sale_price = (float)$product->get_sale_price() +  (float)$sale_price;
                if( isset($sale_price) && $sale_price > 0 ) {
                    $product->set_sale_price( $sale_price );
                }
                if ( '' === $product->get_price() ) {
                    $price = apply_filters( 'woocommerce_empty_price_html', '', $product );
                } elseif ( $product->is_on_sale() ) {
                    $price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
                } else {
                    $price = wc_price( wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
                }
            }
        }
        return $price;
    }

    Best regards,
    Henry N.

  • Stéphane MIDROUET replied

    Thanks a lot... but it doesn't work....

    For our test product, it shows a price of 57,80€ instead of 31,40€ .

    I deactivate the code on the website for now until I hear from you.

    Best regards

    NB : 57,80 - 31,40 = 26,40

    It looks like the code is doubling the price of the main product, which is 26,40...

  •  153
    Henry replied

    Please send me your website credentials (wp-admin link, username, password) then I can check and fix it for you. You also can create a dev/staging site with the same configurations then I'll work on it.

    Best regards,
    Henry N.

  •  153
    Henry replied

    Please add the below custom code (How to add custom code?)

    // Wpclever team support ticket-3099847
    add_action( 'woocommerce_get_price_html', 'woofs_custom_get_price_html', 99, 2 );
    function woofs_custom_get_price_html($price, $product){
        $product_id = $product->get_id();
        if ( $ids = get_post_meta( $product_id, 'woofs_ids', true ) ) {
            $woofs_items = array();
            if ( ! empty( $ids ) ) {
                $items = explode( ',', $ids );
                if ( is_array( $items ) && count( $items ) > 0 ) {
                    foreach ( $items as $item ) {
                        $item_arr      = explode( '/', $item );
                        $woofs_items[] = array(
                            'id'    => apply_filters( 'woofs_item_id', absint( isset( $item_arr[0] ) ? $item_arr[0] : 0 ) ),
                            'price' => isset( $item_arr[1] ) ? preg_replace( '/[^.%0-9]/', '', $item_arr[1] ) : '100%',
                            'qty'   => (float) ( isset( $item_arr[2] ) ? $item_arr[2] : 1 ),
                            'attrs' => isset( $item_arr[3] ) ? (array) json_decode( rawurldecode( $item_arr[3] ) ) : array()
                        );
                    }
                }
            }
            if ( count( $woofs_items ) > 0 ) {
                $price      = $product->get_price();
                $sale_price = $product->get_sale_price();
                foreach ( $woofs_items as $woofs_item ) {
                    $woofs_item_id      = $woofs_item['id'];
                    $woofs_item_price   = $woofs_item['price'];
                    $woofs_item_qty     = $woofs_item['qty'];
                    $woofs_item_product = wc_get_product( $woofs_item_id );
                    if ( ! $woofs_item_product ) {
                        continue;
                    }
                    
                    if ( get_post_meta( $product_id, 'woofs_separately', true ) !== 'on' ) {
                        $woofs_product_price  = $woofs_item_product->get_price();
                        // new price
                        if ( strpos( $woofs_product_price, '%' ) !== false ) {
                            $sale_price += ( ((float) $woofs_product_price * $woofs_item_price ) / 100 ) * (float)$woofs_item_qty;
                        } else {
                            $price += $woofs_product_price * (float)$woofs_item_qty;
                        }
                    }
                }
                $product->set_price( $price );
                if( isset($sale_price) && $sale_price > 0 ) {
                    $product->set_sale_price( $sale_price );
                }
                if ( '' === $product->get_price() ) {
                    $price = apply_filters( 'woocommerce_empty_price_html', '', $product );
                } elseif ( $product->is_on_sale() ) {
                    $price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
                } else {
                    $price = wc_price( wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
                }
            }
        }
        return $price;
    }

    Best regards,
    Henry N.

  • Stéphane MIDROUET replied

    Hi,

    It's working fine ! Thanks a lot !

    Stephane

  •  153
    Henry replied

    If you need help from us at any time, feel free to reach out to us. We are always ready to help.

    Best regards,
    Henry N.