user3515603
user3515603

Reputation: 31

Editing woocommerce booster plugin shortcode

I use a plugin called Booster for woocommerce. I'm using it to build my own product feed. There is a shortcode which returns a whole branch of product categories in this format(string):

"Fruit, Apples, Green Apples"

Unfortunately I need it to return these categories in this format:

"Fruit | Apples | Green Apples"

I found a function for this shortcode in plugin files:

    function wcj_product_categories( $atts ) {
    $return = ( WCJ_IS_WC_VERSION_BELOW_3 ) ? $this->the_product->get_categories() : wc_get_product_category_list( $atts['product_id'] );
    return ( false === $return ) ? '' : $return;
}

And the "wc_get_product_category_list" function from the shortcode code looks like this:

function wc_get_product_category_list( $product_id, $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $product_id, 'product_cat', $before, $sep, $after );

}

I can replace "," for " | " directly in this woocommerce function but I'm afraid it can cause problems in other plugins or woocommerce functions. So what I need is basically editing the plugin function so it gets the category list(string) via "wc_get_product_category_list" and then insert this string into variable and change the "," for "|" but I have no idea how to do this in php.

Sorry for my poor english, hope you get the point. Thanks for help.

//EDIT: Is it possible to do it this way ?

    function wcj_product_categories( $atts ) {
            $product_list = wc_get_product_category_list( $atts['product_id'] );
            $new_string = str_replace(","," |",$product_list);
    $return = ( WCJ_IS_WC_VERSION_BELOW_3 ) ? $this->the_product->get_categories() : $new_string;
    return ( false === $return ) ? '' : $return;
}

Upvotes: 2

Views: 630

Answers (1)

KAGG Design
KAGG Design

Reputation: 1945

Editing plugin files is not a good idea, as your changes will be lost during the update. Instead, create your own shortcode in functions.php:

remove_shortcode( 'wcj_product_categories');
add_shortcode( 'wcj_product_categories', 'my_wcj_product_categories' );

function my_wcj_product_categories( $atts ) {
    $output = wcj_product_categories( $atts );
    $output = str_replace( ",", " |", $output);

    return $output;
}

Upvotes: 1

Related Questions