tewst.php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of tewst.php.php

  1. <?php
  2.  if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3.  
  4. class CrmErpSolutionProProducts{
  5.        
  6.         public $plugin = 'CrmErpSolutionPro';
  7.         public $slug = 'crm-erp-business-solution-pro';
  8.         public $enableProducts ='enableProducts';    
  9.  
  10.         public function __construct() {     
  11.        
  12.                 if( get_option( $this->plugin.$this->enableProducts ) ){
  13.  
  14.                         add_action( "init", array($this,"offlineProducts" ) );
  15.                         add_action( "admin_init", array( $this,"metaBox" ) );
  16.                         add_action( "admin_init", array( $this,"init" ) );
  17.                         add_action( "save_post", array( $this,"saveFields" ) );   
  18.                         add_action( 'admin_menu', array( $this,'menu_page') );                             
  19.                         add_filter( 'manage_offline_products_posts_columns', array( $this,'addColumnHeader' ) );                          
  20.                         add_filter( "manage_edit-offline_products_sortable_columns", array( $this,"addColumnHeader" ) );  
  21.                         add_filter( "manage_offline_products_columns", array( $this,"column_order" ) );
  22.                         add_filter( 'manage_edit-offline_products_columns', array( $this,"column_order" ) );                      
  23.                         add_filter( 'pre_get_posts', array($this,'searchfilter') ); 
  24.                         add_action( 'wp_ajax_nopriv_displayProducts', array( $this,'displayProducts' ) );
  25.                         add_action( 'wp_ajax_displayProducts', array( $this,'displayProducts' ) );        
  26.                         add_action( 'wp_ajax_nopriv_getProducts', array( $this,'displaySoldProductsTransactions' ) );
  27.                         add_action( 'wp_ajax_getProducts', array( $this,'displaySoldProductsTransactions' ) );
  28.                         add_action( 'wp_ajax_nopriv_getProducts', array( $this,'displaySoldProductsEshop' ) );
  29.                         add_action( 'wp_ajax_getProducts', array( $this,'displaySoldProductsEshop' ) );                   
  30.                        
  31.                 }
  32.  
  33.         }
  34.  
  35.         public function init(){
  36.                
  37.                 if( get_option( $this->plugin.$this->enableProducts ) ){
  38.                        
  39.                         add_action( 'restrict_manage_posts', array( $this,'crmpro_filter_products' ) , 10, 2);
  40.                         add_action( 'manage_offline_products_posts_custom_column', array( $this,'addAdColumns' ),10,2 );  
  41.  
  42.                 }
  43.  
  44.         }
  45.  
  46.  
  47.        
  48.        
  49.         public function offlineProducts(){
  50.                   register_post_type( 'offline_products',
  51.                         array(
  52.                           'labels' => array(
  53.                                 'name' => esc_html__( 'Offline Products' ,'CrmErpSolutionPro' ),
  54.                                 'singular_name' => esc_html__( 'Offline Product','CrmErpSolutionPro' ),
  55.                                 'search_items' =>  esc_html__( 'Search Offline Products' ,'CrmErpSolutionPro' ),
  56.                                 'all_items' => esc_html__( 'All Offline Products' ,'CrmErpSolutionPro' ),
  57.                                 'parent_item' => esc_html__( 'Parent Offline Product','CrmErpSolutionPro' ),
  58.                                 'parent_item_colon' => esc_html__( 'Parent Offline Product:','CrmErpSolutionPro' ),
  59.                                 'edit_item' => esc_html__( 'Edit Offline Product', 'CrmErpSolutionPro' ), 
  60.                                 'update_item' => esc_html__( 'Update Offline Product' , 'CrmErpSolutionPro' ),
  61.                                 'add_new_item' => esc_html__( 'Add New Offline Product' , 'CrmErpSolutionPro' ),
  62.                                 'add_new'            => esc_html__( 'New Offline Product', 'CrmErpSolutionPro' ),
  63.                                 'new_item_name' => esc_html__( 'New Offline Product Name', 'CrmErpSolutionPro' ),
  64.                                 'new_item'           => esc_html__( 'New Offline Product', 'CrmErpSolutionPro' ),
  65.                                 'menu_name' => esc_html__( 'Offline Products', 'CrmErpSolutionPro' ),
  66.                                 'not_found' => esc_html__('No Products found', 'CrmErpSolutionPro' ),
  67.                        
  68.                           ),
  69.                           'description' => esc_html__( 'Adding and editing my Offline Products', 'CrmErpSolutionPro' ),
  70.                           'menu_icon'   => 'dashicons-tag',
  71.                           'supports' => array( 'title', 'thumbnail' ),
  72.                                 'show_in_rest'       => true,
  73.                                 'rest_base'          => 'offline_products',
  74.                                 'rest_controller_class' => 'WP_REST_Posts_Controller',       
  75.                                 'capability_type' => 'page',
  76.                                 'hierarchical' => false,
  77.                                 'menu_position'      => null,
  78.                                 'public' => false,  // it's not public, it shouldn't have it's own permalink, and so on
  79.                                 'publicly_queryable' => true,  // you should be able to query it
  80.                                 'show_ui' => true,  // you should be able to edit it in wp-admin
  81.                                 //'show_in_menu'       => false,
  82.                                 'menu_position'      => null,
  83.                                 'show_in_menu'       => false,
  84.                                 'exclude_from_search' => true,  // you should exclude it from search results
  85.                                 'show_in_nav_menus' => false,  // you shouldn't be able to add it to menus
  86.                                 'has_archive' => true,  // it shouldn't have archive page
  87.                                
  88.                                 'rewrite' => false,  // it shouldn't have rewrite rules
  89.                         )
  90.                   );
  91.                    $labels = array(
  92.                         'name' => _x( 'Category', 'CrmErpSolutionPro' ),
  93.                         'singular_name' => _x( 'Category', 'CrmErpSolutionPro' ),
  94.                         'search_items' =>  esc_html__( 'Search Category','CrmErpSolutionPro' ),
  95.                         'all_items' => esc_html__( 'All Category' , 'CrmErpSolutionPro' ),
  96.                         'parent_item' => esc_html__( 'Parent Category' , 'CrmErpSolutionPro' ),
  97.                         'parent_item_colon' => esc_html__( 'Parent Category:' , 'CrmErpSolutionPro' ),
  98.                         'edit_item' => esc_html__( 'Edit Category', 'CrmErpSolutionPro' ), 
  99.                         'update_item' => esc_html__( 'Update Category', 'CrmErpSolutionPro' ),
  100.                         'add_new_item' => esc_html__( 'Add New Category', 'CrmErpSolutionPro' ),
  101.                         'new_item_name' => esc_html__( 'New Type Category', 'CrmErpSolutionPro' ),
  102.                         'menu_name' => esc_html__( 'Category' , 'CrmErpSolutionPro' ),
  103.                   );        
  104.                   register_taxonomy( 'off_prod_cat', array( 'offline_products' ), array(
  105.                         'hierarchical' => false,
  106.                         'labels' => $labels,
  107.                         'show_admin_column' => true,
  108.                         'query_var' => true,
  109.                                 'public' => false,  // it's not public, it shouldn't have it's own permalink, and so on
  110.                                 'publicly_queryable' => true,  // you should be able to query it
  111.                                 'show_ui' => true,  // you should be able to edit it in wp-admin
  112.                                 'exclude_from_search' => true,  // you should exclude it from search results
  113.                                 'show_in_nav_menus' => false,  // you shouldn't be able to add it to menus
  114.                                 'has_archive' => false,  // it shouldn't have archive page
  115.                                 'rewrite' => false,  // it shouldn't have rewrite rules                                
  116.                   ));
  117.  
  118.                    $labels = array(
  119.                         'name' => _x( 'Brand', $this->plugin ),
  120.                         'singular_name' => _x( 'Brand', 'CrmErpSolutionPro' ),
  121.                         'search_items' =>  esc_html__( 'Search Brand', 'CrmErpSolutionPro' ),
  122.                         'all_items' => esc_html__( 'All Brand' , 'CrmErpSolutionPro' ),
  123.                         'parent_item' => esc_html__( 'Parent Brand' , 'CrmErpSolutionPro' ),
  124.                         'parent_item_colon' => esc_html__( 'Parent Brand:' , 'CrmErpSolutionPro' ),
  125.                         'edit_item' => esc_html__( 'Edit Brand', 'CrmErpSolutionPro'  ), 
  126.                         'update_item' => esc_html__( 'Update Brand', 'CrmErpSolutionPro' ),
  127.                         'add_new_item' => esc_html__( 'Add New Brand', 'CrmErpSolutionPro' ),
  128.                         'new_item_name' => esc_html__( 'New Type Brand', 'CrmErpSolutionPro' ),
  129.                         'menu_name' => esc_html__( 'Brand' , 'CrmErpSolutionPro' ),
  130.                   );        
  131.                   register_taxonomy( 'off_prod_brand', array( 'offline_products' ), array(
  132.                         'hierarchical' => false,
  133.                         'labels' => $labels,
  134.                         'show_admin_column' => true,
  135.                         'query_var' => true,
  136.                                 'public' => false,  // it's not public, it shouldn't have it's own permalink, and so on
  137.                                 'publicly_queryable' => true,  // you should be able to query it
  138.                                 'show_ui' => true,  // you should be able to edit it in wp-admin
  139.                                 'exclude_from_search' => true,  // you should exclude it from search results
  140.                                 'show_in_nav_menus' => false,  // you shouldn't be able to add it to menus
  141.                                 'has_archive' => false,  // it shouldn't have archive page
  142.                                 'rewrite' => false,  // it shouldn't have rewrite rules                   
  143.                    
  144.                         'supports' => array( 'title', 'editor', 'image' ),
  145.                   ));
  146.                   
  147.         }
  148.  
  149.         public function crmpro_filter_products( $post_type, $which ) {
  150.  
  151.                 // Apply this only on a specific post type
  152.                 if ( 'offline_products' !== $post_type )
  153.                         return;
  154.  
  155.                 // A list of taxonomy slugs to filter by
  156.                 $taxonomies = array( 'off_prod_brand', 'off_prod_cat' );
  157.  
  158.                 foreach ( $taxonomies as $taxonomy_slug ) {
  159.  
  160.                         // Retrieve taxonomy data
  161.                         $taxonomy_obj = get_taxonomy( $taxonomy_slug );
  162.                         $taxonomy_name = $taxonomy_obj->labels->name;
  163.  
  164.                         // Retrieve taxonomy terms
  165.                         $terms = get_terms( $taxonomy_slug );
  166.  
  167.                         // Display filter HTML
  168.                         echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
  169.                         echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'CrmErpSolutionPro' ), $taxonomy_name ) . '</option>';
  170.                         foreach ( $terms as $term ) {
  171.                                 printf(
  172.                                         '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
  173.                                         $term->slug,
  174.                                         ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
  175.                                         $term->name,
  176.                                         $term->count
  177.                                 );
  178.                         }
  179.                         echo '</select>';
  180.                 }
  181.         }
  182.        
  183.         public function getProducts( $id ){
  184.                 $args = array(
  185.                         'post_type' => array( 'product','offline_products' ),
  186.                         'orderby' => 'ASC',
  187.                         'post__in' => $id
  188.                 );
  189.                
  190.  
  191.                
  192.                 $loop = new WP_Query( $args );
  193.                
  194.                 while ( $loop->have_posts() ) : $loop->the_post();
  195.                         global $product;
  196.                         global $post;
  197.                         global $woocommerce;
  198.                        
  199.                         if (get_post_type( get_the_ID() ) == 'product' ){
  200.                                 $price = get_post_meta( get_the_ID(), '_price', true);
  201.                                 $stock = get_post_meta( get_the_ID(), '_stock', true);
  202.                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  203.                         }else{
  204.                                 $price = get_post_meta( get_the_ID(), $this->plugin.'price', true );
  205.                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  206.                                 $stock = get_post_meta( get_the_ID(), $this->plugin.'quantity', true );                          
  207.                         }
  208.                        
  209.                         if ( get_post_type( get_the_ID() ) == 'product' ){
  210.                                
  211.                                 $product = wc_get_product( get_the_ID() );
  212.                                
  213.                                 if ( is_singular( 'product' ) ) {
  214.                                         echo "<option stock='".$stock."' wholesaleprice='".$wholesaleprice."' price='".$price."' value='".get_the_ID()."'>".get_the_title()."</option>";
  215.                                        
  216.                                 }else{
  217.                                         $product = new WC_Product_Variable( get_the_ID() );
  218.                                         $variations = $product->get_available_variations(); 
  219.                                         foreach ( $variations as $variation ) {  
  220.                                        
  221.                                                 foreach($variation['attributes'] as $key=>$value){
  222.                                                 }
  223.                                                 $attributes = implode('', array_map(
  224.                                                         function ($v, $k) {
  225.                                                                 if(is_array($v)){
  226.                                                                         return $k.'[]='.implode(' '.str_replace("attribute_pa_","",$k).'[]=', $v);
  227.                                                                 }else{
  228.                                                                         return " ".str_replace("attribute_pa_","",$k).'='.$v;
  229.                                                                 }
  230.                                                         }, 
  231.                                                         $variation['attributes'], 
  232.                                                         array_keys($variation['attributes'])
  233.                                                         ));            
  234.                                                        
  235.                                                 $variationId = $variation['variation_id'];   
  236.                                                 echo "<option stock='".$stock."' wholesaleprice='".$wholesaleprice."' price='".$price."' value='".$variationId."'>".get_the_title().": ". implode(', ',$variation['attributes'])."</option>";
  237.                                         }
  238.                                        
  239.                                 }                              
  240.                         }else{
  241.                                 echo "<option stock='".$stock."' wholesaleprice='".$wholesaleprice."' price='".$price."' value='".get_the_ID()."'>".get_the_title()."</option>";
  242.                         }
  243.  
  244.                 endwhile;
  245.  
  246.                 wp_reset_query();              
  247.         }
  248.  
  249.        
  250.         public function searchProductForm(){
  251.                 ?>
  252.                 <i class='fa fa-search productsearch' style='width:10%;cursor:pointer;'></i>
  253.                 <input type="text" id="searchproduct" style='width:85%' name="searchproduct" placeholder='<?php esc_html_e( "Select product: search by sku/name", 'CrmErpSolutionPro' ); ?>' /> 
  254.                 <select id='product' style='display:none;width:100%'  name='product' class="code "></select>
  255.                 <?php
  256.         }
  257.  
  258.         public function searchfilter($query) {
  259.                 if( $title = $query->get( '_meta_or_title' ) )
  260.                 {
  261.                         add_filter( 'get_meta_sql', function( $sql ) use ( $title )
  262.                         {
  263.                                 global $wpdb;
  264.  
  265.                                 // Only run once:
  266.                                 static $nr = 0; 
  267.                                 if( 0 != $nr++ ) return $sql;
  268.  
  269.                                 // Modify WHERE part:
  270.                                 $sql['where'] = sprintf(
  271.                                         " AND ( %s OR %s ) ",
  272.                                         $wpdb->prepare( "{$wpdb->posts}.post_title = '%s'", $title ),
  273.                                         mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
  274.                                 );
  275.                                 return $sql;
  276.                         });
  277.                 }
  278.         }
  279.  
  280.  
  281.         public function displayProducts(){
  282.                
  283.                 if( isset( $_REQUEST['searchproduct'] ) && !empty( $_REQUEST['searchproduct'] ) ){
  284.                
  285.                 $product = sanitize_text_field( $_REQUEST['searchproduct'] );      
  286.  
  287.                
  288.                 $args = array(
  289.                         'post_type'      => array('product','offline_products' ),
  290.                         'posts_per_page' => -1,
  291.                         'post_status'            => 'publish',
  292.                         '_meta_or_title' => $product,
  293.                         'meta_query' => array(
  294.                                 'relation' => 'OR',
  295.                 array(
  296.                    'key'     => '_sku',
  297.                    'value'   => $product,
  298.                    'compare' => 'LIKE'
  299.                 ),
  300.                 array(
  301.                    'key'     => $this->plugin.'quantity',
  302.                    'value'   => $product,
  303.                    'compare' => 'LIKE'
  304.                 ),                                     
  305.             ),                     
  306.                 );
  307.  
  308.                 $loop = new WP_Query( $args );
  309.                
  310.                 while ( $loop->have_posts() ) : $loop->the_post();
  311.                
  312.                         global $product;
  313.                         global $post;
  314.                         global $woocommerce;  
  315.                        
  316.                         if ( get_post_type( get_the_ID() ) == 'product' ){
  317.                                
  318.                         }else{
  319.                                
  320.                                 $price = get_post_meta( get_the_ID(), $this->plugin.'price', true );
  321.                                 $stock = get_post_meta( get_the_ID(), $this->plugin.'quantity', true );  
  322.                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  323.                                
  324.                         }
  325.                        
  326.                         if ( get_post_type( get_the_ID() ) == 'product' ){
  327.                                 $product = wc_get_product( get_the_ID() );
  328.                                
  329.                                 if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) ) {
  330.  
  331.                                         $price = get_post_meta( get_the_ID(), '_price', true );
  332.                                         $stock = get_post_meta( get_the_ID(), '_stock', true );
  333.                                         $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  334.                                
  335.                                         echo "<option stock='".$stock."' sku = '".get_post_meta( get_the_ID(), '_sku', true )."' wholesaleprice='".$wholesaleprice."' price='".$price."' value='".get_the_ID()."'>".get_the_title()."</option>";
  336.                                 }else{
  337.                                         $product = new WC_Product_Variable( get_the_ID() );
  338.                                         $variations = $product->get_available_variations(); 
  339.                                        
  340.                                         foreach ( $variations as $variation ) {  
  341.  
  342.                                         $price = get_post_meta( $variation['variation_id'], '_price', true);
  343.                                         $stock = get_post_meta( $variation['variation_id'], '_stock', true);
  344.                                         $wholesaleprice = get_post_meta( $variation['variation_id'], $this->plugin.'wholesaleprice', true);
  345.                                        
  346.                                                 foreach($variation['attributes'] as $key=>$value){
  347.                                                 }
  348.                                                 $attributes = implode('', array_map(
  349.                                                         function ($v, $k) {
  350.                                                                 if(is_array($v)){
  351.                                                                         return $k.'[]='.implode(' '.str_replace("attribute_pa_","",$k).'[]=', $v);
  352.                                                                 }else{
  353.                                                                         return " ".str_replace("attribute_pa_","",$k).'='.$v;
  354.                                                                 }
  355.                                                         }, 
  356.                                                         $variation['attributes'], 
  357.                                                         array_keys($variation['attributes'])
  358.                                                         ));                                    
  359.                                                 $variationId = $variation['variation_id'];
  360.                                                                
  361.                                                 echo "<option stock='".$stock."' sku = '".get_post_meta( $variation['variation_id'], '_sku', true)."'  wholesaleprice='".$wholesaleprice."' price='".$price."' value='".$variationId."'>".get_the_title().": ". implode(', ',$variation['attributes'])."</option>";
  362.                                         }
  363.                                        
  364.                                 }                              
  365.                         }else{
  366.                                 echo "<option stock='".$stock."' sku = '".get_post_meta( get_the_ID(), $this->plugin.'sku', true )."' wholesaleprice='".$wholesaleprice."' price='".$price."' value='".get_the_ID()."'>".get_the_title()."</option>";
  367.                         }
  368.                 endwhile;
  369.  
  370.                 wp_reset_query();
  371.                
  372.                 }
  373.        
  374.         }
  375.  
  376.         public function displaySoldProducts(){
  377.                
  378.                 if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'getProducts' ){
  379.                                                
  380.                         if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  381.                                
  382.                                 global $woocommerce;
  383.                                 $filters = array(
  384.                                         'post_status' => array( 'wc-completed'),
  385.                                         'post_type' => 'shop_order',
  386.                                         'posts_per_page' => -1,
  387.                                         'paged' => 1,
  388.                                         'orderby' => 'modified',
  389.                                         'order' => 'ASC',
  390.                                 );
  391.                                                
  392.                                 $loop = new WP_Query( $filters );         
  393.  
  394.  
  395.                                         if ( get_post_type( get_the_ID() ) == 'product' ){
  396.                                                 $price = get_post_meta( get_the_ID(), '_price', true );
  397.                                                 $stock = get_post_meta( get_the_ID(), '_stock', true );
  398.                                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  399.                                         }else{
  400.                                                 $price = get_post_meta( get_the_ID(), $this->plugin.'price', true );
  401.                                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true );
  402.                                                 $stock = get_post_meta( get_the_ID(), $this->plugin.'quantity', true );                          
  403.                                         }
  404.                                        
  405.                                 if( $loop->have_posts() ){
  406.                                         $products=array();
  407.                                        
  408.                                         while ( $loop->have_posts() ) {
  409.                                                 $loop->the_post();
  410.                                                 $order = new WC_Order( $loop->post->ID );
  411.                                                
  412.                                                 $items = $order->get_items();
  413.                                                
  414.                                                 foreach( $items as $product ) {   
  415.                                                
  416.                                                         if( !in_array( $product['product_id'] ,$products ) ){
  417.                                                                 array_push( $products, $product['product_id'] );
  418.                                                                 echo "<option  sku = '".get_post_meta( $product['product_id'], '_sku', true )."' value='".$product['product_id']."'>".$product['name']."</option>";
  419.                                                         }
  420.                                                 }
  421.                                         }
  422.                                 }
  423.                        
  424.                         }
  425.                        
  426.                         global $wpdb;
  427.                         $table_name = $wpdb->prefix . 'crm_transaction_items'; // do not forget about tables prefix
  428.                         $result = $wpdb->get_results( "SELECT DISTINCT product_id FROM ".$table_name."  " );
  429.                         if( $result ){
  430.                                 foreach( $result as $res ) {
  431.                                         echo "<option  sku = '".get_post_meta( $res->product_id, $this->plugin.'sku', true )."' value='".$res->product_id."'>".get_the_title( $res->product_id )."</option>";
  432.                                 }
  433.                         }
  434.  
  435.                 }
  436.         }
  437.  
  438.  
  439.         public function displaySoldProductsTransactions(){
  440.                
  441.                 if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'getProducts' ){
  442.                        
  443.                         global $wpdb;
  444.                         $table_name = $wpdb->prefix . 'crm_transaction_items'; // do not forget about tables prefix
  445.                         $result = $wpdb->get_results("SELECT DISTINCT product_id FROM ".$table_name."  ");
  446.                         if( $result ){
  447.                                 foreach( $result as $res ) {
  448.                                         echo "<option  sku = '".get_post_meta( $res->product_id, $this->plugin.'sku', true)."' value='".$res->product_id."'>".get_the_title($res->product_id )."</option>";
  449.                                 }
  450.                         }
  451.                
  452.                 }
  453.        
  454.         }
  455.        
  456.         public function displaySoldProductsEshop(){
  457.                
  458.                 if(isset($_REQUEST['action']) && $_REQUEST['action']='getProducts' ){
  459.                        
  460.                 global $woocommerce;
  461.                 $filters = array(
  462.                         'post_status' => array( 'wc-completed'),
  463.                         'post_type' => 'shop_order',
  464.                         'posts_per_page' => -1,
  465.                         'paged' => 1,
  466.                         'orderby' => 'modified',
  467.                         'order' => 'ASC',
  468.                 );
  469.                                
  470.                 $loop = new WP_Query($filters);             
  471.  
  472.  
  473.                         if (   get_post_type( get_the_ID() ) == 'product' ){
  474.                                 $price = get_post_meta( get_the_ID(), '_price', true);
  475.                                 $stock = get_post_meta( get_the_ID(), '_stock', true);
  476.                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true);
  477.                         }else{
  478.                                 $price = get_post_meta( get_the_ID(), $this->plugin.'price', true);
  479.                                 $wholesaleprice = get_post_meta( get_the_ID(), $this->plugin.'wholesaleprice', true);
  480.                                 $stock = get_post_meta( get_the_ID(), $this->plugin.'quantity', true);                            
  481.                         }
  482.                        
  483.                 if($loop->have_posts()){
  484.                         $products=array();
  485.                         while ($loop->have_posts()) {
  486.                                 $loop->the_post();
  487.                                 $order = new WC_Order($loop->post->ID);
  488.                                
  489.                                 $items = $order->get_items();
  490.                                
  491.                                 foreach( $items as $product ) {   
  492.                                
  493.                                         if( !in_array( $product['product_id'] ,$products ) ){
  494.                                                 array_push($products, $product['product_id'] );
  495.                                                 echo "<option  sku = '".get_post_meta( $product['product_id'], '_sku', true)."' value='".$product['product_id']."'>".$product['name']."</option>";
  496.                                         }
  497.                                 }
  498.                         }
  499.                 }
  500.                
  501.                 }
  502.        
  503.         }
  504.        
  505.         public function metaBox($post){
  506.                 add_meta_box("prodPrice", __('Product Info',$this->plugin), array($this,"priceCreate" ) , "offline_products", "normal", "high"); 
  507.         }      
  508.        
  509.  
  510.                 public function addColumnHeader( $columns ) {
  511.                         $columns['Image']  = 'Image';
  512.                         $columns['SKU']  = 'Sku';
  513.                         $columns['Brand']  = 'Brand';
  514.                         $columns['Category']  = 'Category';
  515.                         $columns['WholesalePrice']  = 'Wholesale Price';
  516.                         $columns['Price']  = 'Retail Price';
  517.                         $columns['Stock']  = 'Stock';
  518.                         return $columns;
  519.  
  520.                 }      
  521.                 public function addAdColumns( $column_name, $post_id ) {
  522.                         global $post;
  523.                        
  524.                         if( $column_name == 'Image' ) {
  525.                                 $image = get_the_post_thumbnail( $post_id, array( 30, 30) );
  526.                                 if($image !='') {
  527.                                         print get_the_post_thumbnail( $post_id, array( 30, 30) );
  528.                                 }
  529.                         }                      
  530.                         if( $column_name == 'SKU' ) {
  531.                                 $sku = get_post_meta( $post_id, $this->plugin.'sku', true );
  532.                                 if($sku !='') {
  533.                                         echo esc_attr($sku);
  534.                                 }
  535.                         }
  536.                         if( $column_name == 'WholesalePrice' ) {
  537.                                 $price = get_post_meta( $post_id, $this->plugin.'wholesaleprice', true );
  538.                                 if($price !='') {
  539.                                         echo esc_attr($price);
  540.                                 }
  541.                         }
  542.                         if( $column_name == 'Price' ) {
  543.                                 $price = get_post_meta( $post_id, $this->plugin.'price', true );
  544.                                 if($price !='') {
  545.                                         echo esc_attr($price);
  546.                                 }
  547.                         }      
  548.                         if( $column_name == 'Stock' ) {
  549.                                 $quantity = get_post_meta( $post_id, $this->plugin.'quantity', true );
  550.                                 if($quantity !='') {
  551.                                         echo esc_attr($quantity);
  552.                                 }
  553.                         }
  554.                         if( $column_name == 'Category' ) {
  555.  
  556.                                 $terms = get_the_terms( $post_id, 'off_prod_cat' );
  557.                                                                                  
  558.                                 if ( $terms && ! is_wp_error( $terms ) ) : 
  559.                                  
  560.                                         $draught_links = array();
  561.                                  
  562.                                         foreach ( $terms as $term ) {
  563.                                                 $draught_links[] = $term->name;
  564.                                         }
  565.                                                                                  
  566.                                         $on_draught = join( ", ", $draught_links );
  567.                                         ?>
  568.                                  
  569.                                         <p class="">
  570.                                                 <?php printf( esc_html__( '%s', 'textdomain' ), esc_html( $on_draught ) ); ?>
  571.                                         </p>
  572.                                 <?php endif; 
  573.                                
  574.                         }                      
  575.                         if( $column_name == 'Brand' ) {
  576.  
  577.                                 $terms = get_the_terms( $post_id, 'off_prod_brand' );
  578.                                                                                  
  579.                                 if ( $terms && ! is_wp_error( $terms ) ) : 
  580.                                  
  581.                                         $draught_links = array();
  582.                                  
  583.                                         foreach ( $terms as $term ) {
  584.                                                 $draught_links[] = $term->name;
  585.                                         }
  586.                                                                                  
  587.                                         $on_draught = join( ", ", $draught_links );
  588.                                         ?>
  589.                                  
  590.                                         <p class="">
  591.                                                 <?php printf( esc_html__( '%s', 'textdomain' ), esc_html( $on_draught ) ); ?>
  592.                                         </p>
  593.                                 <?php endif; 
  594.                                
  595.                         }      
  596.                        
  597.                 }
  598.  
  599.                 public function column_order($columns) {
  600.                         unset( $columns );
  601.                        
  602.                         $columns = array(
  603.                                 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
  604.                                 //'id' => __('ID', $this->plugin),
  605.                                 'Image' => __('Image', $this->plugin),
  606.                                 'title' => __('Title', $this->plugin),
  607.                                 'Brand' => __('Brand', $this->plugin),
  608.                                 'Category' => __('Category', $this->plugin),
  609.                                 'SKU' => __('SKU', $this->plugin),
  610.                                 'WholesalePrice' => __('Wholesale Price', $this->plugin),
  611.                                 'Price' => __('Retail Price', $this->plugin),
  612.                                 'Stock' => __('Stock', $this->plugin),
  613.                                 'Date' => __('Date', $this->plugin),
  614.                                
  615.                                
  616.                         );                     
  617.                         return $columns;
  618.                 }
  619.                
  620.         public function priceCreate($post){
  621.                
  622.                 $sku = get_post_meta($post->ID, $this->plugin.'sku' , true ) ;                  
  623.         ?>   <p><label ><?php _e('SKU', $this->plugin)?></label> <input placeholder='sku' name='<?php print $this->plugin."sku";?>' value='<?php print esc_attr($sku); ?>' /></p>    <?php 
  624.  
  625.                 $wholesaleprice = get_post_meta($post->ID, $this->plugin.'wholesaleprice' , true ) ;                    
  626.         ?>   <p><label ><?php _e('Wholesale Price', $this->plugin)?></label> <input required placeholder='price' name='<?php print $this->plugin."wholesaleprice";?>' value='<?php print esc_attr($wholesaleprice); ?>' /></p>    <?php  
  627.                
  628.                 $price = get_post_meta($post->ID, $this->plugin.'price' , true ) ;                      
  629.         ?>   <p><label ><?php _e('Retail Price', $this->plugin)?></label> <input required placeholder='price' name='<?php print $this->plugin."price";?>' value='<?php print esc_attr($price); ?>' /></p>    <?php       
  630.                
  631.                 $quantity = get_post_meta($post->ID, $this->plugin.'quantity' , true ) ;
  632.                
  633.         ?>   <p><label ><?php _e('Stock', $this->plugin)?></label> <input required placeholder='quantity' name='<?php print $this->plugin."quantity";?>' value='<?php print esc_attr($quantity); ?>' /></p>    <?php
  634.         }
  635.  
  636.        
  637.         public function saveFields(){
  638.                        
  639.                 global $post;
  640.  
  641.                 if(isset($_POST[$this->plugin.'sku'])){
  642.                         if (!empty($_POST[$this->plugin.'sku']) ) {
  643.                                 $sku = htmlspecialchars(sanitize_textarea_field($_POST[$this->plugin.'sku']) );
  644.                                  update_post_meta($post->ID, $this->plugin.'sku', $sku);            
  645.                         }
  646.                 }
  647.                
  648.                 if(isset($_POST[$this->plugin.'quantity'])){
  649.                         if (!empty($_POST[$this->plugin.'quantity']) ) {
  650.                                 $quantity = htmlspecialchars(sanitize_textarea_field($_POST[$this->plugin.'quantity']) );
  651.                                  update_post_meta($post->ID, $this->plugin.'quantity', $quantity);          
  652.                         }
  653.                 }              
  654.                
  655.                 if(isset($_POST[$this->plugin.'price'])){
  656.                         if (!empty($_POST[$this->plugin.'price']) ) {
  657.                                 $price=sanitize_text_field($_POST[$this->plugin.'price']);
  658.                                  update_post_meta($post->ID, $this->plugin.'price', $price);        
  659.                         }
  660.                 }
  661.  
  662.                 if(isset($_POST[$this->plugin.'wholesaleprice'])){
  663.                         if (!empty($_POST[$this->plugin.'wholesaleprice']) ) {
  664.                                 $wholesaleprice=sanitize_text_field($_POST[$this->plugin.'wholesaleprice']);
  665.                                  update_post_meta($post->ID, $this->plugin.'wholesaleprice', $wholesaleprice);              
  666.                         }
  667.                 }
  668.                
  669.         }
  670.  
  671.         public function menu_page() {
  672.                 add_submenu_page( $this->slug, esc_html__("Offline Products", $this->plugin ), esc_html__("Offline Products", $this->plugin), $this->plugin, 'edit.php?post_type=offline_products', NULL );
  673.         }
  674.        
  675. }
  676.  
  677. $start = new CrmErpSolutionProProducts();
File Description
  • tewst.php
  • PHP Code
  • 23 Dec-2020
  • 24.87 Kb
You can Share it: