The best solution for implementing new custom stock status I implement on shop.brisk-ict.nl is:

  1. Adding a new stock status in the front end
  2. Adding the new stock status to the website back end
  3. Hide the add to cart button

The steps are as below:

  1. Adding the code below to functions.php. Note that because it is added to the functions.php file, every time you update the theme or WordPress core, this code should be implemented again because the new update will be replaced with the previous version of the core files. You can find the functions.php file under your WordPress theme folder.
// Add new stock status options

function filter_woocommerce_product_stock_status_options( $status ) {

// Add new statuses

$status[‘contact_us’] = __( ‘Niet op voorraad, neem contact met ons op voor levertijd’, ‘woocommerce’ );

 

return $status;

}

add_filter( ‘woocommerce_product_stock_status_options’, ‘filter_woocommerce_product_stock_status_options’, 10, 1 );

 

// Availability text

function filter_woocommerce_get_availability_text( $availability, $product ) {

switch( $product->get_stock_status() ) {

case ‘contact_us’:

$availability = __( ‘Niet op voorraad, neem contact met ons op voor levertijd’, ‘woocommerce’ );

break;

}

 

return $availability;

}

add_filter( ‘woocommerce_get_availability_text’, ‘filter_woocommerce_get_availability_text’, 10, 2 );

 

// Availability class

function filter_woocommerce_get_availability_class( $class, $product ) {

switch( $product->get_stock_status() ) {

case ‘contact_us’:

$class = ‘contact-us’;

braeak;

}

 

return $class;

 

 

}

add_filter( ‘woocommerce_get_availability_class’, ‘filter_woocommerce_get_availability_class’, 10, 2 );

2- To add the new custom status to the backend, use the code below:

// admin stock html

function filter_woocommerce_admin_stock_html( $stock_html, $product ) {

switch( $product->get_stock_status() ) {

case ‘contact_us’:

$stock_html = ‘<mark class="contact-us" style="background:transparent none;color:#33ffcc;font-weight:700;line-height:1;">’ . __( ‘Niet op voorraad, neem contact met ons op voor levertijd’, ‘woocommerce’ ) . ‘</mark>’;

break;

}

&nbsp;

return $stock_html;

}

add_filter( ‘woocommerce_admin_stock_html’, ‘filter_woocommerce_admin_stock_html’, 10, 2 );

3- Use the CSS code below to hide the Add to cart button when the new custom stock status is shown:

.stock.contact-us + form{display: none !important;}

Note that for me it was like this:

<p  class=”stock contact-us”> Please contact us</p>

<form class=”cart customize-unpreviewable” action=https://test method=”post” enctype=”multipart/form-data”>

To address a class within another class I have used + and it worked. In this case if I change the status to in stock, the Add to cart button will show again.

You can also change the font color also using the CSS code below:

.stock.contact-us {color: #DE0000 !important; font-weight: 400;}

Tagged: