Subscription_legacy_info
class BAM_Legacy_Meta_Box {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'bam_legacy',
__( 'Legacy information', 'dagens_woocommerce_tweak' ),
array( $this, 'render_metabox' ),
'shop_subscription',
'side',
'default'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'bam_legacy_nonce_action', 'bam_legacy_nonce' );
// Retrieve an existing value from the database.
$bam_legacy_nav_number = get_post_meta( $post->ID, 'bam_legacy_nav_number', true );
$bam_legacy_full_billing_name = get_post_meta( $post->ID, 'bam_legacy_full_billing_name', true );
$bam_legacy_full_shipping_name = get_post_meta( $post->ID, 'bam_legacy_full_shipping_name', true );
$bam_legacy_shipping_name2 = get_post_meta( $post->ID, 'bam_legacy_shipping_name2', true );
// Set default values.
if( empty( $bam_legacy_nav_number ) ) $bam_legacy_nav_number = '';
if( empty( $bam_legacy_full_billing_name ) ) $bam_legacy_full_billing_name = '';
if( empty( $bam_legacy_full_shipping_name ) ) $bam_legacy_full_shipping_name = '';
if( empty( $bam_legacy_shipping_name2 ) ) $bam_legacy_shipping_name2 = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="bam_legacy_nav_number" class="bam_legacy_nav_number_label">' . __( 'Subscription number (legacy)', 'dagens_woocommerce_tweak' ) . '</label></th>';
echo ' <td>';
echo ' <input type="number" id="bam_legacy_nav_number" name="bam_legacy_nav_number" class="bam_legacy_nav_number_field" placeholder="' . esc_attr__( '', 'dagens_woocommerce_tweak' ) . '" value="' . esc_attr( $bam_legacy_nav_number ) . '">';
echo ' <p class="description">' . __( 'Add legacy subscription number from Navision', 'dagens_woocommerce_tweak' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="bam_legacy_full_billing_name" class="bam_legacy_full_billing_name_label">' . __( 'Full Billing Name (legacy)', 'dagens_woocommerce_tweak' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="bam_legacy_full_billing_name" name="bam_legacy_full_billing_name" class="bam_legacy_full_billing_name_field" placeholder="' . esc_attr__( '', 'dagens_woocommerce_tweak' ) . '" value="' . esc_attr( $bam_legacy_full_billing_name ) . '">';
echo ' <p class="description">' . __( 'Add legacy full billing name from Navision', 'dagens_woocommerce_tweak' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="bam_legacy_full_shipping_name" class="bam_legacy_full_shipping_name_label">' . __( 'Full Shipping Name (legacy)', 'dagens_woocommerce_tweak' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="bam_legacy_full_shipping_name" name="bam_legacy_full_shipping_name" class="bam_legacy_full_shipping_name_field" placeholder="' . esc_attr__( '', 'dagens_woocommerce_tweak' ) . '" value="' . esc_attr( $bam_legacy_full_shipping_name ) . '">';
echo ' <p class="description">' . __( 'Add legacy full shipping name from Navision', 'dagens_woocommerce_tweak' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="bam_legacy_shipping_name2" class="bam_legacy_shipping_name2_label">' . __( 'Shipping Name2 (legacy)', 'dagens_woocommerce_tweak' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="bam_legacy_shipping_name2" name="bam_legacy_shipping_name2" class="bam_legacy_shipping_name2_field" placeholder="' . esc_attr__( '', 'dagens_woocommerce_tweak' ) . '" value="' . esc_attr( $bam_legacy_shipping_name2 ) . '">';
echo ' <p class="description">' . __( 'Add legacy shipping name2 from Navision', 'dagens_woocommerce_tweak' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['bam_legacy_nonce'] ) ? $_POST['bam_legacy_nonce'] : '';
$nonce_action = 'bam_legacy_nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Sanitize user input.
$bam_legacy_new_nav_number = isset( $_POST[ 'bam_legacy_nav_number' ] ) ? floatval( $_POST[ 'bam_legacy_nav_number' ] ) : '';
$bam_legacy_new_full_billing_name = isset( $_POST[ 'bam_legacy_full_billing_name' ] ) ? sanitize_text_field( $_POST[ 'bam_legacy_full_billing_name' ] ) : '';
$bam_legacy_new_full_shipping_name = isset( $_POST[ 'bam_legacy_full_shipping_name' ] ) ? sanitize_text_field( $_POST[ 'bam_legacy_full_shipping_name' ] ) : '';
$bam_legacy_new_shipping_name2 = isset( $_POST[ 'bam_legacy_shipping_name2' ] ) ? sanitize_text_field( $_POST[ 'bam_legacy_shipping_name2' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'bam_legacy_nav_number', $bam_legacy_new_nav_number );
update_post_meta( $post_id, 'bam_legacy_full_billing_name', $bam_legacy_new_full_billing_name );
update_post_meta( $post_id, 'bam_legacy_full_shipping_name', $bam_legacy_new_full_shipping_name );
update_post_meta( $post_id, 'bam_legacy_shipping_name2', $bam_legacy_new_shipping_name2 );
}
}
new BAM_Legacy_Meta_Box;