Tag Archives: WordPress

Get the min or max variation ID in WooCommerce

if ( ! function_exists( 'wpcodebook_get_variation_id' ) ) {
    /**
     * Get the min or max (by price) active variation ID in WooCommerce.
     */
    function wpcodebook_get_variation_id( $variable_product_id, $min_or_max = 'min' ) {
        $product = wc_get_product( $variable_product_id );
        if ( $product && $product->is_type( 'variable' ) ) {
            $prices        = $product->get_variation_prices( true );
            $variation_ids = array_keys( $prices['price'] );
            return ( 'min' === $min_or_max ? $variation_ids[0] : $variation_ids[ count( $variation_ids ) - 1 ] );
        }
        return false; // not a (variable) product
    }
}

Add "Read more" to Product Short Description in WooCommerce

add_filter( 'woocommerce_short_description', 'wpcb_add_read_more_to_short_description' );
if ( ! function_exists( 'wpcb_add_read_more_to_short_description' ) ) {
    /**
     * wpcb_add_read_more_to_short_description.
     */
    function wpcb_add_read_more_to_short_description( $short_description ) {
        $words     = explode( ' ', $short_description );
        $num_words = 55;
        return ( sizeof( $words ) > $num_words ?
            implode( ' ', array_slice( $words, 0, $num_words ) ) .
                '<details><summary>Read more...</summary>' .
                    implode( ' ', array_slice( $words, $num_words, sizeof( $words ) ) ) . '</details>' :
            $short_description );
    }
}

Hide regular price for products on sale in WooCommerce

add_filter( 'woocommerce_get_price_html', 'wpcb_hide_regular_price_for_sale_products', 10, 2 );
if ( ! function_exists( 'wpcb_hide_regular_price_for_sale_products' ) ) {
    /**
     * wpcb_hide_regular_price_for_sale_products.
     *
     * @see  https://wpcodebook.com/snippets/hide-regular-price-for-products-on-sale-in-woocommerce/
     */
    function wpcb_hide_regular_price_for_sale_products( $price, $product ) {
        if ( $product->is_on_sale() ) {
            if ( $product->is_type( 'variable' ) ) {
                // Variable products
                $prices = $product->get_variation_prices( true );
                if ( ! empty( $prices['price'] ) ) {
                    $min_price     = current( $prices['price'] );
                    $max_price     = end( $prices['price'] );
                    $min_reg_price = current( $prices['regular_price'] );
                    $max_reg_price = end( $prices['regular_price'] );
                    if ( $min_price === $max_price && $min_reg_price === $max_reg_price ) {
                        return wc_price( $min_price ) . $product->get_price_suffix();
                    }
                }
            } elseif ( ! $product->is_type( 'grouped' ) ) {
                // Simple etc. products (i.e. all except grouped)
                return wc_price( wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
            }
        }
        // No changes
        return $price;
    }
}

Hide some product sorting option from the frontend in WooCommerce

if ( ! function_exists( 'wpcb_hide_sorting_option_from_frontend' ) ) {
    /**
     * wpcb_hide_sorting_option_from_frontend.
     *
     * @see https://wpcodebook.com/snippets/hide-some-product-sorting-option-from-the-frontend-in-woocommerce/
     */
    function wpcb_hide_sorting_option_from_frontend( $sorting_options ) {
        if ( isset( $sorting_options['popularity'] ) ) {
            // This will hide "Sort by popularity" option
            // All default WooCommerce sorting options: 'menu_order', 'popularity', 'rating', 'date', 'price', 'price-desc'
            unset( $sorting_options['popularity'] );
        }
        return $sorting_options;
    }
    add_filter( 'woocommerce_catalog_orderby', 'wpcb_hide_sorting_option_from_frontend', PHP_INT_MAX );
}