| Server IP : 103.48.194.25 / Your IP : 216.73.216.74 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/phukiencasu.com/public_html/wp-content/plugins/mtw-muticat/ |
Upload File : |
<?php
/*Plugin Name: Muti Category
Plugin URI: https://mythuatweb.com/
Description: Show muti category for products
Author: tritigi@gmail.com
Version: 1.0
Author URI: https://mythuatweb.com/
Text Domain: mtw-muticat
*/
if(!class_exists('mtw_muti_category')) {
class mtw_muti_category
{
function __construct()
{
$name = dirname ( plugin_basename ( __FILE__ ) );
$this->pluginurl = WP_PLUGIN_URL . "/$name/";
$this->pluginpath = WP_PLUGIN_DIR . "/$name/";
add_action('admin_menu', array( &$this,'mtw_muti_category_create_menu'));
add_action( 'wp_footer', array( &$this, 'resiter_scripts_and_styles') );
add_action( 'woocommerce_after_single_product_summary', array(&$this,"get_list_category_from_detail"));
}
function register_mysettings()
{
register_setting( 'csrf_mtw_muticat', 'mtw_muticat_name' );
}
function mtw_muti_category_create_menu()
{
add_menu_page('Muti Category For Product', 'Muti Category', 'administrator', "mtw-muti-category", array(&$this,'mtw_muti_category_settings_page'),"dashicons-menu", 10);
}
//show form page for plugin
function mtw_muti_category_settings_page(){
include $this->pluginpath."mtw-muticat-options.php";
}
//3. Phan hien thi ra bên ngoài
function resiter_scripts_and_styles()
{
wp_register_style( 'muti_category' , $this->pluginurl.'css/style.css');
wp_enqueue_style( 'muti_category' );
}
function get_list_category_from_detail()
{
global $product, $post;
$number_product = get_option( 'mtw_muticat_name' );
$arr_catid = $product->get_category_ids();
foreach ($arr_catid as $catid):
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
//'ignore_sticky_posts' => 1,
'posts_per_page' => $number_product,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $catid,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
),
),
'orderby' => 'rand',
'order' => 'ASC'
);
$products = new WP_Query($args);
//Lay thông tin category
$catname = get_the_category_by_ID($catid);
$catlink = get_category_link($catid);
if ( $products->have_posts() ) :
?>
<section class="relate_products">
<h2><a href="<?php echo $catlink;?>"><?php echo $catname;?></a></h2>
<?php
woocommerce_product_loop_start();
while ($products->have_posts() ) {
$products->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
?>
</section>
<?php
endif;
endforeach;
}
}
}
//load plugin
function mtw_muti_category_load(){
global $muti_category;
$muti_category = new mtw_muti_category();
}
add_action( 'plugins_loaded', 'mtw_muti_category_load' );
?>