Advertisement
garruco

WCMP new

Apr 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: UP Custom Fields for WC
  4.  * Description: Adds a custom field to WooCommerce product
  5.  * Version: 1.0.0
  6.  * Author: UPPER TODAY
  7.  * Author URI: https://upper.today
  8.  * Text Domain: cfwc
  9. */
  10.  
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13.     exit;
  14. }
  15.  
  16. function console_log( $data ){
  17.     echo '<script>';
  18.     echo 'console.log('. json_encode( $data ) .')';
  19.     echo '</script>';
  20.   }
  21.  
  22. /**
  23.  * Display the custom text field
  24.  * @since 1.0.0
  25.  */
  26. function cfwc_create_custom_fields() {
  27.     $args = array(
  28.         'id'            => 'impact_field',
  29.         'label'         => __( 'Impact', 'cfwc' ),
  30.         'class'         => 'cfwc-custom-field',
  31.         'desc_tip'      => true,
  32.         'description'   => __( 'Write the impact of your product.', 'ctwc' ),
  33.     );
  34.     woocommerce_wp_text_input( $args );
  35.     $args = array(
  36.         'id'            => 'certification_field',
  37.         'label'         => __( 'Certification', 'cfwc' ),
  38.         'class'         => 'cfwc-custom-field',
  39.         'desc_tip'      => true,
  40.         'description'   => __( 'Write the certification of your product.', 'ctwc' ),
  41.     );
  42.     woocommerce_wp_text_input( $args );
  43. }
  44. add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_fields' );
  45.  
  46. /**
  47.  * Save the custom fields
  48.  * @since 1.0.0
  49.  */
  50. function cfwc_save_custom_field( $post_id ) {
  51.     $product = wc_get_product( $post_id );
  52.  
  53.     $title = isset( $_POST['impact_field'] ) ? $_POST['impact_field'] : '';
  54.     $product->update_meta_data( 'impact_field', sanitize_text_field( $title ) );
  55.     $product->save();
  56.  
  57.     $title = isset( $_POST['certification_field'] ) ? $_POST['certification_field'] : '';
  58.     $product->update_meta_data( 'certification_field', sanitize_text_field( $title ) );
  59.     $product->save();
  60. }
  61. add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );
  62.  
  63. /**
  64.  * Display impact on the front end
  65.  * @since 1.0.0
  66.  */
  67. function wc_tab_impact() {
  68.     global $post;
  69.     $product = wc_get_product( $post->ID );
  70.     printf('<p>%s</p>', esc_html( $product->get_meta( 'impact_field' )));
  71. }
  72.  
  73. /**
  74.  * Display Certification on the front end
  75.  * @since 1.0.0
  76.  */
  77. function wc_tab_certification() {
  78.     global $post;
  79.     $product = wc_get_product( $post->ID );
  80.     printf('<p>%s</p>', esc_html( $product->get_meta( 'certification_field' )));
  81. }
  82.  
  83. /**
  84.  * Display Certification on the front end
  85.  * @since 1.0.0
  86.  */
  87. function wc_tab_test() {
  88.     global $post;
  89.     $product = wc_get_product( $post->ID );
  90.     printf('<p>%s</p>', esc_html( $product->get_meta( 'custom_text_field' )));
  91. }
  92.  
  93. /**
  94.  * Adds custom product tabs
  95.  * @since 1.0.0
  96.  */
  97. function wc_tab_extra_tabs( $tabs ) {
  98.     global $post;
  99.  
  100.     // Check for the custom field values
  101.     $product = wc_get_product( $post->ID );
  102.  
  103.     if($product->get_meta( 'impact_field' )) {
  104.         // Adds the impact tab
  105.         $tabs['impact'] = array(
  106.             'title'     => __( 'Impact', 'woocommerce' ),
  107.             'priority'  => 50,
  108.             'callback'  => 'wc_tab_impact'
  109.         );
  110.     }
  111.  
  112.     if($product->get_meta( 'certification_field' )) {
  113.         // Adds the certification tab
  114.         $tabs['certification'] = array(
  115.             'title'     => __( 'Certification', 'woocommerce' ),
  116.             'priority'  => 50,
  117.             'callback'  => 'wc_tab_certification'
  118.         );
  119.     }
  120.  
  121.     if($product->get_meta( 'custom_text_field' )) {
  122.         // Adds the test tab
  123.         $tabs['certification'] = array(
  124.             'title'     => __( 'Test', 'woocommerce' ),
  125.             'priority'  => 50,
  126.             'callback'  => 'wc_tab_test'
  127.         );
  128.     }
  129.  
  130.     return $tabs;
  131.  
  132. }
  133. add_filter( 'woocommerce_product_tabs', 'wc_tab_extra_tabs' );
  134.  
  135. /**
  136. * Add Custom Tab in add product page.
  137. * @author WC Marketplace
  138. * @Version 3.3.0
  139. */
  140. function cfwc_add_custom_product_data_tabs( $tabs ) {
  141.    $tabs['advanced'] = array(
  142.        'label'    => __( 'Custom Tab', 'cfwc' ),
  143.        'target'   => 'custom_tab_product_data',
  144.        'class'    => array(),
  145.        'priority' => 100,
  146.    );
  147.    return $tabs;
  148. }
  149. add_filter( 'wcmp_product_data_tabs', 'cfwc_add_custom_product_data_tabs' );
  150.  
  151. /**
  152. * Add Custom Tab content in add product page.
  153. * @author WC Marketplace
  154. * @Version 3.3.0
  155. */
  156. function cfwc_add_custom_product_data_content( $pro_class_obj, $product, $post ) {
  157.    ?>
  158.    <div role="tabpanel" class="tab-pane fade" id="custom_tab_product_data"> <!-- just make sure tabpanel id should replace with your added tab target -->
  159.        <div class="row-padding">
  160.            <div class="form-group">
  161.                <label class="control-label col-sm-3 col-md-3">Custom Text Field</label>
  162.                <div class="col-md-6 col-sm-9">
  163.                    <input type="text" name="custom_text_field" class="form-control" />
  164.                </div>
  165.            </div>
  166.        </div>
  167.    </div>
  168.    <?php
  169. }
  170. add_action( 'wcmp_product_tabs_content', 'cfwc_add_custom_product_data_content', 10, 3 );
  171.  
  172. /**
  173. * Save Custom Tab content data.
  174. * @author WC Marketplace
  175. * @Version 3.3.0
  176. */
  177. function cfwc_save_custom_product_data( $product, $post_data ) {
  178.    
  179.    if( isset($post_data['post_ID']) && isset($post_data['custom_text_field'])){
  180.        update_post_meta( absint( $post_data['post_ID'] ), '_custom_text_field', $post_data['custom_text_field']);
  181.    }
  182. }
  183. add_action( 'wcmp_process_product_object', 'cfwc_save_custom_product_data', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement