Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save plugin-republic/e761bdba36304b067029b7ee60fabe98 to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/e761bdba36304b067029b7ee60fabe98 to your computer and use it in GitHub Desktop.
<?php
/**
* WooCommerce Product Add-Ons Ultimate: show a custom label for specific
* linked products in Products fields (e.g. "Long Traction Bar" -> "Long Bed").
*
* Key = product ID (use the variation ID for a specific variation)
* Value = label the customer sees
*/
function prefix_pewc_child_product_aliases() {
return array(
123 => 'Long Bed',
456 => 'Short Bed',
);
}
/**
* Swap the product title for its alias on the product page.
* Priority 5 runs before the plugin appends the price and stock status (priority 10).
*/
function prefix_pewc_alias_child_product_title( $title, $child_product ) {
if ( ! $child_product instanceof WC_Product ) {
return $title;
}
$aliases = prefix_pewc_child_product_aliases();
$id = $child_product->get_id();
return isset( $aliases[ $id ] ) ? esc_html( $aliases[ $id ] ) : $title;
}
add_filter( 'pewc_child_product_title', 'prefix_pewc_alias_child_product_title', 5, 2 );
/**
* Show the alias in the cart and checkout for child products added by a Products field.
* The order line item keeps the real product name, so fulfilment still sees the actual product.
*/
function prefix_pewc_alias_child_cart_item_name( $name, $cart_item, $cart_item_key ) {
if ( empty( $cart_item['product_extras']['products']['child_field'] ) ) {
return $name;
}
$aliases = prefix_pewc_child_product_aliases();
$id = ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];
return isset( $aliases[ $id ] ) ? esc_html( $aliases[ $id ] ) : $name;
}
add_filter( 'woocommerce_cart_item_name', 'prefix_pewc_alias_child_cart_item_name', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment