Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: UP Custom Fields for WC
- * Description: Adds a custom field to WooCommerce product
- * Version: 1.0.0
- * Author: UPPER TODAY
- * Author URI: https://upper.today
- * Text Domain: cfwc
- */
- // Exit if accessed directly
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- function console_log( $data ){
- echo '<script>';
- echo 'console.log('. json_encode( $data ) .')';
- echo '</script>';
- }
- /**
- * Display the custom text field
- * @since 1.0.0
- */
- function cfwc_create_custom_fields() {
- $args = array(
- 'id' => 'impact_field',
- 'label' => __( 'Impact', 'cfwc' ),
- 'class' => 'cfwc-custom-field',
- 'desc_tip' => true,
- 'description' => __( 'Write the impact of your product.', 'ctwc' ),
- );
- woocommerce_wp_text_input( $args );
- $args = array(
- 'id' => 'certification_field',
- 'label' => __( 'Certification', 'cfwc' ),
- 'class' => 'cfwc-custom-field',
- 'desc_tip' => true,
- 'description' => __( 'Write the certification of your product.', 'ctwc' ),
- );
- woocommerce_wp_text_input( $args );
- }
- add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_fields' );
- /**
- * Save the custom fields
- * @since 1.0.0
- */
- function cfwc_save_custom_field( $post_id ) {
- $product = wc_get_product( $post_id );
- $title = isset( $_POST['impact_field'] ) ? $_POST['impact_field'] : '';
- $product->update_meta_data( 'impact_field', sanitize_text_field( $title ) );
- $product->save();
- $title = isset( $_POST['certification_field'] ) ? $_POST['certification_field'] : '';
- $product->update_meta_data( 'certification_field', sanitize_text_field( $title ) );
- $product->save();
- }
- add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );
- /**
- * Display impact on the front end
- * @since 1.0.0
- */
- function wc_tab_impact() {
- global $post;
- $product = wc_get_product( $post->ID );
- printf('<p>%s</p>', esc_html( $product->get_meta( 'impact_field' )));
- }
- /**
- * Display Certification on the front end
- * @since 1.0.0
- */
- function wc_tab_certification() {
- global $post;
- $product = wc_get_product( $post->ID );
- printf('<p>%s</p>', esc_html( $product->get_meta( 'certification_field' )));
- }
- /**
- * Display Certification on the front end
- * @since 1.0.0
- */
- function wc_tab_test() {
- global $post;
- $product = wc_get_product( $post->ID );
- printf('<p>%s</p>', esc_html( $product->get_meta( 'custom_text_field' )));
- }
- /**
- * Adds custom product tabs
- * @since 1.0.0
- */
- function wc_tab_extra_tabs( $tabs ) {
- global $post;
- // Check for the custom field values
- $product = wc_get_product( $post->ID );
- if($product->get_meta( 'impact_field' )) {
- // Adds the impact tab
- $tabs['impact'] = array(
- 'title' => __( 'Impact', 'woocommerce' ),
- 'priority' => 50,
- 'callback' => 'wc_tab_impact'
- );
- }
- if($product->get_meta( 'certification_field' )) {
- // Adds the certification tab
- $tabs['certification'] = array(
- 'title' => __( 'Certification', 'woocommerce' ),
- 'priority' => 50,
- 'callback' => 'wc_tab_certification'
- );
- }
- if($product->get_meta( 'custom_text_field' )) {
- // Adds the test tab
- $tabs['certification'] = array(
- 'title' => __( 'Test', 'woocommerce' ),
- 'priority' => 50,
- 'callback' => 'wc_tab_test'
- );
- }
- return $tabs;
- }
- add_filter( 'woocommerce_product_tabs', 'wc_tab_extra_tabs' );
- /**
- * Add Custom Tab in add product page.
- * @author WC Marketplace
- * @Version 3.3.0
- */
- function cfwc_add_custom_product_data_tabs( $tabs ) {
- $tabs['advanced'] = array(
- 'label' => __( 'Custom Tab', 'cfwc' ),
- 'target' => 'custom_tab_product_data',
- 'class' => array(),
- 'priority' => 100,
- );
- return $tabs;
- }
- add_filter( 'wcmp_product_data_tabs', 'cfwc_add_custom_product_data_tabs' );
- /**
- * Add Custom Tab content in add product page.
- * @author WC Marketplace
- * @Version 3.3.0
- */
- function cfwc_add_custom_product_data_content( $pro_class_obj, $product, $post ) {
- ?>
- <div role="tabpanel" class="tab-pane fade" id="custom_tab_product_data"> <!-- just make sure tabpanel id should replace with your added tab target -->
- <div class="row-padding">
- <div class="form-group">
- <label class="control-label col-sm-3 col-md-3">Custom Text Field</label>
- <div class="col-md-6 col-sm-9">
- <input type="text" name="custom_text_field" class="form-control" />
- </div>
- </div>
- </div>
- </div>
- <?php
- }
- add_action( 'wcmp_product_tabs_content', 'cfwc_add_custom_product_data_content', 10, 3 );
- /**
- * Save Custom Tab content data.
- * @author WC Marketplace
- * @Version 3.3.0
- */
- function cfwc_save_custom_product_data( $product, $post_data ) {
- if( isset($post_data['post_ID']) && isset($post_data['custom_text_field'])){
- update_post_meta( absint( $post_data['post_ID'] ), '_custom_text_field', $post_data['custom_text_field']);
- }
- }
- add_action( 'wcmp_process_product_object', 'cfwc_save_custom_product_data', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement