Make table for this meta box? (wordpress) - wordpress

I have been trying to display this meta box info in my template as a table, but it isn't working out.
What I want to do is make a simple table out of it. All I need is someone to help me start. The code for the meta box is done, but I have no idea how to output it.
$prefix = 'anime_';
$anime_box = array(
'id' => 'anime-meta-box',
'title' => 'Anime Details',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Name',
'desc' => 'Add the name of the Anime in either English or Japanese(Romanji).',
'id' => $prefix . 'anname',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Genre',
'desc' => 'Is it a thriller, action/adventure, etc...',
'id' => $prefix . 'angenre',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Directed by',
'desc' => 'Name of director(s).',
'id' => $prefix . 'an_director',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Music by',
'desc' => 'Name of composer(s)',
'id' => $prefix . 'anmusic',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Studio',
'desc' => 'Studio which owns the anime.',
'id' => $prefix . 'anstudio',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Licensed by',
'desc' => 'Name of both American and Japanese license holders.',
'id' => $prefix . 'anlicense',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Network(s)',
'desc' => 'Networks which air the show in both Japan and the United States.',
'id' => $prefix . 'annetwork',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Original run',
'desc' => 'Date of when the anime first aired and when it stopped.',
'id' => $prefix . 'anrun',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Episodes',
'desc' => 'Number of episodes.',
'id' => $prefix . 'anepisodes',
'type' => 'text',
'std' => ''
),
)
);
add_action('admin_menu', 'anime_add_box');
// Add meta box
function anime_add_box() {
global $anime_box;
add_meta_box($anime_box['id'], $anime_box['title'], 'anime_show_box', $anime_box['page'], $anime_box['context'], $anime_box['priority']);
}
// Callback function to show fields in meta box
function anime_show_box() {
global $anime_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="anime_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($anime_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '"><strong>', $field['name'], ':</strong></label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br /><small>', $field['desc'],'</small>';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
add_action('save_post', 'anime_save_data');
// Save data from meta box
function anime_save_data($post_id) {
global $anime_box;
// verify nonce
if (!wp_verify_nonce($_POST['anime_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($anime_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
I tried using
<table>
<tr>
<td><?php echo get_post_meta($post->ID, "anname", true); ?></td>
</tr>
</table>
As a way to test it out one field, but it hasn't worked. Any ideas?

I believe the code you are using should be correct, however, the meta data ID you retrieving seems to be missing your prefix variable out: "anime_". Try:
<?php echo get_post_meta($post->ID, "anime_anname", true); ?>

Related

Add woocommerce variation GTIN to structured data

If a webshop has variable products, in some cases the variations have unique GTINs. In our case, it is EAN.
How to add these different GTINS to the structured data? I made a function to insert multiple "offer" in "offers", but the GTIN is not recognized here.
This is the function:
//add structured data to the markup
function set_structured_data( $markup, $product ) {
if ($product->is_type('simple')){
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
$markup['gtin8'] = get_post_meta( $product->get_id(), '_EAN', true );
}
if ($product->is_type('variable')){
$available_variations = $product->get_available_variations();
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$stock = $product->get_stock_status();
$offers[] = array(
'type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $priceuntil,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
));
}
$markup['offers'] = $offers;
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
Any ideas?
Ok after a couple of days puzzling i found the path to a solution here:
https://support.google.com/merchants/answer/6386198?hl=en
Result is that i corrected the standardized method from woocommerce and made each variation a unique product.
So first remove the markup in case of a variable product like this:
function set_structured_data( $markup, $product ) {
if ($product->is_type('variable')) {
$markup = array();
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
And then build up the ld+json script again, but then the desired way:
function set_structured_data_variable() {
global $product;
if (isset($product)){
if ($product->is_type('variable') && !empty($product)){
$available_variations = $product->get_available_variations();
$date = date("Y-m-d",strtotime(" + 3months"));
$commenttext = '';
$commentauthor = '';
$commentdate = '';
$comments = get_comments(array( 'post_id' => $product->get_id(), 'number' => '1' ));
foreach($comments as $comment) {
$commenttext = $comment->comment_content;
$commentauthor = $comment->comment_author;
$commentdate = $comment->comment_date;
}
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$gtin = 0000000000001;
if (!empty(get_post_meta( $variation_id, '_EAN', true ))){
$gtin = get_post_meta( $variation_id, '_EAN', true );
}
$stock = $product->get_stock_status();
$arrays[$i] = array(
'#context' => 'https://schema.org/',
'#type' => 'Product',
'sku' => $variproduct->get_sku(),
'gtin13' => $gtin,
'image' => get_the_post_thumbnail_url($product->get_id()),
'name' => implode(" / ", $variproduct->get_variation_attributes()),
'description' => wp_strip_all_tags(get_the_excerpt($product->get_id())),
'brand' => array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) ),
'review' => array(
'#type' => 'Review',
'reviewRating' => array (
'#type' => 'Rating',
'ratingValue' => $product->get_review_count(),
'bestRating' => '5',
'worstRating' => '1',
),
'author' => array(
'#type' => 'person',
'name' => $commentauthor,
'reviewBody' => $commenttext,
'datePublished' => $commentdate,
)),
'aggregateRating' => array (
'#type' => 'AggregateRating',
'ratingValue'=> $product->get_average_rating(),
'reviewCount' => $product->get_rating_count(),
),
'inProductGroupWithID' => $product->get_sku(),
'offers' => array(
'#type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $date,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
)));
}
echo '<script type="application/ld+json" class="buronoort">[';
$i = 0;
foreach ($arrays as $array){
$i++;
echo json_encode($array);
if ($i < array_key_last($arrays)){
echo ',';
}
}
echo ']</script>';
}
}
}
add_action('wp_head','set_structured_data_variable', 19);
Tested in the test tool for structured data and it is working.
A little concern is the product review however, i have to look into that what happens after multiple reviews.
Another concern is the EAN, i have set it standard to "0000000000001" as not all EANS are inputted yet. So this is a debatle solution, but if someone has a better idea, keep me posted.

Wordpress CPT add new overwrites existing ones

I'm working on a Wordpress projekt and added some custom post types and metaboxes. Inside my VM it all works fine, but on the server, the second CPT I add overwrites the previous one. This only happens for the CPT shops, the other ones I've added work like they should.
I've placed the code for CPT and the metaboxes in different files and included them inside my functions.php for each CPT.
Here's the file custom-post-type-shop.php for the shop
<?php
/**
* Plugin Name: BUYeinander Shops
* Version: 0.1
* Text Domain: buy_shops
**/
// Register Shop Custom Post Type
function buy_shops()
{
$labels = array(
'name' => _x('Shops', 'Post Type General Name', 'buy_shops'),
'singular_name' => _x('Shop', 'Post Type Singular Name', 'buy_shops'),
'menu_name' => __('Shops', 'buy_shops'),
'name_admin_bar' => __('Shops', 'buy_shops'),
'archives' => __('Shop Archives', 'buy_shops'),
'attributes' => __('Shop Attributes', 'buy_shops'),
'parent_item_colon' => __('Parent Shop:', 'buy_shops'),
'all_items' => __('All Shops', 'buy_shops'),
'add_new_item' => __('Add New Shop', 'buy_shops'),
'add_new' => __('Add New', 'buy_shops'),
'new_item' => __('New Shop', 'buy_shops'),
'edit_item' => __('Edit Shop', 'buy_shops'),
'update_item' => __('Update Shop', 'buy_shops'),
'view_item' => __('View Shop', 'buy_shops'),
'view_items' => __('View Shops', 'buy_shops'),
'search_items' => __('Search Shop', 'buy_shops'),
'not_found' => __('Not found', 'buy_shops'),
'not_found_in_trash' => __('Not found in Trash', 'buy_shops'),
'featured_image' => __('Shop Image', 'buy_shops'),
'set_featured_image' => __('Set Shop image', 'buy_shops'),
'remove_featured_image' => __('Remove Shop image', 'buy_shops'),
'use_featured_image' => __('Use as Shop image', 'buy_shops'),
'insert_into_item' => __('Insert into Shop', 'buy_shops'),
'uploaded_to_this_item' => __('Uploaded to this Shop', 'buy_shops'),
'items_list' => __('Shops list', 'buy_shops'),
'items_list_navigation' => __('Shops list navigation', 'buy_shops'),
'filter_items_list' => __('Filter Shops list', 'buy_shops'),
);
$args = array(
'label' => __('Shops', 'buy_shops'),
'description' => __('Shop-Eintrag', 'buy_shops'),
'labels' => $labels,
'supports' => array(
'title',
// 'editor',
// 'thumbnail',
// 'comments',
'revisions',
// 'custom-fields'
),
// 'taxonomies' => array( 'category' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-store',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
);
register_post_type('Shops', $args);
}
add_action('init', 'buy_shops', 0);
This is the file meta-box-shop.php for the metabox
<?php
// Metabox for shops
$prefix_shop = 'buy_shop_';
$regionargs = array(
'post_type' => "regionen",
'post_status' => 'publish'
);
$regionquery = new WP_Query($regionargs);
$regions = [];
if ($regionquery->have_posts()) {
while ($regionquery->have_posts()) {
$regionquery->the_post();
$regionTitle = $regionquery->post->post_title;
if (!in_array($regionTitle, $regions)) {
$regions[] = $regionTitle;
}
}
}
$shop_meta_box = array(
'id' => 'shop-meta-box',
'title' => 'Shop Informationen',
'page' => 'Shops',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Untertitel',
'desc' => '',
'id' => $prefix_shop . 'description',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Straße',
'desc' => '',
'id' => $prefix_shop . 'street',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Hausnummer',
'desc' => '',
'id' => $prefix_shop . 'housenumber',
'type' => 'number',
'std' => ''
),
array(
'name' => 'Postleitzahl',
'desc' => '',
'id' => $prefix_shop . 'zipcode',
'type' => 'number',
'std' => ''
),
array(
'name' => 'Stadt',
'desc' => '',
'id' => $prefix_shop . 'city',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Internetadresse',
'desc' => '',
'id' => $prefix_shop . 'url',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Region',
'desc' => '',
'id' => $prefix_shop . 'region',
'type' => 'select',
'std' => '',
'options' => $regions
),
array(
'name' => 'Zweigstelle',
'desc' => '',
'id' => $prefix_shop . 'branch',
'type' => 'checkbox',
'std' => ''
),
)
);
add_action('admin_menu', 'shop_add_box');
// Add meta box
function shop_add_box()
{
global $shop_meta_box;
add_meta_box($shop_meta_box['id'], $shop_meta_box['title'], 'shop_show_box', $shop_meta_box['page'], $shop_meta_box['context'], $shop_meta_box['priority']);
}
// Callback function to show fields in meta box
function shop_show_box()
{
global $shop_meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="shop_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($shop_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
break;
case 'number':
echo '<input type="number" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:50%" />', '<br />', $field['desc'];
break;
// case 'textarea':
// echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
// break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
// case 'radio':
// foreach ($field['options'] as $option) {
// echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
// }
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '</td><td>',
'</td></tr>';
}
echo '</table>';
}
add_action('save_post', 'shop_save_data');
// Save data from meta box
function shop_save_data($post_id)
{
global $shop_meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['shop_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($shop_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
/*
* Add a meta box for image upload
*/
function lacuna2_case_study_bg( $post ) {
wp_nonce_field( 'case_study_bg_submit', 'case_study_bg_nonce' );
$lacuna2_stored_meta = get_post_meta( $post->ID ); ?>
<p>
<img style="max-width:200px;height:auto;" id="meta-image-preview" src="<?php if ( isset ( $lacuna2_stored_meta['meta-image'] ) ){ echo $lacuna2_stored_meta['meta-image'][0]; } ?>" /> <br>
<input type="text" name="meta-image" id="meta-image" class="meta_image" value="<?php if ( isset ( $lacuna2_stored_meta['meta-image'] ) ){ echo $lacuna2_stored_meta['meta-image'][0]; } ?>" />
<input type="button" id="meta-image-button" class="button" value="Choose or Upload an Image" />
</p>
<script>
jQuery('#meta-image-button').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#meta-image').val(attachment.url);
jQuery('#meta-image-preview').attr('src',attachment.url);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open();
return false;
});
</script>
<?php
}
/**
* Add Case Study background image metabox to the back end of Case Study posts
*/
function lacuna2_add_meta_boxes() {
add_meta_box( 'case-study-bg', 'Shop Bilddatei', 'lacuna2_case_study_bg', 'shops', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'lacuna2_add_meta_boxes' );
/**
* Save background image metabox for Case Study posts
*/
function save_case_study_bg_meta_box( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'case_study_bg_nonce' ] ) && wp_verify_nonce( $_POST[ 'case_study_bg_nonce' ], 'case_study_bg_submit' ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-image' ] ) ) {
update_post_meta( $post_id, 'meta-image', $_POST[ 'meta-image' ] );
}
}
add_action( 'save_post', 'save_case_study_bg_meta_box' );
1) Make sure both post_types use an unique post_type key. That is the first parameter for the register_post_type function. See docs: https://developer.wordpress.org/
2) Also: make sure to use a post_type key with only lowercase characters. As I may quote the documentation:
$post_type (string) (Required) Post type key. Must not exceed 20
characters and may only contain lowercase alphanumeric characters,
dashes, and underscores.
So change:
register_post_type('Shops', $args);
into:
register_post_type('shops', $args);
In register_post_type() function, you have to put a unique post_type_key for each Custom Post Type. You're providing the same post_type_key for both Custom Post Type that's why the second one is overriding the first one. As you're using
register_post_type('shops', $args);
for the first one, you should use something else as post_type_key at the first parameter for the second Custom Post Type. You have to use anything unique for this. For example,
register_post_type('my-shop', $args);
Visit the official doc for details.

WooCommerce custom bacs payment gateway not saving bank account details

I build a plugin-based upon gateway BACS, I followed this guide
https://docs.woocommerce.com/document/payment-gateway-api/
I also found some other examples which are used some code from, maybe not a good idea.
The plugin works, it shows op as a payment method, click manage and I go the admin page, can change, update the fields and this is saved but the problem is the bank accounts table at the bottom, these are not saved and I cannot find out why.
Here is my code, please keep in mind, writing plugins is not my profession so if the mistakes are stupid, it is due to my lack of knowledge. Any help would be appreciated.
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_action('plugins_loaded', 'init_gateway_revolut_class');
function init_gateway_revolut_class()
{
/**
* Custom Payment Gateway.
*
* Provides a Custom Payment Gateway, to modify revolut formatting.
*/
class WC_Gateway_Revolut extends WC_Payment_Gateway
{
// public $domain;
public $locale;
/**
* Referral Url.
*
* #var string
*/
public $revolut_url;
/**
* Constructor for the gateway.
*/
public function __construct()
{
$this->id = 'revolut';
$this->icon = apply_filters('woocommerce_revolut_icon', '');
$this->has_fields = false;
$this->method_title = __('Revolut Multi Currency', 'woocommerce');
$this->method_description = __('Allows payments in different currencies to your Revolut bank account, more commonly known as direct bank/wire transfer.', 'woocommerce');
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->instructions = $this->get_option('instructions');
$this->referral_text = $this->get_option('referral_text');
$this->referral_link = $this->get_option('referral_link');
$this->revolut_url = $this->revolut_url;
$this->order_status = $this->get_option('order_status');
$this->status_text = $this->get_option('status_text');
if (!empty($this->referral_link)) {
$this->description .= ' ' . $this->get_referral_link();
// $this->description = trim( $this->description );
}
// revolut account fields shown on the thanks page and in emails
$this->account_details = get_option('woocommerce_revolut_accounts', array(
array(
'currency' => $this->get_option('currency'),
'account_name' => $this->get_option('account_name'),
'account_number' => $this->get_option('account_number'),
'bank_name' => $this->get_option('bank_name'),
'sort_code' => $this->get_option('sort_code'),
'iban' => $this->get_option('iban'),
'bic' => $this->get_option('bic')
)
));
// Actions
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(
$this,
'process_admin_options'
));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(
$this,
'save_account_details'
));
add_action('woocommerce_thankyou_direct_deposit', array(
$this,
'thankyou_page'
));
// Customer Emails
add_action('woocommerce_email_before_order_table', array(
$this,
'email_instructions'
), 10, 3);
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Bank Transfer', 'woocommerce'),
'default' => 'no'
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('Revolut Direct Bank Transfer', 'woocommerce'),
'desc_tip' => true
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'description' => __('Payment method description that the customer will see on your checkout. Link to open new account will be added.', 'woocommerce'),
'default' => __('Do your payment directly into TeCuro\'s Ltd. bank account. If you are a Revolut account holder, payment will be instant. If you open a new account you will receive an extra discount.', 'woocommerce'),
'desc_tip' => true
),
'instructions' => array(
'title' => __('Instructions', 'woocommerce'),
'type' => 'textarea',
'description' => __('Instructions that will be added to the thank you page and emails.', 'woocommerce'),
'default' => __('Please use your Order ID as the payment reference. When the funds have cleared in our account your order will be shipped.', 'woocommerce'),
'desc_tip' => true
),
'referral_text' => array(
'title' => __('Referal Link', 'woocommerce'),
'type' => 'text',
'description' => __('This is the text link to Revolut.', 'woocommerce'),
'default' => __('Click here to open a free account with Revolut', 'woocommerce'),
'desc_tip' => true
),
'referral_link' => array(
'title' => __('Referal Link', 'woocommerce'),
'type' => 'text',
'description' => __('Please paste your referal code you received from Revolut. When not empty referral link will be added.', 'woocommerce'),
'default' => '',
'desc_tip' => true
),
'order_status' => array(
'title' => __('Order Status', 'woocommerce'),
'type' => 'select',
'description' => __('Choose whether order status you wish after checkout.', 'woocommerce'),
'default' => 'wc-on-hold',
'desc_tip' => true,
'class' => 'wc-enhanced-select',
'options' => wc_get_order_statuses()
),
'status_text' => array(
'title' => __('Order Status Text', 'woocommerce'),
'type' => 'text',
'description' => __('Set the text for the selected order status.', 'woocommerce'),
'default' => __('Awaiting Revolut payment', 'woocommerce'),
'desc_tip' => true
),
'account_details' => array(
'type' => 'account_details'
)
);
}
/**
* Generate account details html.
*
* #return string
*/
public function generate_account_details_html()
{
ob_start();
$country = WC()->countries->get_base_country();
$locale = $this->get_country_locale();
// Get sortcode label in the $locale array and use appropriate one
$sortcode = isset($locale[$country]['sortcode']['label']) ? $locale[$country]['sortcode']['label'] : __('Sort Code', 'woocommerce');
?>
<tr valign="top">
<th scope="row" class="titledesc"><?php _e('Account Details', 'woocommerce');?>:</th>
<td class="forminp" id="revolut_accounts">
<div class="wc_input_table_wrapper">
<table class="widefat wc_input_table sortable" cellspacing="0">
<thead>
<tr>
<th class="sort"> </th>
<th><?php _e('Currency', 'woocommerce');?></th>
<th><?php _e('Account Name', 'woocommerce');?></th>
<th><?php _e('Account Number', 'woocommerce');?></th>
<th><?php _e('Bank', 'woocommerce');?></th>
<th><?php echo $sortcode;?></th>
<th><?php _e('IBAN', 'woocommerce');?></th>
<th><?php _e('BIC / Swift', 'woocommerce');?></th>
</tr>
</thead>
<tbody class="accounts">
<?php
$i = -1;
if ($this->account_details) {
foreach ($this->account_details as $account) {
$i++;
echo '<tr class="account">
<td class="sort"></td>
<td><select class="wc-enhanced-select" name="revolut_currency[' . $i . ']" id="revolut_currency">
<option value="">Select</option>
<option ' . ($account['currency'] == 'AUD') ? 'selected' : '' . ' value="AUD">AUD</option>
<option ' . ($account['currency'] == 'BGN') ? 'selected' : '' . ' value="BGN">BGN</option>
<option ' . ($account['currency'] == 'CAD') ? 'selected' : '' . ' value="CAD">CAD</option>
<option ' . ($account['currency'] == 'CHF') ? 'selected' : '' . ' value="CHF">CHF</option>
<option ' . ($account['currency'] == 'CZK') ? 'selected' : '' . ' value="CZK">CZK</option>
<option ' . ($account['currency'] == 'CHF') ? 'selected' : '' . ' value="CHF">CHF</option>
<option ' . ($account['currency'] == 'DKK') ? 'selected' : '' . ' value="DKK">DKK</option>
<option ' . ($account['currency'] == 'EUR') ? 'selected' : '' . ' value="EUR">EUR</option>
<option ' . ($account['currency'] == 'GBP') ? 'selected' : '' . ' value="GBP">GBP</option>
<option ' . ($account['currency'] == 'HKD') ? 'selected' : '' . ' value="HKD">HKD</option>
<option ' . ($account['currency'] == 'HRK') ? 'selected' : '' . ' value="HRK">HRK</option>
<option ' . ($account['currency'] == 'HUF') ? 'selected' : '' . ' value="HUF">HUF</option>
<option ' . ($account['currency'] == 'JPY') ? 'selected' : '' . ' value="JPY">JPY</option>
<option ' . ($account['currency'] == 'NOK') ? 'selected' : '' . ' value="NOK">NOK</option>
<option ' . ($account['currency'] == 'PLN') ? 'selected' : '' . ' value="PLN">PLN</option>
<option ' . ($account['currency'] == 'RON') ? 'selected' : '' . ' value="RON">RON</option>
<option ' . ($account['currency'] == 'SEK') ? 'selected' : '' . ' value="SEK">SEK</option>
<option ' . ($account['currency'] == 'USD') ? 'selected' : '' . ' value="USD">USD</option>
<option ' . ($account['currency'] == 'ZAR') ? 'selected' : '' . ' value="ZAR">ZAR</option>
</select></td>
<td><input type="text" value="' . esc_attr(wp_unslash($account['account_name'])) . '" name="revolut_account_name[' . $i . ']" /></td>
<td><input type="text" value="' . esc_attr($account['account_number']) . '" name="revolut_account_number[' . $i . ']" /></td>
<td><input type="text" value="' . esc_attr(wp_unslash($account['bank_name'])) . '" name="revolut_bank_name[' . $i . ']" /></td>
<td><input type="text" value="' . esc_attr($account['sort_code']) . '" name="revolut_sort_code[' . $i . ']" /></td>
<td><input type="text" value="' . esc_attr($account['iban']) . '" name="revolut_iban[' . $i . ']" /></td>
<td><input type="text" value="' . esc_attr($account['bic']) . '" name="revolut_bic[' . $i . ']" /></td>
</tr>';
}
}
?>
</tbody>
<tfoot>
<tr>
<th colspan="8"><?php _e('+ Add Account', 'woocommerce');?>
<?php _e('Remove selected account(s)', 'woocommerce');?></th>
</tr>
</tfoot>
</table>
</div>
<script type="text/javascript">
jQuery(function() {
jQuery('#revolut_accounts').on( 'click', 'a.add', function(){
var size = jQuery('#revolut_accounts').find('tbody .account').length;
var accountname = jQuery("#revolut_accounts tr:nth-child(1) td:nth-child(3) input[type='text']").val();
if(accountname == null){
accountname = '';
} else{
accountname = accountname;
}
jQuery('<tr class="account">\
<td class="sort"></td>\
<td><select class="wc-enhanced-select" name="revolut_currency[' + size + ']">\
<option value="">↓</option>\
<option value="AUD">AUD</option>\
<option value="BGN">BGN</option>\
<option value="CAD">CAD</option>\
<option value="CHF">CHF</option>\
<option value="CZK">CZK</option>\
<option value="CHF">CHF</option>\
<option value="DKK">DKK</option>\
<option value="EUR">EUR</option>\
<option value="GBP">GBP</option>\
<option value="HKD">HKD</option>\
<option value="HRK">HRK</option>\
<option value="HUF">HUF</option>\
<option value="JPY">JPY</option>\
<option value="NOK">NOK</option>\
<option value="PLN">PLN</option>\
<option value="RON">RON</option>\
<option value="SEK">SEK</option>\
<option value="USD">USD</option>\
<option value="ZAR">ZAR</option>\
</select>\
</td>\
<td><input type="text" value="' + accountname + '" name="revolut_account_name[' + size + ']" /></td>\
<td><input type="text" name="revolut_account_number[' + size + ']" /></td>\
<td><input type="text" value="REVOLUT" name="revolut_bank_name[' + size + ']"/></td>\
<td><input type="text" name="revolut_sort_code[' + size + ']" /></td>\
<td><input type="text" name="revolut_iban[' + size + ']" /></td>\
<td><input type="text" value="REVOGB21" name="revolut_bic[' + size + ']" /></td>\
</tr>').appendTo('#revolut_accounts table tbody');
return false;
});
});
</script>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Save account details table.
*/
public function save_account_details()
{
$accounts = array();
if (isset($_POST['revolut_currency']) && isset($_POST['revolut_account_name']) && isset($_POST['revolut_account_number']) && isset($_POST['revolut_bank_name']) && isset($_POST['revolut_sort_code']) && isset($_POST['revolut_iban']) && isset($_POST['revolut_bic'])) {
$currencies = array_map('wc_clean', $_POST['revolut_currency']);
$account_names = array_map('wc_clean', $_POST['revolut_account_name']);
$account_numbers = array_map('wc_clean', $_POST['revolut_account_number']);
$bank_names = array_map('wc_clean', $_POST['revolut_bank_name']);
$sort_codes = array_map('wc_clean', $_POST['revolut_sort_code']);
$ibans = array_map('wc_clean', $_POST['revolut_iban']);
$bics = array_map('wc_clean', $_POST['revolut_bic']);
foreach ($account_names as $i => $name) {
if (!isset($account_names[$i])) {
continue;
}
$accounts[] = array(
'currency' => $currencies[$i],
'account_name' => $account_names[$i],
'account_number' => $account_numbers[$i],
'bank_name' => $bank_names[$i],
'sort_code' => $sort_codes[$i],
'iban' => $ibans[$i],
'bic' => $bics[$i]
);
}
}
update_option('woocommerce_revolut_accounts', $accounts);
}
/**
* Return the referral_link for admin screens.
*
* #return url
*/
public function get_referral_link()
{
$link = $this->referral_link; //get_option( 'referral_link', true );
$text = $this->referral_text; //get_option( 'referral_text', true );
if (!empty($this->referral_link)) {
$revolut_url = sprintf('%s', $link, $text);
} else {
$revolut_url = sprintf('%s', $text);
}
return $revolut_url;
}
// apply_filters( 'woocommerce_get_referral_link', $referral_link );
/**
* Output the input fields in checkout.
* todo: add input box for return code Revolut
*/
/*
/**
* Output for the order received page.
*
* #param int $order_id
*/
public function thankyou_page($order_id)
{
if ($this->instructions) {
echo wpautop(wptexturize(wp_kses_post($this->instructions)));
}
$this->bank_details($order_id);
}
/**
* Add content to the WC emails.
*
* #param WC_Order $order
* #param bool $sent_to_admin
* #param bool $plain_text
*/
public function email_instructions($order, $sent_to_admin, $plain_text = false)
{
/*
if ( ! $sent_to_admin && 'revolut' === $order->payment_method && $order->has_status( 'on-hold' ) ) {
if ( $this->instructions ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
$this->bank_details( $order->id );
}
*/
if ($this->instructions && !$sent_to_admin && $this->id === $order->get_payment_method() && $order->has_status($this->order_status)) {
echo wpautop(wptexturize($this->instructions)) . PHP_EOL;
}
$this->bank_details($order->id);
}
/**
* Get bank details and place into a list format.
*
* #param int $order_id
*/
private function bank_details($order_id = '')
{
if (empty($this->account_details)) {
return;
}
// Get order and store in $order
$order = wc_get_order($order_id);
// Get the order country and country $locale
$country = $order->billing_country;
$locale = $this->get_country_locale();
// Get sortcode label in the $locale array and use appropriate one
$sortcode = isset($locale[$country]['sortcode']['label']) ? $locale[$country]['sortcode']['label'] : __('Sort Code', 'woocommerce');
$revolut_accounts = apply_filters('woocommerce_revolut_accounts', $this->account_details);
if (!empty($revolut_accounts)) {
echo '<h2 class="wc-revolut-bank-details-heading">' . __('Our Bank Details', 'woocommerce') . '</h2>' . PHP_EOL;
foreach ($revolut_accounts as $revolut_account) {
$revolut_account = (object) $revolut_account;
if ($revolut_account->account_name || $revolut_account->bank_name) {
echo '<h3>' . wp_unslash(implode(' - ', array_filter(array(
$revolut_account->account_name,
$revolut_account->bank_name
)))) . '</h3>' . PHP_EOL;
}
echo '<ul class="wc-revolut-bank-details order_details revolut_details">' . PHP_EOL;
//echo 'PAYMENT DETAILS RIGHT HERE!!!';
// revolut account fields shown on the thanks page and in emails
$account_fields = apply_filters('woocommerce_revolut_account_fields', array(
'currency' => array(
'label' => __('Currency', 'woocommerce'),
'value' => $revolut_account->currency
),
'bank_name' => array(
'label' => __('Bank Name', 'woocommerce'),
'value' => $revolut_account->bank_name
),
'sort_code' => array(
'label' => $sortcode,
'value' => $revolut_account->sort_code
),
'account_name' => array(
'label' => __('Account Name', 'woocommerce'),
'value' => $revolut_account->account_name
),
'account_number' => array(
'label' => __('Account Number', 'woocommerce'),
'value' => $revolut_account->account_number
),
'iban' => array(
'label' => __('IBAN', 'woocommerce'),
'value' => $revolut_account->iban
),
'bic' => array(
'label' => __('BIC', 'woocommerce'),
'value' => $revolut_account->bic
)
), $order_id);
foreach ($account_fields as $field_key => $field) {
if (!empty($field['value'])) {
echo '<li class="' . esc_attr($field_key) . '">' . esc_attr($field['label']) . ': <strong>' . wptexturize($field['value']) . '</strong></li>' . PHP_EOL;
}
}
echo '</ul>';
}
}
}
/**
* Process the payment and return the result.
*
* #param int $order_id
* #return array
*/
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
// Mark as on-hold (we're awaiting the payment)
//$order->update_status( 'on-hold', __( 'Awaiting Revolut payment', 'woocommerce' ) );
if ($order->get_total() > 0) {
// Mark as on-hold (we're awaiting the payment).
// $order->update_status( apply_filters( 'woocommerce_bacs_process_payment_order_status', 'on-hold', $order ), __( 'Awaiting BACS payment', 'woocommerce' ) );
$order->update_status($this->order_status, $this->status_text);
} else {
$order->payment_complete();
}
// Reduce stock levels
//$order->reduce_order_stock();
wc_reduce_stock_levels($order->get_id());
// Remove cart
WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order)
);
}
/**
* Get country locale if localized.
*
* #return array
*/
public function get_country_locale()
{
if (empty($this->locale)) {
// Locale information to be used - only those that are not 'Sort Code'
$this->locale = apply_filters('woocommerce_get_revolut_locale', array(
'AU' => array(
'sortcode' => array(
'label' => __('BSB', 'woocommerce')
)
),
'CA' => array(
'sortcode' => array(
'label' => __('Bank Transit Number', 'woocommerce')
)
),
'IN' => array(
'sortcode' => array(
'label' => __('IFSC', 'woocommerce')
)
),
'IT' => array(
'sortcode' => array(
'label' => __('Branch Sort', 'woocommerce')
)
),
'NZ' => array(
'sortcode' => array(
'label' => __('Bank Code', 'woocommerce')
)
),
'SE' => array(
'sortcode' => array(
'label' => __('Bank Code', 'woocommerce')
)
),
'US' => array(
'sortcode' => array(
'label' => __('Routing Number', 'woocommerce')
)
),
'ZA' => array(
'sortcode' => array(
'label' => __('Branch Code', 'woocommerce')
)
)
));
}
return $this->locale;
}
}
}
add_filter('woocommerce_payment_gateways', 'add_gateway_revolut_class');
function add_gateway_revolut_class($methods)
{
$methods[] = 'WC_Gateway_Revolut';
return $methods;
}
}
Okay found the mistake, the select is not working, changed it to input with a datalist. Works now and accounts are saved.

WooCommerce add heading at start of custom fields

I have created custom fields in my WooCommerce checkout page billing form. It all works fine but I am trying to add an h3 element with text in between. So basically I ask for some additional information in the billing form, but I want to give that a heading.
I tried to create a h3 dynamically with Javascript/jQuery and insert before the specific id I want it to be. But this didn't work as I liked and I rather have a server-side solution.
Thanks in advance!
Here's the function where I define my custom fields. I've tried an echo at the beginning but it ends up displaying at the top of the entire form.
// Modify billing fields
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['billing']['billing_email']);
//echo '<h3>Wie is de verzender?</h3>';
$fields['billing']['name_sender'] = array(
'label' => __('Uw naam', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['email_sender'] = array(
'label' => __('Uw email', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['phone_sender'] = array(
'label' => __('Uw telefoonnummer', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['billing']['anoniem'] = array(
'label' => __('Anoniem verzenden?', 'woocommerce'),
'type' => 'checkbox',
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
A visual explanation:
Visual explanation
You can hook into the woocommerce_form_field_<field_type> filter. This allows us to add HTML output for an additional field type (and in this case we can output HTML for a heading instead) and we can use this new heading "field type" when modifying checkout fields with the woocommerce_checkout_fields filter.
// Add field type to WooCommerce form field
add_filter( 'woocommerce_form_field_heading','sc_woocommerce_form_field_heading', 10, 4 );
function sc_woocommerce_form_field_heading($field, $key, $args, $value) {
$output = '<h3 class="form-row form-row-wide">'.__( $args['label'], 'woocommerce' ).'</h3>';
echo $output;
}
// Modify checkout fields
add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_heading_name'] = array(
'type' => 'heading',
'label' => 'Heading here',
);
When modifying fields using the woocommerce_checkout_fields filter you can now place your new heading field in any position you need.
function filter_woocommerce_form_field_radio( $field, $key, $args, $value ) {
// Based on key
if ( $key == 'radio_choice' ) {
if ( ! empty( $args['options'] ) ) {
$field = '<div><h1>Heading</h1><ul>';
foreach ( $args['options'] as $option_key => $option_text ) {
$field .= '<li>';
$field .= '<input type="radio" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" />';
$field .= '<label>' . esc_html( $option_text ) . '</label>';
$field .= '</li>';
}
$field .= '</ul></div>';
}
}
return $field;
}
add_filter( 'woocommerce_form_field_radio', 'filter_woocommerce_form_field_radio', 10, 4 );

My metabox data are not saving

I am facing a problem which start to drive me nuts.I have created a metabox for my wordpress site. For the testings, I used it on a page. Everything went fine, without problems, but when I try to use the very same metabox in the media file (attachment) I am not able to save the datas. The metabox is properly displayed there, but I am unable to save any datas I enter in.
I must be missing something but can't figure out what.
Thanks in advance for your help
Kind regards
Alain
Here is the code:
<?php
$meta_box = array(
'id' => 'onzeroadagain-meta-box',
'title' => 'Prints size and price availablility',
'page' => 'attachment',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Small',
'id' => 'small-checkbox',
'type' => 'checkbox'
),
array(
'name' => 'Dimensions',
'id' => 'dimension-small',
'type' => 'text',
'std' => 'W x H in cm'
),
array(
'name' => 'Price',
'id' => 'price-small',
'type' => 'text',
'std' => 'in Euro'
),
array(
'name' => 'Medium',
'id' => 'medium-checkbox',
'type' => 'checkbox'
),
array(
'name' => 'Dimensions',
'id' => 'dimension-medium',
'type' => 'text',
'std' => 'W x H in cm'
),
array(
'name' => 'Price',
'id' => 'price-medium',
'type' => 'text',
'std' => 'in Euro'
),
array(
'name' => 'Large',
'id' => 'large-checkbox',
'type' => 'checkbox'
),
array(
'name' => 'Dimensions',
'id' => 'dimension-large',
'type' => 'text',
'std' => 'W x H in cm'
),
array(
'name' => 'Price',
'id' => 'price-large',
'type' => 'text',
'std' => 'in Euro'
)
)
);
add_action('admin_menu', 'onzeroadagain_add_box');
// Add meta box
function onzeroadagain_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'onzeroadagain_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function onzeroadagain_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="onzeroadagain_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:30%" />', '<br />', $field['desc'];
break;
}
echo '</td><td>',
'</td></tr>';
}
echo '</table>';
}
add_action('save_post', 'onzeroadagain_save_data');
// Save data from meta box
function onzeroadagain_save_data($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['onzeroadagain_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
I encountered the same problem lately. I had two different sets of metaboxes, one on the side and one on the 'normal' column. The fields in my 'side' metaboxes were saving fine but not the ones in 'normal'. I tried changing the context of my second one to 'side' instead of 'normal' and BAM! Everything saved fine.
So I suggest you try that too and tell us if it works.
I did some research but I found no explanation to this. There might be something I don't undestand and do the wrong way, or could be some kind of bug. I'll keep investigating.
Hi modify your code like below:
add_action('save_post', 'onzeroadagain_save_data');
add_action( 'edit_attachment', 'onzeroadagain_save_data' );
The Key is:
'edit_attachment'

Resources