woocommerce自定义产品的选项卡

在woocommerce的选项卡中,有一个过滤钩子,可以用这个钩子添加、修改、删除、排序我们的产品tabs选项卡。

图片[1]-woocommerce自定义产品的选项卡 - KEKC博客-KEKC博客

添加(默认的代码):

可以看到,添加就是添加一个数组,在里面添加名称、排序、回调函数,回调函数的时候可以配合短代码。

add_filter( 'woocommerce_product_tabs', 'woocommerce_default_product_tabs' );
function woocommerce_default_product_tabs( $tabs = array() ) {
	global $product, $post;

	// Description tab - shows product content.
	if ( $post->post_content ) {
		$tabs['description'] = array(
			'title'    => __( 'Description', 'woocommerce' ),
			'priority' => 10,
			'callback' => 'woocommerce_product_description_tab',
		);
	}

	// Additional information tab - shows attributes.
	if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) {
		$tabs['additional_information'] = array(
			'title'    => __( 'Additional information', 'woocommerce' ),
			'priority' => 20,
			'callback' => 'woocommerce_product_additional_information_tab',
		);
	}

	// Reviews tab - shows comments.
	if ( comments_open() ) {
		$tabs['reviews'] = array(
			/* translators: %s: reviews count */
			'title'    => sprintf( __( 'Reviews (%d)', 'woocommerce' ), $product->get_review_count() ),
			'priority' => 30,
			'callback' => 'comments_template',
		);
	}
	return $tabs;
}
// Custom Product Tabs
function flatsome_custom_product_tabs( $tabs ) {
  global $wc_cpdf;

    // Product video Tab
  if($wc_cpdf->get_value(get_the_ID(), '_product_video_placement') == 'tab'){
      $tabs['ux_video_tab'] = array(
        'title'   => __('Video','flatsome'),
        'priority'  => 10,
        'callback'  => 'flatsome_product_video_tab'
      );
  }

  // Adds the new tab
  if($wc_cpdf->get_value(get_the_ID(), '_custom_tab_title')){
    $tabs['ux_custom_tab'] = array(
      'title'   =>  $wc_cpdf->get_value(get_the_ID(), '_custom_tab_title'),
      'priority'  => 40,
      'callback'  => 'flatsome_custom_tab_content'
    );
  }

  // Custom Global Section
  if(get_theme_mod('tab_title')){
      $tabs['ux_global_tab'] = array(
        'title'   => get_theme_mod('tab_title'),
        'priority'  => 50,
        'callback'  => 'flatsome_global_tab_content'
      );
  }

  // Move review tab to the last position
  //$tabs['reviews']['priority'] = 100;

  return $tabs;
}

add_filter( 'woocommerce_product_tabs', 'flatsome_custom_product_tabs' );

function flatsome_product_video_tab() {
	global $wc_cpdf;
	echo flatsome_apply_shortcode( 'ux_video', array(
		'url' => trim( $wc_cpdf->get_value( get_the_ID(), '_product_video' ) ),
	) );
}

function flatsome_custom_tab_content() {
  // The new tab content
  global $wc_cpdf;
  echo do_shortcode($wc_cpdf->get_value(get_the_ID(), '_custom_tab'));
}

function flatsome_custom_tab_content() {
  // The new tab content
  global $wc_cpdf;
  echo do_shortcode($wc_cpdf->get_value(get_the_ID(), '_custom_tab'));
}

function flatsome_global_tab_content() {
  // The new tab content
  echo do_shortcode(get_theme_mod('tab_content'));
}

修改:

和添加差不多,就是修改现有$tabs数组。

删除:

add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
function wcs_woo_remove_reviews_tab($tabs) {
	unset($tabs['reviews']);
	return $tabs;
}

排序:

通过代码修改priority的值,或者在添加的时候写好数字,代码就不写了。

woocommerce源码中自带的排序代码:

add_filter( 'woocommerce_product_tabs', 'woocommerce_sort_product_tabs', 99 );
function woocommerce_sort_product_tabs( $tabs = array() ) {

	// Make sure the $tabs parameter is an array.
	if ( ! is_array( $tabs ) ) {
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
		trigger_error( 'Function woocommerce_sort_product_tabs() expects an array as the first parameter. Defaulting to empty array.' );
		$tabs = array();
	}

	// Re-order tabs by priority.
	if ( ! function_exists( '_sort_priority_callback' ) ) {
		/**
		 * Sort Priority Callback Function
		 *
		 * @param array $a Comparison A.
		 * @param array $b Comparison B.
		 * @return bool
		 */
		function _sort_priority_callback( $a, $b ) {
			if ( ! isset( $a['priority'], $b['priority'] ) || $a['priority'] === $b['priority'] ) {
				return 0;
			}
			return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
		}
	}

	uasort( $tabs, '_sort_priority_callback' );

	return $tabs;
}
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享