4

Im beginning to expect that there is some "update function" built into WooCommerce that only lets me rename variations post_title for a little while. And then it gets set back to what the hooks / WooCommerce has decided?

I want to add postfixes like "(Cancelled)" to specific variations programatically.

$new_title = get_the_title( $variationid ) . ' (Cancelled)';
wp_update_post(array('ID' =>$variationid, 'post_title' => $new_title));

This only "hangs on" for a little while...

I tried disabling this hook, and then change the title, but still it gets overwritten.

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );

Is there any way to get WooCommerce to stop overwriting titles of variations?

My solution based on @LoicTheAztec 's answer, using logic based on my custom post status "cancelled".

add_filter( 'woocommerce_product_variation_title', 'filter_product_variation_title_callback', 10, 4 );
function filter_product_variation_title_callback( $variation_title, $product, $title_base, $title_suffix ) {

    $id = $product->get_id();
    $status = get_post_status($id);
    if ($status == 'cancelled'){
        return $variation_title . ' (' . __("Cancelled", "woocommerce") . ')';
    } else {
        return $variation_title;
    }
}

2 Answers2

3

You should try using dedicated woocommerce_product_variation_title filter hook that allows temporarily to alter product variation title (conditionally), this way:

add_filter( 'woocommerce_product_variation_title', 'filter_product_variation_title_callback', 10, 4 );
function filter_product_variation_title_callback( $variation_title, $product, $title_base, $title_suffix ) {
    // Conditional custom field (example)
    if( $product->get_meta('_is_cancelled')  )
        $title_base .= ' (' . __("cancelled", "woocommerce") . ')';

    return $title_base
}

Code goes in function.php file of your active child theme (or active theme). It should works.

Note: The $variation_title returns the product title including product attributes, that is disabled in the function code above…


On order edit pages (it is reflected too):

enter image description here

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • Nice! I made some minor changes to use my custom post status. While I have you here Loic, how do I reflect this change in the edit-order page? – Carl F. Corneil Mar 13 '19 at 19:31
  • @CarlF.Corneil You can add that as an edit in your question may be… The other answer author just added my code in his answer, and that is really not the way. – LoicTheAztec Mar 13 '19 at 19:36
  • @CarlF.Corneil You need to target Order Edit pages… Or does my code does not change the order item title for the product variations in order edit pages? – LoicTheAztec Mar 13 '19 at 19:49
  • It seems this name change is not reflected in existing orders. So im looking for a way to signify that the order item, as seen from edit-order view, is cancelled. It could be as simple as echoing the variations post_status somewhere in the order-item box on the edit order page. So simply put, im probably looking for a hook/filter into the order-item box in woocommerce edit-order pages. – Carl F. Corneil Mar 13 '19 at 19:52
  • For me on my test server it is reflected on order edit pages (on line items). see the screenshot on my answer… – LoicTheAztec Mar 13 '19 at 19:59
  • I think the item's name gets saved in the order on purchase, so that if the item is bought while the postfix is added, it will have the postfix in the order. I want to retroactivly change the item name in the order after the order is done. So adding the postfix to older order items, where the variation did not have the postfix on purchase. – Carl F. Corneil Mar 13 '19 at 21:13
  • @CarlF.Corneil You will need to bulk update the item name from all existing orders in this case… – LoicTheAztec Mar 13 '19 at 21:15
  • 1
    Ok, thank you for your help so far, Ill look into this. – Carl F. Corneil Mar 13 '19 at 21:16
  • 1
    I think I figured it out using some of your other answers Loic. In combination with some of the functions I hav written already. foreach ( $order->get_items() as $item_id => $item ){ $item->set_name($item_name . ' (Cancelled)'); $item->save();} Struggled a bit with the save() -part for a while... – Carl F. Corneil Mar 13 '19 at 23:46
0

The code section in WooCommerce and their filters.

        $should_include_attributes = apply_filters( 'woocommerce_product_variation_title_include_attributes', $should_include_attributes, $product );
        $separator                 = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $product );
        $title_base                = get_post_field( 'post_title', $product->get_parent_id() );
        $title_suffix              = $should_include_attributes ? wc_get_formatted_variation( $product, true, false ) : '';

        return apply_filters( 'woocommerce_product_variation_title', $title_suffix ? $title_base . $separator . $title_suffix : $title_base, $product, $title_base, $title_suffix );




add_filter('woocommerce_product_variation_title', 'change_variation_title_temporary');

function change_variation_title_temporary($variation_title, $product, $title_base, $title_suffix) {

    return $title_base .  ' (Cancelled)';

} 
mujuonly
  • 6,482
  • 5
  • 30
  • 59