D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
danluatbos
/
domains
/
danluatbook.com
/
public_html
/
wp-content
/
themes
/
megastore
/
Filename :
functions.php
back
Copy
<?php /** * megastore functions and definitions * * @version 1.0 * * @date 12.08.2015 */ add_filter('use_block_editor_for_post', '__return_false'); load_theme_textdomain( 'megastore', get_template_directory() . '/languages' ); /** * Set the content width based on the theme's design and stylesheet. */ if ( ! isset( $content_width ) ) { $content_width = 640; /* pixels */ } require_once( trailingslashit( get_template_directory() ). '/7upframe/function/function.php' ); require_once( trailingslashit( get_template_directory() ). '/7upframe/config/config.php' ); require_once( trailingslashit( get_template_directory() ). '/7upframe/index.php' ); // pendaica // remove menu admin function remove_menus(){ // remove_menu_page( 'plugins.php' ); remove_menu_page( 'themepunch-google-fonts' ); remove_menu_page( 'tools.php' ); remove_menu_page( 'options-general.php?page=tinymce-advanced' ); } add_action( 'admin_menu', 'remove_menus', 999 ); // remove sub menu admin function remove_submenu() { remove_submenu_page( 'admin.php', 'admin.php?page=svp_importer' ); } add_action( 'admin_menu', 'remove_submenu', 999 ); function change_post_object_label() { global $wp_post_types; $labels = &$wp_post_types['post']->labels; // $labels->wpseo_dashboard = 'sfdsdfsdfd'; } add_action( 'init', 'change_post_object_label' ); // tắt thông báo admin add_action('in_admin_header', function () { if (is_admin()) return; remove_all_actions('admin_notices'); remove_all_actions('all_admin_notices'); add_action('admin_notices', function () { echo 'My notice'; }); }, 1000); // START CUSTOM FUNCTION // // Tạo check box cho các trang thương mại điện tử để người dùng có thể linh hoạt trong việc upload sản phẩm. function add_ecommerce_checkbox() { add_meta_box( 'ecommerce_action', // Unique ID 'Trang Thương Mại Điện Tử', // Box title 'ecommerce_checkbox_html', // Content callback, must be of type callable 'product' // Post type ); } // Add check box META. add_action('add_meta_boxes', 'add_ecommerce_checkbox'); function ecommerce_checkbox_html($post) { $disabled = $post->post_status == "publish"; $lazada_value = get_post_meta($post->ID, '_lazada_checkbox_value', true); $shopee_value = get_post_meta($post->ID, '_shopee_checkbox_value', true); $tiki_value = get_post_meta($post->ID, '_tiki_checkbox_value', true); wp_nonce_field( 'upload_to_lazada', 'lazada_nonce' ); ?> <label>Tick vào các ô bên dưới để được đăng lên các trang TMĐT</label> <br/> <br/> <div> <label for="lazada">Lazada </label> <input <?php echo ($disabled )? "disabled='true'" : null; ?>id="has_lazada_upload" name="has_lazada_upload" type="checkbox" <?php checked($lazada_value, 'on'); ?> /> </div> <?php } function ecommerce_checkbox_action($post_id){ $post = get_post($post_id); $lazada_default_value = get_post_meta($post->ID, '_lazada_checkbox_value', true); $lazada_nonce = $_POST['lazada_nonce']; // Kiểm tra nếu nonce chưa được gán giá trị if( !isset( $lazada_nonce ) ) { return; } // Kiểm tra nếu giá trị nonce không trùng khớp if( !wp_verify_nonce( $lazada_nonce, 'upload_to_lazada' ) ) { return; } if (!$_POST['has_lazada_upload']) { // Trường hợp có sẳn dữ liệu, thực hiện update // Nếu không, ngừng xử lý. if($lazada_default_value) { lazada_product_post($post_id); }else { return; } } lazada_product_post($post_id); } // Handle Publish Prorduct button. add_action( 'save_post', 'handle_submit', 10, 3 ); function handle_submit( $post_id, $post, $update ) { if(( $post->post_type == 'product' ) && ($post->post_status == 'publish') ) { ecommerce_checkbox_action($post_id); } } // Lấy đường dẫn trang. function siteUrl() { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $domainName = $_SERVER['HTTP_HOST'].'/'; return $protocol.$domainName; } // Bulk action, tạo mới các hành động cho trang sản phẩm. // Đăng tải sản phẩm lên các trang mạng điện tử. add_action( 'current_screen', 'custom_bulk_actions' ); function custom_bulk_actions() { add_filter('bulk_actions-edit-product', 'register_product_bulk_actions'); add_filter('handle_bulk_actions-edit-product', 'handle_product_bulk_action', 10, 3 ); } // Add more bulk action for product admin page function register_product_bulk_actions($bulk_actions) { $bulk_actions['lazada_upload'] = __( 'Post to Lazada', 'lazada_upload'); return $bulk_actions; } // Handle ACTION OF BULK ACTION. function handle_product_bulk_action( $redirect_to, $doaction, $post_ids ) { if ( $doaction == 'lazada_upload' ) { foreach ( $post_ids as $post_id ) { lazada_product_post($post_id); } $redirect_to = add_query_arg( 'lazada_upload', count( $post_ids ), $redirect_to ); } return $redirect_to; } // Thêm mới column ở giao diện "Danh sách sản phẩm" để quản lý các sản phẩm ở trang thương mại điện tử. add_filter( 'manage_product_posts_columns', 'custom_product_column' ); function custom_product_column( $columns ) { $columns['ecommerce_status'] = __( 'Thương Mại Điện Tử' ); return $columns; } // Lấy dữ liệu về đổ vào columns. add_action( 'manage_product_posts_custom_column', 'custom_column_values', 10, 2); function custom_column_values( $column, $post_id ) { if ( 'ecommerce_status' === $column ) { $has_lazada_upload = get_post_meta( $post_id, '_lazada_checkbox_value', true ); echo '<strong>Lazada: </strong>' , $has_lazada_upload == 'on' ? '<span style="color: green;">Yes</span><br/>' : '<span style="color: red;">No</span><br/>'; } } // Dùng để tạo mới và chỉnh sửa sản phẩm function lazada_product_post($post_id) { $product = wc_get_product( $post_id ); $data = $product->get_data(); $url = "https://apiadmin.webtrongoi.vn/lazada/post_product"; $post_thumbnail_id = get_post_thumbnail_id( $post_id , 'single-post-thumbnail' ); $data["image_url"] = wp_get_attachment_image_url( $post_thumbnail_id, "post-thumbnail" ); $data["domain"] = siteUrl(); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); $result_decode = json_decode($result); // Write log. $fh = fopen(TEMPLATEPATH . "/test.txt", "w"); fputs($fh, print_r($result, true)); fclose($fh); update_post_meta($post_id, '_lazada_checkbox_value', 'on'); if($result_decode && strval($result_decode->code) == "0") { // update_post_meta($post_id, '_lazada_checkbox_value', 'on'); } else { printf('<div id="message_lazada" class="error fade">' . "[ LAZADA ] ". $result_decode->message . '<br/>' . $result_decode->detail[0]->message . '</div>'); } } // Kiểm tra trạng thái product post // Xoá sản phẩm hoặc có thể những action khác vào đây. add_action( 'transition_post_status', 'transition_post_status', 10, 3 ); function transition_post_status( $new_status, $old_status, $post ) { if ( $new_status == 'trash' && ( $post->post_type == 'product' ) ) { $data["id"] = $post->ID; $url = "https://apiadmin.webtrongoi.vn/lazada/post_product"; $post_thumbnail_id = get_post_thumbnail_id( $post_id , 'single-post-thumbnail' ); $data["domain"] = siteUrl(); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); $result_decode = json_decode($result); $fh = fopen(TEMPLATEPATH . "/test.txt", "w"); fputs($fh, print_r($result , true)); fclose($fh); } }