<?php
/*@MDN*/
// Ẩn top bar admin
// function hide_admin_bar(){ return false; }
// add_filter( 'show_admin_bar', 'hide_admin_bar' );
add_filter('flatsome_lightbox_close_btn_inside', '__return_true');
// Thêm các file css, js
function add_theme_scripts_client() {
$ver = "1.1";
wp_enqueue_style('custom-css', get_template_directory_uri() . '-child' . '/customs/style.css', array(), $ver, 'all');
}
add_action('wp_enqueue_scripts', 'add_theme_scripts_client');
// Button trang chi tiết sản phẩm
function add_button_product_detail() {
$html = '';
if (is_singular('product')) {
$linkDownload = get_field('link_download_hdsd') ? get_field('link_download_hdsd') : '#popup-form';
$html = '<div id="button-product-detail">
<a href="#popup-form" class="button alert is-larger lowercase animate-scale" style="border-radius: 100px;"><span>Nhận báo giá</span></a>
<a href="'.$linkDownload.'" class="button is-larger lowercase animate-scale disable-color" style="border-radius: 100px;"><span>Hướng dẫn sử dụng</span></a>
</div>';
}
echo $html;
}
add_action('flatsome_custom_single_product_1', 'add_button_product_detail');
// Config số lượng sản phẩm liên quan
function change_number_related_products( $args ) {
$args['posts_per_page'] = 4;
return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'change_number_related_products', 20 );
// Custom section sản phẩm liên quan
function custom_related_product() {
$html = '';
if (is_singular('product')) {
global $product;
$link = $product->get_permalink();
$sku = $product->get_sku() ? $product->get_sku() : 'N/A';
$category = get_the_terms( $product->get_id(), 'product_cat' ) ? get_the_terms( $product->get_id(), 'product_cat' )[0]->name : 'N/A';
$size = $product->get_attribute( 'kich-co' ) ? $product->get_attribute( 'kich-co' ) : 'N/A';
$excaly = $product->get_attribute( 'do-chinh-xac' ) ? $product->get_attribute( 'do-chinh-xac' ) : 'N/A' ;
$html = '<div class="button-wrapper">
<a href="'.$link.'" class="button alert is-larger lowercase" style="border-radius: 100px;"><span>Xem thêm</span></a>
</div>
<ul class="config-wrapper">
<li>
<span class="icon"><img src="'.home_url() .'/' . 'wp-content/uploads/2021/07/point.png'.'" alt=""></span>
<span class=config-value>Models: '.$sku.'</span>
</li>
<li>
<span class="icon"><img src="'.home_url() .'/' . 'wp-content/uploads/2021/07/point.png'.'" alt=""></span>
<span class=config-value>'.$category.'</span>
</li>
<li>
<span class="icon"><img src="'.home_url() .'/' . 'wp-content/uploads/2021/07/point.png'.'" alt=""></span>
<span class=config-value>Kích cỡ: '.$size.'</span>
</li>
<li>
<span class="icon"><img src="'.home_url() .'/' . 'wp-content/uploads/2021/07/point.png'.'" alt=""></span>
<span class=config-value>Độ chính xác: '.$excaly.'</span>
</li>
</ul>
';
}
echo $html;
}
add_action('flatsome_product_box_after','custom_related_product');
// Remove tab đánh giá trong chi tiết sản phẩm
function remove_product_tabs( $tabs ) {
if (is_singular('product')) {
unset( $tabs['reviews'] );
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 );
// Add thêm tab trong chi tiết sản phẩm
function add_new_product_tab( $tabs ) {
if (is_singular('product')) {
$tabs['configs_tab'] = array(
'title' => __( 'Thông số kỹ thuật', 'woocommerce' ),
'priority' => 50,
'callback' => 'add_content_configs_tab'
);
$tabs['certifications_tab'] = array(
'title' => __( 'Chứng chỉ sản phẩm', 'woocommerce' ),
'priority' => 50,
'callback' => 'add_content_certifications_tab'
);
$tabs['accessaries_tab'] = array(
'title' => __( 'Phụ kiện', 'woocommerce' ),
'priority' => 50,
'callback' => 'add_content_accessaries_tab'
);
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'add_new_product_tab' );
// Tab thông số kĩ thuật
function add_content_configs_tab() {
$content = get_field('configs_tab_detail');
if ($content) {
echo $content;
} else {
echo "<p>Đang cập nhật...</p>";
}
}
// Tab chứng chỉ
function add_content_certifications_tab() {
$images = get_field('certifications_tab_img');
$html = '';
if ($images) {
$html = '<ul id="tab-cretification-img">';
foreach( $images as $img ) {
$html .= '<li><img src="'.esc_url($img['sizes']['thumbnail']).'" alt="'.esc_attr($img['alt']).'" /></li>';
}
$html .= '</ul>';
} else {
echo "<p>Đang cập nhật...</p>";
}
echo $html;
}
// Tab phụ kiện
function add_content_accessaries_tab() {
$content = get_field('accessaries_tab_detail');
if ($content) {
echo $content;
} else {
echo "<p>Đang cập nhật...</p>";
}
}
// Tiêu đề trang danh mục sản phẩm
function add_title_category_name() {
$title = '';
// Lấy info danh mục
global $wp_query;
$cat = $wp_query->get_queried_object();
if (is_product_category() && $cat->name) {
$title = '<h2 id="category-name" class="text-center">'.$cat->name.'</h2>';
}
echo $title;
}
add_action('woocommerce_before_main_content','add_title_category_name');
// Lấy mã sản phẩm trang danh mục
function get_sku_category_product() {
$html = '';
if (is_product_category() || is_shop()) {
global $product;
$title = $product->get_sku() ? $product->get_sku() : $product->get_title();
$link = $product->get_permalink();
$html = '<div class="title-wrapper custom-title">
<p class="name product-title woocommerce-loop-product__title">
<a href="'.$link.'" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">'.$title.'</a>
</p>
</div>';
}
echo $html;
}
add_action('flatsome_product_box_after','get_sku_category_product');
// Meta description for posts + WooCommerce products
function zoyi_meta_description() {
if (is_singular()) {
global $post;
$desc = trim(wp_strip_all_tags(get_the_excerpt($post)));
if (empty($desc)) {
$desc = trim(wp_strip_all_tags(wp_trim_words($post->post_content, 30, '...')));
}
if (!empty($desc)) {
echo '<meta name="description" content="' . esc_attr($desc) . '">' . "
";
}
}
}
add_action('wp_head', 'zoyi_meta_description', 1);
// === SEO: Organization Schema + WebSite Schema + BreadcrumbList Schema ===
function zoyi_seo_schema() {
// Organization Schema
$org = array(
'@context' => 'https://schema.org',
'@type' => 'Organization',
'name' => 'Zoyi Việt Nam',
'url' => 'https://zoyi.vn',
'logo' => 'https://zoyi.vn/wp-content/uploads/2021/08/favicon.png',
'description' => 'Đồng hồ vạn năng và thiết bị đo lường điện chính hãng chất lượng cao',
'address' => array(
'@type' => 'PostalAddress',
'addressCountry' => 'VN',
),
'sameAs' => array(
'https://www.facebook.com/zoyi.vietnam',
),
);
// WebSite Schema with SearchAction
$website = array(
'@context' => 'https://schema.org',
'@type' => 'WebSite',
'name' => 'Zoyi Việt Nam',
'url' => 'https://zoyi.vn',
'potentialAction' => array(
'@type' => 'SearchAction',
'target' => 'https://zoyi.vn/?s={search_term_string}',
'query-input' => 'required name=search_term_string',
),
);
echo '<script type="application/ld+json">' . json_encode($org, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
echo '<script type="application/ld+json">' . json_encode($website, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
// BreadcrumbList Schema for single posts/products
if (is_singular()) {
global $post;
$breadcrumb = array(
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => array(),
);
// Home
$breadcrumb['itemListElement'][] = array(
'@type' => 'ListItem',
'position' => 1,
'name' => 'Trang chủ',
'item' => 'https://zoyi.vn',
);
// Blog or Product category
if (is_product()) {
$breadcrumb['itemListElement'][] = array(
'@type' => 'ListItem',
'position' => 2,
'name' => 'Sản phẩm',
'item' => 'https://zoyi.vn/products/',
);
} elseif (is_single()) {
$breadcrumb['itemListElement'][] = array(
'@type' => 'ListItem',
'position' => 2,
'name' => 'Blog',
'item' => 'https://zoyi.vn/blog/',
);
}
// Current page
$breadcrumb['itemListElement'][] = array(
'@type' => 'ListItem',
'position' => count($breadcrumb['itemListElement']) + 1,
'name' => get_the_title(),
'item' => get_permalink(),
);
echo '<script type="application/ld+json">' . json_encode($breadcrumb, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
}
}
add_action('wp_head', 'zoyi_seo_schema', 5);
// === SEO: Article Schema for blog posts ===
function zoyi_article_schema() {
if (is_single() && !is_product()) {
global $post;
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'Article',
'headline' => get_the_title(),
'url' => get_permalink(),
'datePublished' => get_the_date('c'),
'dateModified' => get_the_modified_date('c'),
'author' => array(
'@type' => 'Organization',
'name' => 'Zoyi Việt Nam',
'url' => 'https://zoyi.vn',
),
'publisher' => array(
'@type' => 'Organization',
'name' => 'Zoyi Việt Nam',
'logo' => array(
'@type' => 'ImageObject',
'url' => 'https://zoyi.vn/wp-content/uploads/2021/08/favicon.png',
),
),
'description' => trim(wp_strip_all_tags(get_the_excerpt($post))),
);
if (has_post_thumbnail()) {
$schema['image'] = array(
'@type' => 'ImageObject',
'url' => get_the_post_thumbnail_url($post, 'full'),
);
}
echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
}
}
add_action('wp_head', 'zoyi_article_schema', 6);
// === 7. Open Graph + Twitter Card Tags ===
function zoyi_og_tags() {
if (is_singular()) {
global $post;
$title = get_the_title();
$desc = trim(wp_strip_all_tags(get_the_excerpt($post)));
if (empty($desc)) {
$desc = trim(wp_strip_all_tags(wp_trim_words($post->post_content, 30, '...')));
}
$url = get_permalink();
$image = has_post_thumbnail() ? get_the_post_thumbnail_url($post, 'large') : 'https://zoyi.vn/wp-content/uploads/2021/08/favicon.png';
echo '<meta property="og:type" content="' . (is_product() ? 'product' : 'article') . '">' . "\n";
echo '<meta property="og:title" content="' . esc_attr($title) . '">' . "\n";
echo '<meta property="og:description" content="' . esc_attr($desc) . '">' . "\n";
echo '<meta property="og:url" content="' . esc_url($url) . '">' . "\n";
echo '<meta property="og:image" content="' . esc_url($image) . '">' . "\n";
echo '<meta property="og:site_name" content="Zoyi Vietnam">' . "\n";
echo '<meta name="twitter:card" content="summary_large_image">' . "\n";
echo '<meta name="twitter:title" content="' . esc_attr($title) . '">' . "\n";
echo '<meta name="twitter:description" content="' . esc_attr($desc) . '">' . "\n";
} elseif (is_front_page()) {
echo '<meta property="og:type" content="website">' . "\n";
echo '<meta property="og:title" content="Zoyi Việt Nam - Đồng Hồ Vạn Năng Chính Hãng">' . "\n";
echo '<meta property="og:description" content="Zoyi.vn - Đồng hồ vạn năng và thiết bị đo lường điện chính hãng chất lượng cao.">' . "\n";
echo '<meta property="og:url" content="https://zoyi.vn/">' . "\n";
echo '<meta property="og:image" content="https://zoyi.vn/wp-content/uploads/2021/08/favicon.png">' . "\n";
echo '<meta property="og:site_name" content="Zoyi Vietnam">' . "\n";
}
}
add_action('wp_head', 'zoyi_og_tags', 2);
// === 8. Title Fix for Flatsome Theme ===
function zoyi_fix_title($title_parts) {
if (is_front_page() || is_home()) {
$title_parts['title'] = 'Zoyi Việt Nam — Đồng Hồ Vạn Năng Chính Hãng, Giá Tốt';
} elseif (is_single()) {
$title_parts['title'] = get_the_title() . ' | Zoyi Vietnam';
} elseif (is_page()) {
$title_parts['title'] = get_the_title() . ' | Zoyi Vietnam';
}
return $title_parts;
}
add_filter('document_title_parts', 'zoyi_fix_title', 20);
// === 9. FAQPage Schema for posts with FAQ sections ===
function zoyi_faq_schema() {
if (is_single()) {
global $post;
$content = $post->post_content;
if (strpos($content, 'Câu hỏi thường gặp') !== false || strpos($content, 'FAQ') !== false) {
preg_match_all('/<h[23][^>]*>(.*?)<\/h[23]>\s*(?:<p[^>]*>)?(.*?)(?=<h[23]|<\/div|$)/si', $content, $matches, PREG_SET_ORDER);
$items = array();
foreach ($matches as $m) {
$q = trim(strip_tags($m[1]));
$a = trim(strip_tags($m[2]));
if (strlen($q) > 10 && strlen($a) > 20 && (strpos($q, '?') !== false || stripos($q, 'là gì') !== false || stripos($q, 'cách') !== false)) {
$items[] = array('@type' => 'Question', 'name' => $q, 'acceptedAnswer' => array('@type' => 'Answer', 'text' => $a));
}
}
if (count($items) >= 2) {
$faq = array('@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => array_slice($items, 0, 10));
echo '<script type="application/ld+json">' . json_encode($faq, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
}
}
}
}
add_action('wp_head', 'zoyi_faq_schema', 7);
// === 8. Open Graph Tags ===
function zoyi_og_tags() {
if (is_singular()) {
global $post;
$title = get_the_title();
$desc = trim(wp_strip_all_tags(get_the_excerpt($post)));
if (empty($desc)) {
$desc = trim(wp_strip_all_tags(wp_trim_words($post->post_content, 30, '...')));
}
$url = get_permalink();
$image = has_post_thumbnail() ? get_the_post_thumbnail_url($post, 'large') : 'https://zoyi.vn/wp-content/uploads/2021/08/favicon.png';
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
} elseif (is_front_page()) {
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
}
}
add_action('wp_head', 'zoyi_og_tags', 2);
// === 9. Title Fix for Flatsome ===
function zoyi_fix_title($title_parts) {
if (is_front_page() || is_home()) {
$title_parts['title'] = 'Zoyi Việt Nam - Đồng Hồ Vạn Năng Chính Hãng, Giá Tốt';
} elseif (is_single()) {
$title_parts['title'] = get_the_title() . ' | Zoyi Vietnam';
} elseif (is_page()) {
$title_parts['title'] = get_the_title() . ' | Zoyi Vietnam';
}
return $title_parts;
}
add_filter('document_title_parts', 'zoyi_fix_title', 20);