Created
September 5, 2024 13:00
-
-
Save a1iraxa/145ae9bb2ea053277ed8d3c664fab0a1 to your computer and use it in GitHub Desktop.
Add custom fields in WooCommerce Brand and Category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add WYSIWYG editor to product category add/edit form | |
function x2a_add_category_short_description_field() { | |
?> | |
<div class="form-field"> | |
<label for="x2a_short_description"><?php _e('Short Description', 'text-domain'); ?></label> | |
<?php | |
wp_editor('', 'x2a_short_description', array( | |
'textarea_name' => 'x2a_short_description', | |
'textarea_rows' => 5, | |
'media_buttons' => false, | |
'tinymce' => true, | |
'quicktags' => true | |
)); | |
?> | |
</div> | |
<?php | |
} | |
add_action('product_cat_add_form_fields', 'x2a_add_category_short_description_field'); | |
function x2a_edit_category_short_description_field($term) { | |
$short_description = get_term_meta($term->term_id, 'x2a_short_description', true); | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="x2a_short_description"><?php _e('Short Description', 'text-domain'); ?></label></th> | |
<td> | |
<?php | |
wp_editor($short_description, 'x2a_short_description', array( | |
'textarea_name' => 'x2a_short_description', | |
'textarea_rows' => 5, | |
'media_buttons' => false, | |
'tinymce' => true, | |
'quicktags' => true | |
)); | |
?> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action('product_cat_edit_form_fields', 'x2a_edit_category_short_description_field'); | |
// Save custom field value when creating or editing product category | |
function x2a_save_category_short_description_field($term_id) { | |
if (isset($_POST['x2a_short_description'])) { | |
update_term_meta($term_id, 'x2a_short_description', wp_kses_post($_POST['x2a_short_description'])); | |
} | |
} | |
add_action('edited_product_cat', 'x2a_save_category_short_description_field'); | |
add_action('created_product_cat', 'x2a_save_category_short_description_field'); | |
// Add WYSIWYG editor to product brand add/edit form | |
function x2a_add_brand_short_description_field() { | |
?> | |
<div class="form-field"> | |
<label for="x2a_short_description"><?php _e('Short Description', 'text-domain'); ?></label> | |
<?php | |
wp_editor('', 'x2a_short_description', array( | |
'textarea_name' => 'x2a_short_description', | |
'textarea_rows' => 5, | |
'media_buttons' => false, | |
'tinymce' => true, | |
'quicktags' => true | |
)); | |
?> | |
</div> | |
<?php | |
} | |
add_action('product_brand_add_form_fields', 'x2a_add_brand_short_description_field'); | |
function x2a_edit_brand_short_description_field($term) { | |
$short_description = get_term_meta($term->term_id, 'x2a_short_description', true); | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="x2a_short_description"><?php _e('Short Description', 'text-domain'); ?></label></th> | |
<td> | |
<?php | |
wp_editor($short_description, 'x2a_short_description', array( | |
'textarea_name' => 'x2a_short_description', | |
'textarea_rows' => 5, | |
'media_buttons' => false, | |
'tinymce' => true, | |
'quicktags' => true | |
)); | |
?> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action('product_brand_edit_form_fields', 'x2a_edit_brand_short_description_field'); | |
// Save custom field value when creating or editing product brand | |
function x2a_save_brand_short_description_field($term_id) { | |
if (isset($_POST['x2a_short_description'])) { | |
update_term_meta($term_id, 'x2a_short_description', wp_kses_post($_POST['x2a_short_description'])); | |
} | |
} | |
add_action('edited_product_brand', 'x2a_save_brand_short_description_field'); | |
add_action('created_product_brand', 'x2a_save_brand_short_description_field'); | |
// Function to show an archive description on taxonomy archives | |
if (!function_exists('woocommerce_taxonomy_archive_description')) { | |
function woocommerce_taxonomy_archive_description() { | |
if (is_product_taxonomy() && 0 === absint(get_query_var('paged'))) { | |
$term = get_queried_object(); | |
if ($term) { | |
$term_description = apply_filters('woocommerce_taxonomy_archive_description_raw', $term->description, $term); | |
if (!empty($term_description)) { | |
echo '<div class="term-description x2a-updated">' . wc_format_content(wp_kses_post($term_description)) . '</div>'; | |
} | |
$short_description = apply_filters('woocommerce_taxonomy_archive_description_raw_short', get_term_meta($term->term_id, 'x2a_short_description', true), $term); | |
if (!empty($short_description)) { | |
echo '<div class="term-description term-description--short x2a-updated">' . wc_format_content(wp_kses_post($short_description)) . '</div>'; | |
echo '<a href="#" class="term-description--toggle">show more</a>'; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment