I'm creating a WooCommerce Product Database for Gadgets together with ACF Custom Fields. Now what I would like to do is to simplify these codes. I'm thinking of Factory method? or other design patterns? Because these codes will be so big and cluttered in the long run as I add more Custom Product, and its Custom Fields. I have also resolved in the method of adding hidden fields as suggested from my previous question
Will appreciate if you can give me examples from these codes I have so far. Let me describe part by part so you can think of how I can do like templating these. and for reference I attached the screenshot of the final output of this when implemented.
IN THE FUTURE or Coming Soon: I'll create also the comparing of products in the page.
Also, feel free to suggest if there are parts here that can be simplified? maybe. I'll check. Thank you so much.
Create Custom WooCommerce Product Type
<?php
/**
* Plugin Name: WooCommerce Custom Product Type
* Plugin URI: http://ift.tt/2sctq08
* Description: A simple plugin to add a custom product type.
* Version: 1.0
* Author: RJ Ramirez
*/
/**
* Register the custom product type after init
*/
function register_simple_mobile_product_type() {
/**
* This should be in its own separate file.
*/
class WC_Product_Simple_Mobile extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'simple_mobile';
parent::__construct( $product );
}
}
}
add_action( 'plugins_loaded', 'register_simple_mobile_product_type' );
Add the Product Type in the Dropdown
/**
* Add to product type drop down.
*/
function add_simple_mobile_product( $types ){
// Key should be exactly the same as in the class
$types[ 'simple_mobile' ] = __( 'Simple Mobile' );
return $types;
}
add_filter( 'product_type_selector', 'add_simple_mobile_product' );
Some custom settings for the WooCommerce Product Created
/**
* Show pricing fields for simple_mobile product.
*/
function simple_mobile_custom_js() {
if ( 'product' != get_post_type() ) :
return;
endif;
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_simple_mobile' ).show();
});
</script><?php
}
add_action( 'admin_footer', 'simple_mobile_custom_js' );
/**
* Remove Product tabs from the page
*/
function woo_remove_product_tabs( $tabs = array() ) {
unset( $tabs['shipping'] ); // Remove the description tab
unset( $tabs['linked_product'] ); // Remove the reviews tab
unset( $tabs['advanced'] ); // Remove the additional information tab
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'woo_remove_product_tabs', 10 );
/**
* Hide Attributes data panel.
*/
function hide_attributes_data_panel( $tabs) {
$tabs['attribute']['class'][] = 'hide_if_simple_mobile hide_if_variable_mobile';
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'hide_attributes_data_panel' );
//Remove Tabs at the Page
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_page_tabs', 98 );
function woo_remove_product_page_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
Start adding Custom Product Tab in the WooCommerce Product Created
/**
* Add a custom product tab.
*/
function custom_product_tabs( $tabs) {
$tabs['General'] = array(
'label' => __( 'General', 'woocommerce' ),
'target' => 'general_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Launch'] = array(
'label' => __( 'Launch', 'woocommerce' ),
'target' => 'launch_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Network'] = array(
'label' => __( 'Network', 'woocommerce' ),
'target' => 'network_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Platform'] = array(
'label' => __( 'Platform', 'woocommerce' ),
'target' => 'platform_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Body'] = array(
'label' => __( 'Body', 'woocommerce' ),
'target' => 'body_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Display'] = array(
'label' => __( 'Display', 'woocommerce' ),
'target' => 'display_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Memory'] = array(
'label' => __( 'Memory', 'woocommerce' ),
'target' => 'memory_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Camera'] = array(
'label' => __( 'Camera', 'woocommerce' ),
'target' => 'camera_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
$tabs['Sound'] = array(
'label' => __( 'Sound', 'woocommerce' ),
'target' => 'sound_contents',
'class' => array( 'show_if_simple_mobile', 'show_if_variable_mobile' ),
);
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'custom_product_tabs' );
Start Adding Custom Fields in the created Custom Tabs
/**
* Contents of the mobile options product tab.
*/
function general_product_tab_content() {
global $woocommerce, $post;
//General
$label_text_model_number = __( 'Model Number', 'woocommerce');
?><div id='general_contents' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_text_input( array(
'id' => '_text_model_number',
'label' => $label_text_model_number,
'desc_tip' => 'true',
'description' => __( 'The model number of the product', 'woocommerce' ),
'type' => 'text',
) );
//Add hidden inputs for label
echo '<input type="hidden" id="text_model_number_label" name="text_model_number_label" value="'.$label_text_model_number.'" />';
?></div>
</div><?php
}
add_action( 'woocommerce_product_data_panels', 'general_product_tab_content' );
/**
* Contents of the mobile options product tab.
*/
function launch_product_tab_content() {
global $woocommerce, $post;
// Setting here your labels
$label_text_announced = __( 'Announced(Global)', 'woocommerce' );
$label_text_announced_ph = __( 'Announced(Philippines)', 'woocommerce' );
$label_text_availability_ph = __( 'Availability(Philippines)', 'woocommerce');
?><div id='launch_contents' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_text_input( array(
'id' => '_text_announced',
'label' => $label_text_announced,
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_announced_ph',
'label' => $label_text_announced_ph,
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_availability_ph',
'label' => $label_text_availability_ph,
'desc_tip' => 'true',
'description' => __( 'Schedule date of availability in the Philippines', 'woocommerce' ),
'type' => 'text',
) );
//Add hidden inputs for label
echo '<input type="hidden" id="text_announced_label" name="text_announced_label" value="'.$label_text_announced.'" />';
echo '<input type="hidden" id="text_announced_ph_label" name="text_announced_ph_label" value="'.$label_text_announced_ph.'" />';
echo '<input type="hidden" id="text_availability_ph_label" name="text_availability_ph_label" value="'.$label_text_availability_ph.'" />';
?></div>
</div><?php
}
add_action( 'woocommerce_product_data_panels', 'launch_product_tab_content' );
function network_product_tab_content() {
global $woocommerce, $post;
// Setting here your labels
$label_text_network_technology = __( 'Technology', 'woocommerce');
$label_text_network_speed = __( 'Speed', 'woocommerce');
?><div id='network_contents' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_text_input( array(
'id' => '_text_network_technology',
'label' => $label_text_network_technology,
'desc_tip' => 'true',
'description' => __( 'Schedule date of availability in the Philippines', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_network_speed',
'label' => $label_text_network_speed,
'desc_tip' => 'true',
'description' => __( 'Speed per technology category', 'woocommerce' ),
'type' => 'text',
) );
//Add hidden inputs for label
echo '<input type="hidden" id="text_network_technology_label" name="text_network_technology_label" value="'.$label_text_network_technology.'" />';
echo '<input type="hidden" id="text_network_speed_label" name="text_network_speed_label" value="'.$label_text_network_speed.'" />';
?></div>
</div><?php
}
add_action( 'woocommerce_product_data_panels', 'network_product_tab_content' );
Start adding the Save functionality of Custom fields added
/**
* Save the custom fields.
*/
function general_product_tab_save( $post_id ) {
if ( isset( $_POST['_text_model_number'] ) ) :
update_post_meta( $post_id, '_text_model_number', sanitize_text_field( $_POST['_text_model_number'] ) );
endif;
}
add_action( 'woocommerce_process_product_meta', 'general_product_tab_save' );
function launch_product_tab_save( $post_id ) {
if ( isset( $_POST['_text_announced'] ) ) :
update_post_meta( $post_id, '_text_announced', sanitize_text_field( $_POST['_text_announced'] ) );
endif;
if ( isset( $_POST['_text_announced_ph'] ) ) :
update_post_meta( $post_id, '_text_announced_ph', sanitize_text_field( $_POST['_text_announced_ph'] ) );
endif;
if ( isset( $_POST['_text_availability_ph'] ) ) :
update_post_meta( $post_id, '_text_availability_ph', sanitize_text_field( $_POST['_text_availability_ph'] ) );
endif;
}
add_action( 'woocommerce_process_product_meta', 'launch_product_tab_save' );
function network_product_tab_save( $post_id ) {
if ( isset( $_POST['_text_network_technology'] ) ) :
update_post_meta( $post_id, '_text_network_technology', sanitize_text_field( $_POST['_text_network_technology'] ) );
endif;
if ( isset( $_POST['_text_network_speed'] ) ) :
update_post_meta( $post_id, '_text_network_speed', sanitize_text_field( $_POST['_text_network_speed'] ) );
endif;
}
add_action( 'woocommerce_process_product_meta', 'network_product_tab_save' );
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['specifications_tab'] = array(
'title' => __( 'Specifications', 'woocommerce' ),
'priority' => 5,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
Start generating the Display of Custom Product in Page when embedded
function woo_new_product_tab_content() {
//GENERAL
$field_object_text_model_number = get_field_object('_text_model_number', $post->ID);
// LAUNCH
$field_object_text_announced = get_field_object('_text_announced', $post->ID);
$field_object_text_announced_ph = get_field_object('_text_announced_ph', $post->ID);
$field_object_text_availability_ph = get_field_object('_text_availability_ph', $post->ID);
/*
* get all custom fields, loop through them and create a label => value markup
*/
echo '<h2>' . get_the_title( get_the_ID() ) . '</h2>'; ?>
<div id="mobile_specs_wrapper" class="table-responsive">
<table id="table_mobile_specs" class="flat-table flat-table-3">
<thead>
</thead>
<tbody>
<tr>
<td>GENERAL or <?php echo esc_html( $field_object_text_model_number['label'] ); ?></td>
<td>1<?php echo esc_html( $field_object_text_model_number['value'] ); ?></td>
<td>2<?php echo get_post_meta(get_the_ID(),"_text_model_number_label",true); ?></td>
</tr>
<tr>
<td>LAUNCH</td>
<td>3<?php echo get_post_meta(get_the_ID(),"_text_announced_label",true); ?></td>
<td>4<?php echo esc_html( $field_object_text_announced['value'] ); ?></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3"><b>Disclaimer.</b> Information inscribed here are with utmost accuracy but not 100% guaranteed.</td>
</tr>
</tfoot>
</table>
</div>
<?php
}
Aucun commentaire:
Enregistrer un commentaire