Home > Software design >  Wordpress, PHP - how to correctly add a function with filters
Wordpress, PHP - how to correctly add a function with filters

Time:01-24

I'm using the WC Vendors Woocommerce plugin that uses the following snippet in order to show 'Product Status' in vendors front-end dashboard:

/**
* Product status text for output on front end.
*/
    public static function product_status( $status ) {
        $product_status = apply_filters(
            'wcv_product_status',
            array(
                'publish' => __( 'Online', 'wcvendors-pro' ),
                'future'  => __( 'Scheduled', 'wcvendors-pro' ),
                'draft'   => __( 'Draft', 'wcvendors-pro' ),
                'pending' => __( 'Pending Approval', 'wcvendors-pro' ),
                'private' => __( 'Admin Only', 'wcvendors-pro' ),
                'trash'   => __( 'Trash', 'wcvendors-pro' ),
            )
        );
        return $product_status[ $status ];
    } // product_status()

I have created an additional custom post status 'Refunded' and I would like to add this custom status to the existing statuses.

So I'm trying to tap into the filter 'wcv_product_status' to create a function that adds my new product status.

My coding knowledge is really poor and I'm just struggling to finish my snippet.

This is what I currently have:

add_filter( 'wcv_product_status', 'refunded_product_status' );
function refunded_product_status( $status ) {
    
        'refunded' => __( 'Refunded', 'wcvendors-pro' );
   
    return $status;
}

I guess I should be using the 'product_status' somewhere in my snippet, but how would I compile the whole thing together?

Any hint is really appreciated.

CodePudding user response:

You've to change the existing array which you received as a parameter and return it.

add_filter( 'wcv_product_status', 'refunded_product_status', 20, 1 ); 
function refunded_product_status( $status ) {  
    $status['refunded'] = __( 'Refunded', 'wcvendors-pro' );  
    return $status;
}
  • Related