Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active November 21, 2023 09:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/317b77c110016b1a3b3d to your computer and use it in GitHub Desktop.
Save SiR-DanieL/317b77c110016b1a3b3d to your computer and use it in GitHub Desktop.
Make product attributes linkable
<?php
/**
* Register term fields for WooCommerce attributes.
*/
add_action( 'init', 'register_attributes_url_meta' );
function register_attributes_url_meta() {
$attributes = wc_get_attribute_taxonomies();
foreach ( $attributes as $tax ) {
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
add_action( "{$name}_add_form_fields", 'add_attribute_url_meta_field' );
add_action( "{$name}_edit_form_fields", 'edit_attribute_url_meta_field', 10 );
add_action( "edited_{$name}", 'save_attribute_url' );
add_action( "create_{$name}", 'save_attribute_url' );
}
}
/**
* Add URL field to the new attribute term form.
*/
function add_attribute_url_meta_field() {
wp_nonce_field( basename( __FILE__ ), 'attribute_url_meta_nonce' );
?>
<div class="form-field">
<label for="attribute_url"><?php esc_html_e( 'URL', 'woocommerce' ); ?></label>
<input type="url" name="attribute_url" id="attribute_url" value="" />
</div>
<?php
}
/**
* Add URL field to the edit attribute term form.
*/
function edit_attribute_url_meta_field( $term ) {
$url = get_term_meta( $term->term_id, 'attribute_url', true );
wp_nonce_field( basename( __FILE__ ), 'attribute_url_meta_nonce' );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="attribute_url"><?php esc_html_e( 'URL', 'woocommerce' ); ?></label></th>
<td>
<input type="url" name="attribute_url" id="attribute_url" value="<?php echo esc_url( $url ); ?>" />
</td>
</tr>
<?php
}
/**
* Save URL field for attribute terms.
*/
function save_attribute_url( $term_id ) {
if ( ! isset( $_POST['attribute_url'], $_POST['attribute_url_meta_nonce'] ) ||
! wp_verify_nonce( $_POST['attribute_url_meta_nonce'], basename( __FILE__ ) ) ) {
return;
}
$new_url = esc_url_raw( $_POST['attribute_url'] );
update_term_meta( $term_id, 'attribute_url', $new_url );
}
/**
* Filter to make product attributes linkable.
*/
add_filter( 'woocommerce_attribute', 'make_product_atts_linkable', 10, 3 );
function make_product_atts_linkable( $text, $attribute, $values ) {
$new_values = array();
foreach ( $values as $value ) {
if ( $attribute['is_taxonomy'] ) {
// Handle global attributes
$term = get_term_by( 'name', $value, $attribute['name'] );
if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
$url = get_term_meta( $term->term_id, 'attribute_url', true );
if ( ! empty( $url ) ) {
$new_values[] = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( $value ) . '">' . esc_html( $value ) . '</a>';
} else {
$new_values[] = esc_html( $value );
}
}
} else {
// Handle local attributes (Markdown syntax)
if ( preg_match( "/\[(.*?)\]\((.*?)\)/", $value, $matches ) ) {
$link_text = esc_html( $matches[1] );
$link_url = strip_tags( $matches[2] ); // Directly use the URL from the match
// Continue with your existing logic
if ( filter_var( $link_url, FILTER_VALIDATE_URL ) ) {
$new_values[] = '<a href="' . esc_url( $link_url ) . '">' . $link_text . '</a>';
} else {
// If the URL isn't valid, just use the text
$new_values[] = $link_text;
}
}
}
}
return implode( ', ', $new_values );
}
@shimarrr
Copy link

Hi
I use from this code and work correctly. but show a Notice in top of the every product in single page. That Notice is:
"Notice: Trying to get property 'term_id' of non-object in /home/micromod/domains/micmodshop.ir/public_html/wp-content/themes/avada-child/functions.php on line 155"
How can i fixed it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment