Tag Archives: WordPress

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

Hide product description for non-logged users in WooCommerce

if ( ! function_exists( 'wpcb_hide_product_description_for_non_logged_users' ) ) {
    /*
     * wpcb_hide_product_description_for_non_logged_users.
     *
     * @see https://wpcodebook.com/snippets/hide-product-description-for-non-logged-users-in-woocommerce/
     */
    function wpcb_hide_product_description_for_non_logged_users( $content ) {
        return ( is_product() && ! is_user_logged_in() ? 'You must be logged in to see the product description.' : $content );
    }
    add_filter( 'the_content', 'wpcb_hide_product_description_for_non_logged_users', PHP_INT_MAX );
}

Add screen to WooCommerce screen IDs

add_filter( 'woocommerce_screen_ids', 'wpcb_add_screen_to_wc_screen_ids' );
if ( ! function_exists( 'wpcb_add_screen_to_wc_screen_ids' ) ) {
    /**
     * wpcb_add_screen_to_wc_screen_ids.
     *
     * @see https://wpcodebook.com/snippets/add-screen-to-woocommerce-screen-ids/
     */
    function wpcb_add_screen_to_wc_screen_ids( $screen_ids ) {
        $screen_ids[] = 'your-screen-id'; // to get your screen ID call `$your_screen = get_current_screen()`, then get `$your_screen->id`
        return $screen_ids;
    }
}

Add product min, max and step quantity columns to admin products list in WooCommerce

add_filter( 'manage_edit-product_columns', 'wpcb_add_product_qty_columns' );
if ( ! function_exists( 'wpcb_add_product_qty_columns' ) ) {
    /**
     * Add qty columns to products list.
     */
    function wpcb_add_product_qty_columns( $columns ) {
        $columns['wpcb_min_qty']  = 'Min Qty';
        $columns['wpcb_max_qty']  = 'Max Qty';
        $columns['wpcb_qty_step'] = 'Qty Step';
        return $columns;
    }
}

add_action( 'manage_product_posts_custom_column', 'wpcb_render_product_qty_columns' );
if ( ! function_exists( 'wpcb_render_product_qty_columns' ) ) {
    /**
     * Output qty columns.
     */
    function wpcb_render_product_qty_columns( $column ) {
        switch ( $column ) {
            case 'wpcb_min_qty':
                echo apply_filters( 'woocommerce_quantity_input_min',  1, wc_get_product() );
                break;
            case 'wpcb_max_qty':
                echo apply_filters( 'woocommerce_quantity_input_max',  1, wc_get_product() );
                break;
            case 'wpcb_qty_step':
                echo apply_filters( 'woocommerce_quantity_input_step', 1, wc_get_product() );
                break;
        }
    }
}