Can 'customer' column be easily changed to 'company name' in reports? - woocommerce

Is there a hook that I can use to change the customer column heading to the company name?

As mentioned in the comments there are no hooks you can use here as the Analytics pages are rendered via JavaScript.
The code snippet below will add a mutation observer checking if the table on the order analytics page is changing (as these headers are being added dynamically via JavaScript). Each time a change (mutation) is registered it checks for the 'Customer' header. If it is found it is changed to 'Company name'. Perhaps not the most elegant solution but it does work.
add_action( 'admin_footer', 'woocommerce_analytics_change_customer_header' );
function woocommerce_analytics_change_customer_header() {
if ( isset( $_GET['page'] ) && isset( $_GET['path'] ) ) {
if ( $_GET['page'] == 'wc-admin' && $_GET['path'] == '/analytics/orders' ) {
?>
<script>
jQuery( function( $ ) {
// select the target node
var target = document.querySelector('#woocommerce-layout__primary');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if ( $('th.woocommerce-table__header').length ) {
$('th.woocommerce-table__header').each(function(){
let $label = $(this).find('span');
if ( $label.text() == 'Customer' ) {
$label.text('Company name');
}
});
}
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true, subtree: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
});
</script>
<?php
}
}
}
This code snippet should be added to the functions.php of your child theme or via a plugin like Code Snippets.

Related

is it possible to execute WordPress hook dynamically?

The only possible is if I try to dynamic WordPress hook tag or callback. But I've failed to dynamic both like a complete hook. The scenario is here 👇 have a look and give me a solution if it is possible.
foreach ( $option_root as $option_root_key => $option_root_value ) { // Loop through admin options.
$wp_hook_tag = $option_root_value['wp_hook_tag']; // Getting hook tag like, wp_head, wp_footer, etc.
$wp_template = $option_root_value['wp_template']; // Getting template like is_single(), is_page(), etc.
$some_page_id_selected = $option_root_value['page_ids']; // Selected Page IDs.
$custom_code = $option_root_value['custom_code']; // Custom code like '<script>alert("Hello World!");</script>'
add_action(
$wp_hook_tag,
function() use (
$acc_libraries,
$wp_template,
$custom_code ) {
if ( 'page' === get_post_type() && is_page( $some_page_id_selected ) ) {
if ( $wp_template() ) {
echo $custom_code;
}
}
}
);
}
Note: When I run this code, get_post_type() is not working.

Show / hide row WPbakery on click

I want to create a conditional logic quiz (chained quiz) with wpbakery. I want to click on a column and then show a diferent row. On click i want to hide the first row. I've already got the code for the first part but cant seem to figure out how to hide the first row.
// CSS - Add wherever you put your custom CSS.
body:not(.compose-mode) .my-hidden-row:not(.my-hidden-row--active) {
display: none;
}
// PHP - Add to child theme functions.php or via Code Snippets plugin.
add_action( 'wp_footer', function() { ?>
<script>
document.addEventListener( 'click', function( event ) {
button = event.target.closest( '.my-hidden-row-toggle' );
if ( ! button ) {
return;
}
var link = button;
if ( 'A' !== link.tagName ) {
link = button.querySelector( 'a' );
}
if ( ! link ) {
return;
}
var href = link.getAttribute( 'href' );
if ( ! href ) {
return;
}
var hiddenRow = document.querySelector( href );
if ( hiddenRow ) {
hiddenRow.classList.toggle( 'my-hidden-row--active');
}
event.preventDefault();
event.stopPropagation();
} );
</script>
<?php }, 99 );

Remove selected checkout fields with woocommerce depending on shipping method

Hello so I'm trying to figure out how to remove some billing fields using woocommerce checkout depending on the shipping method selected. So with this code I'm trying to unset the billing address, billing city, billing state and billing postcode when the customer selects local shipping but this code isn't working. Any help would be appreciated.
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:1'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_address_1']); // Add/change filed name to be hide
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_postcode']);
}
return $fields;
}
Here's how I would go about solving this problem.
This will involve php, css, and javascript (jQuery).
PHP
add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
// change below for the method
$shipping_method ='local_pickup:1';
// change below for the list of fields
$hide_fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode' );
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
// uncomment below line and reload checkout page to check current $chosen_methods
// print_r($chosen_methods);
$chosen_shipping = $chosen_methods[0];
foreach($hide_fields as $field ) {
if ($chosen_shipping == $shipping_method) {
$fields['billing'][$field]['required'] = false;
$fields['billing'][$field]['class'][] = 'hide';
}
$fields['billing'][$field]['class'][] = 'billing-dynamic';
}
return $fields;
}
Instead of unsetting the fields, we will just alter it's requiredness.
That means, if the chosen method is the one we want to check, we will not make it required. Then we will add a hide class. With this, we can hide these fields using css. And woocommerce will not throw an error that it is required. Using jQuery, we can show/hide these fields. So if we unset it on the first run, there's nothing to show because the fields are not there in the first place and with that the page needs to reload.
So here's the javascript and the css part.
add_action( 'wp_footer', 'cart_update_script', 999 );
function cart_update_script() {
if (is_checkout()) :
?>
<style>
.hide {display: none!important;}
</style>
<script>
jQuery( function( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
$(document).on( 'change', '#shipping_method input[type="radio"]', function() {
// change local_pickup:1 accordingly
$('.billing-dynamic').toggleClass('hide', this.value == 'local_pickup:1');
});
});
</script>
<?php
endif;
}

Multiple image upload in wordpress

I want to upload multiple images using wordpress.
Here is my code which is using to single upload
if ( ! function_exists( 'wp_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['photo'];
$upload_overrides = array( 'test_form' => FALSE );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
}
else {
echo "Possible file upload attack!\n";
}
How I can upload multiple images using wordpress?
Try to implement like this:
$files = $_FILES['photo'];
foreach ($files['photo'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
wp_handle_upload($file);
}
}
You might be able to achieve what you're trying to do easier by using the native WordPress Media Uploader.
I had a good resource bookmarked at one point but can't seem to find it. I ran into a situation a few months ago that required the same thing. This is what I ended up doing:
Loading the WordPress image scripts into the beginning of my PHP script:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
Then you can call the uploader on element click using jQuery like this:
jQuery(document).ready(function($) {
// Define a variable to be used to store the frame data
var file_frame;
var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
var set_to_post_id = 999; // Set this
$('#upload_button').live('click', function( event ){
element = $(this);
event.preventDefault();
// If the media frame already exists, reopen it.
if (file_frame) {
// Set the post ID to what we want
file_frame.uploader.uploader.param('post_id', set_to_post_id);
// Open frame
file_frame.open();
return;
} else {
// Set the wp.media post id so the uploader grabs the ID we want when initialised
wp.media.model.settings.post.id = set_to_post_id;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: $(this).data('uploader_title'),
button: {
text: $(this).data('uploader_button_text'),
},
multiple: true // Set to false to allow only one file to be selected
});
// When an image(s) have been selected, run a callback.
file_frame.on('select', function() {
// Do something if necessary
function_to_fire();
});
// Finally, open the modal
file_frame.open();
});
});
There is an attachment object passed to the callback function which will contain all of the IDs and such for the attachments uploaded.
The set_to_post_id is the id of the post/page you want to "attach" the media to. If you're not trying to attach them to a post, you shouldn't need to worry about this and some of the above code will not apply.

'setMapTypeId' not working with custom map (Google Maps)

I have a set of different map types (styles). As you can see in the simplified example, I try to switch the map types with an onClick event.
This works great for the default - by google.maps provided - styles. But when I try to switch to my custom style, I have a delay of ~2-3 seconds (local environment) and then get a completely grey map (nothing on the tiles).
I know that the map style itself works (not the same as in the linked example), because it's my initial map style. So only the switch back to my custom style is not working. Sadly I get nothing in the console and don't know how I could debug this.
<?php
// Inside the init(); function:
?>
var custom_style = [
{
featureType: 'water'
,stylers: [
{ hue: "#009ee0" }
,{ saturation: 100 }
,{ lightness: 0 }
]
}
];
var CUSTOMMAPTYPE = new google.maps.StyledMapType(
custom_style,
// Options:
{
alt: "Show Custom style",
name: "custom"
}
);
<?php
// Inside the view
$map_types = array(
'Roadmap'
,'Terrain'
,'Satellite'
,'Hybrid'
,'CustomMapType'
);
foreach ( $map_types as $index => $type )
{
?>
<span
class="map-type"
onClick="
my_map.setMapTypeId( google.maps.MapTypeId.<?php echo strtoupper( $type ); ?> );
// This is funny: I can access all mapType objects without a problem:
console.log( my_map.mapTypes.<?php echo strtolower( $type ); ?> );
"
id="<?php echo strtolower( $type ); ?>"
>
<?php echo $type; ?>
</span>
<?php
}
?>
Any help is highly appreciated.
Thanks in advance
EDIT:
Here's something other I tried (from inside my init(); fn, that does the setup for the map):
// The map type control DOM/UI Element
var map_type_controls = document.getElementsByClassName( 'map-type' );
// using jQuery to add a DOM listener function for click events to the UI elements:
jQuery.each( map_type_controls, function() {
google.maps.event.addDomListener( this, 'click', function() {
var control_id = jQuery( this ).attr( 'id' );
var control = control_id.toUpperCase();
// uncomment to debug:
// Shows all map type control DOM element correct
// console.log( control );
// Shows all registered mapType objects incl. the custom one correct:
// console.log( my_map.mapTypes );
my_map.setMapTypeId( google.maps.MapTypeId[ control ] );
// Per recommendation - doesn't help
google.maps.event.trigger( atlas_map, "resize" );
} );
} );
Normally when you modified a property of a map, let's say a new coordinates or a zoom, even if you change the size of the container (div), you should trigger the event called resize it's the only way to avoid this problem, now, i don't know if this works when you change the type of the map but you should try, the code it's the following:
google.maps.event.trigger(map, "resize");

Resources