I am writing some functions to display a custom field which value will show up after the subcategory titles on the product parent category page (e.g. http:website.com/catalog/parent-category).
The content I need for this custom field is html and shortcodes. So another function I am creating is the shortcode, which looks like this:
[product_attr slug="Straight Drill" label="Straight Drill"]
where I will need to show the link to a filtered-by-product-attributes product page. I want to be able to edit the label in shortcode attributes, but the slug is generated from product attributes' slugs.
This is what I am working with below, I haven't been able to make the shortcode works, and displaying the content below the sub product_cat titles. Any help pointing out the errors will be appreciated!!
// Display product attributes from a product category
global $product;
$current_category = $wp_query->get_queried_object();
$category_id = $current_category->term_id;
$category_name = $current_category->name;
$products = get_posts(array('product_cat' => $category_id));
$term_link = get_term_link( $current_category );
// Extended subscription function with subscription type variable
function create_attribute_shortcode( $atts ) {
// Get product attributes
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
$attribute_name = $attribute['name'];
$attribute_slug = $attribute['slug'];
extract( shortcode_atts( array(
'slug' => $attribute_slug,
'label' => ''), $atts));
$link= '<a href="'. esc_url( $term_link ). '/'. $slug .'">'. $label .'</a>';
return $link;
}
}
add_shortcode( 'product-attr', 'create_attribute_shortcode' );
}
add_action('product_cat_add_form_fields', 'tft_taxonomy_add_new_meta_field', 10, 2);
add_action('product_cat_edit_form_fields', 'tft_taxonomy_edit_meta_field', 10, 2);
//Product Cat Create page
function tft_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label>
<textarea name="tft_meta_desc" id="tft_meta_desc"></textarea>
<p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
</div>
<?php
}
//Product Cat Edit page
function tft_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field.
$tft_meta_desc = get_term_meta($term_id, 'tft_meta_desc', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label></th>
<td>
<textarea name="tft_meta_desc" id="tft_meta_desc"><?php echo esc_attr($tft_meta_desc) ? esc_attr($tft_meta_desc) : ''; ?></textarea>
<p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
</td>
</tr>
<?php
}
add_action('edited_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
// Save extra taxonomy fields callback function.
function tft_save_taxonomy_custom_meta($term_id) {
$tft_meta_desc = filter_input(INPUT_POST, 'tft_meta_desc');
update_term_meta($term_id, 'tft_meta_desc', $tft_meta_desc);
}
// check if page is parent product cat
function has_term_have_children( $term_id = '', $taxonomy = 'product_cat' ) {
// Check if we have a term value, if not, return false
if ( !$term_id )
return false;
// Get term children
$term_children = get_term_children( filter_var( $term_id, FILTER_VALIDATE_INT ), filter_var( $taxonomy, FILTER_SANITIZE_STRING ) );
// Return false if we have an empty array or WP_Error object
if ( empty( $term_children ) || is_wp_error( $term_children ) )
return true;
return false;
}
// Add product attribute lists to underneath product cats
if( is_tax( 'product_cat' ) && has_term_have_children(get_queried_object_id()) ) {
$args = array('child_of' => $termID, 'parent' => $termID);
$termchildren = get_terms( 'product_cat', $args);
add_action( 'woocommerce_after_shop_loop_item_title', 'tft_do_prod_attr_list', 1 );
function tft_do_prod_attr_list() {
$tft_do_meta_desc = get_term_meta($termchildren->term_id, 'tft_meta_desc', true);
$tft_display_meta_desc = do_shortcode($tft_do_meta_desc);
echo '<div class="tft-meta-desc">';
echo $tft_display_meta_desc;
echo '</div>';
}
}