وبلاگ

ووکامرس: اضافه کردن توضیحات دوم در دسته بندی محصول

اگه نیاز دارین تا صفحات دسته‌بندی محصول رو برای سئو سایتتون بهبود بدید، نیاز دارین از فیلد “توضیحات” و “بنر” پیش‌فرض  WooCommerce استفاده کنید. اکثر پوسته‌ها، در صورت سازگاری با WooCommerce، این محتوا را در زیر نام دسته‌بندی محصول و بالای محصولات نمایش می‌دهند.

تا اینجا چیز جدیدی نداریم.

اما اگر می‌خواهید یک تکه متن دیگه به زیر محصولات در دسته‌بندی اضافه کنین و در عین حال توضیحات پیش‌فرض را نگه دارین، باید صفحه ویرایش دسته‌بندی رو سفارشی سازی کنیم.

برای این کار اول یک فیلد ویرایشگر متن جدید در بخش ادیت دسته بندی قرار میدیم

بعد قابلیت ذخیره سازی بهش اضافه می‌کنیم

و در نهایت متن مورد نظر رو در جای مورد نظر نمایش میدیم.

برای این کار قطعه کد زیر رو در فایل function.php قالبتون می‌تونین استفاده کنین.

/**
* @snippet اضافه کردن بخش محتوایی جدید به صفحه دسته بندی محصولات ووکامرس
* @other article pournasir.com 
* @compatible WooCommerce 5
*/ 

// ---------------
// 1. نمایش فیلد در بخش افزودن دسته بندی جدید
<?php
add_action( 'product_cat_add_form_fields', 'pournasir_wp_editor_add', 10, 2 );

function pournasir_wp_editor_add() {
?>
<div class="form-field">
<label for="seconddesc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label>

<?php
$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);

wp_editor( '', 'seconddesc', $settings );
?>

<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</div>
<?php
}

// ---------------
// 2. نمایش فیلد در صفحه ادیت دسته بندی محصول

add_action( 'product_cat_edit_form_fields', 'pournasir_wp_editor_edit', 10, 2 );

function pournasir_wp_editor_edit( $term ) {
$second_desc = htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="second-desc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label></th>
<td>
<?php

$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);

wp_editor( $second_desc, 'seconddesc', $settings );
?>

<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</td>
</tr>
<?php
}

// ---------------
// 3. ذخیره سازی فیلد

add_action( 'edit_term', 'pournasir_save_wp_editor', 10, 3 );
add_action( 'created_term', 'pournasir_save_wp_editor', 10, 3 );

function pournasir_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
if ( isset( $_POST['seconddesc'] ) && 'product_cat' === $taxonomy ) {
update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
}
}

// ---------------
// 4. نمایش فیلد در صفحه دسته بندی محصول به کاربر

add_action( 'woocommerce_after_shop_loop', 'pournasir_display_wp_editor_content', 5 );

function pournasir_display_wp_editor_content() {
if ( is_product_taxonomy() ) {
$term = get_queried_object();
if ( $term && ! empty( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) {
echo '<p class="term-description">' . wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) . '</p>';
}
}
}