RSS feed for this section

Archive | Snippets

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;
    }
}

Check if two arrays values are equal in PHP

if ( ! function_exists( 'wpcb_are_arrays_equal' ) ) {
    /**
     * wpcb_are_arrays_equal.
     *
     * @see https://wpcodebook.com/snippets/check-if-two-arrays-values-are-equal-in-php/
     */
    function wpcb_are_arrays_equal( $array1, $array2 ) {
        if ( count( $array1 ) != count( $array2 ) ) {
            return false;
        }
        $diff = array_diff( $array1, $array2 );
        return empty( $diff );
    }
}

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 );
}

Replace price range with lowest price for variable products in WooCommerce

if ( ! function_exists( 'wpcb_display_lowest_price_for_variable_products' ) ) {
    /**
     * wpcb_display_lowest_price_for_variable_products.
     *
     * @see https://wpcodebook.com/snippets/replace-price-range-with-lowest-price-for-variable-products-in-woocommerce/
     */
    function wpcb_display_lowest_price_for_variable_products( $price_html, $product ) {
        return ( $product->get_variation_price( 'max' ) != ( $min_price = $product->get_variation_price( 'min' ) ) ?
            sprintf( 'From: %s', wc_price( $min_price ) ) : $price_html );
    }
    add_filter( 'woocommerce_variable_price_html', 'wpcb_display_lowest_price_for_variable_products', PHP_INT_MAX, 2 );
}

Cancel sale for low stock products in WooCommerce

if ( ! function_exists( 'wpcb_is_product_low_stock' ) ) {
    /*
     * wpcb_is_product_low_stock.
     *
     * @see https://wpcodebook.com/snippets/cancel-sale-for-low-stock-products-in-woocommerce/
     */
    function wpcb_is_product_low_stock( $_product ) {
        return ( $_product->managing_stock() && $_product->get_stock_quantity() <= wc_get_low_stock_amount( $_product ) );
    }
}

if ( ! function_exists( 'wpcb_set_product_regular_price_on_low_stock' ) ) {
    /*
     * wpcb_set_product_regular_price_on_low_stock.
     */
    function wpcb_set_product_regular_price_on_low_stock( $price, $_product ) {
        return ( wpcb_is_product_low_stock( $_product ) ? $_product->get_regular_price() : $price );
    }
    add_filter( 'woocommerce_product_get_price', 'wpcb_set_product_regular_price_on_low_stock', PHP_INT_MAX, 2 );
}

if ( ! function_exists( 'wpcb_cancel_product_sale_on_low_stock' ) ) {
    /*
     * wpcb_cancel_product_sale_on_low_stock.
     */
    function wpcb_cancel_product_sale_on_low_stock( $is_on_sale, $_product ) {
        return ( wpcb_is_product_low_stock( $_product ) ? false : $is_on_sale );
    }
    add_filter( 'woocommerce_product_is_on_sale', 'wpcb_cancel_product_sale_on_low_stock', PHP_INT_MAX, 2 );
}