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 ); }
Tag Archives: PHP
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; } }