Home New Guten Forums WoodMart support forum What is hook or filter to show only on single product

What is hook or filter to show only on single product

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #660711

    Muamer Lapandić
    Participant

    I have function:

    /**
     Calculate savings for single product
     */
    add_filter('woocommerce_get_price_html', 'simple_product_saving_amount_as_variation', 10, 2);
    function simple_product_saving_amount_as_variation($price_html, $product) {
        if (!is_product()) {
            return $price_html;
        }
        if ($product->is_type('simple') && $product->is_on_sale()) {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
            if (!$regular_price || !$sale_price) {
                return $price_html;
            }
            $saved_amount = $regular_price - $sale_price;
            $saving_html = sprintf('<p id="saving_total_price"><span class="zh-saving-text">%s %s</span></p>', __('Ušteda:'), wc_price($saved_amount));
            $price_html.= '<br>' . $saving_html;
        }
        return $price_html;
    }

    As we are using woocommerce_get_price_html it is also affecting related products, lastly viewed products and so on.
    Is there a way, hook or something I can limit it this way only on single product page and not in any elementor widgets or anything else?

    PS. I have seen someone also asked how to calculate difference, so you can use this code as function in your theme.

    #660712

    Muamer Lapandić
    Participant

    This is for variable product

    /**
     Calculate savings for variable product
     */
    add_action('woocommerce_available_variation', 'product_variation_saving_amount', 10, 3);
    function product_variation_saving_amount($variation_data, $product, $variation) {
        if (!is_product()) {
            return $variation_data;
        }
        if ($variation->is_on_sale()) {
            $args = array('price' => $variation->get_regular_price());
            if ('incl' === get_option('woocommerce_tax_display_shop')) {
                $regular_price = wc_get_price_including_tax($variation, $args);
                $active_price = wc_get_price_including_tax($variation);
            } else {
                $regular_price = wc_get_price_excluding_tax($variation, $args);
                $active_price = wc_get_price_excluding_tax($variation);
            }
            $saved_amount = $regular_price - $active_price;
            $saving_html = sprintf('<p id="saving_total_price"><span class="zh-saving-text">%s %s</span></p>', __('Ušteda:'), wc_price($saved_amount));
            $variation_data['price_html'].= '<br>' . $saving_html;
        }
        return $variation_data;
    }
    #660714

    Muamer Lapandić
    Participant

    Here is what is happening with simple product prize with discount in attachments, where it should be show and where not….
    Where not is more than this, so I need filter, action or anything that will keep this code onl yon single product page.

    Attachments:
    You must be logged in to view attached files.
    #661051

    Hung Pham
    Keymaster

    Hi muamer.lapandic.cn,

    Thanks for reaching to us.

    Unfortunately, it requires customization and out of our basic support. Thanks for understanding our limitations.

    Regards,

Viewing 4 posts - 1 through 4 (of 4 total)