0

i'm customizing a popular CSV exporting tool for Woocommerce. I've got an issue on extracting names for products. Look at this line of code:

 $product->name = get_the_title($product->ID);

This returns a string like

  Variation #4923 of ProductName

I would like to remove "Variation #Number of " from every export that i make. I do not want to use a regexp. I thought about modyfing the first line of code in :

$temp = new WC_Product($product->ID);
$product->name = $temp->get_title();

so that i could use get_title() which is a native call of Woocommerce. How can i customize the get_title() function, then? Thanks.

1 Answer 1

1

In WooCommerce, the product variants are children of the actual product. So perhaps just check if the $product_id you're on is a child, and if so get the title of the parent.

Untested code:

// Check if product has parent
if($product->post_parent){
    $parents = get_post_ancestors( $product->ID );

    /* Get the top Level page->ID count base 1, array base 0 so -1 */ 
    $parentId = ($parents) ? $parents[count($parents)-1]: $product->ID;

    $product->name = get_the_title($parentId);
}else{
    $product->name = get_the_title($product->ID);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.