woocommerce order details dashboard custom text - woocommerce

I am currently using this to add extra information on the order page details. it is working perfect however i want to move it to be under the box where u add notes to customers.
Want the "fast responses" to be under the add note box ( as seen on the picture ), so i can just copy the responses fast when i need them to send to customers. either like that or in a box under it. but i cant find any hooks to talk to the order notes, or after order notes.
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Fast responses' ); ?></h4>
<?php
echo '<p><strong>' . __( '2 parcel shippment' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
echo '<p><strong>' . __( 'You incorrect adress' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>';
echo '<p><strong>' . __( 'Your payment failed' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

You can add a meta box under the order notes then print your response in it.
try out this code.
// Adding Meta container admin shop_order pages
add_action('add_meta_boxes', 'zillion_add_meta_boxes');
if (!function_exists('zillion_add_meta_boxes')) {
function zillion_add_meta_boxes()
{
add_meta_box('zillion_other_fields', __('My Response', 'woocommerce'), 'zillion_add_other_fields_for_packaging', 'shop_order', 'side', 'core');
}
}
// Adding Meta field in the meta container admin shop_order pages
if (!function_exists('zillion_add_other_fields_for_packaging')) {
function zillion_add_other_fields_for_packaging()
{
global $post;
?>
<div class="order_data_column">
<h4><?php _e('Fast responses'); ?></h4>
<?php
echo '<p><strong>' . __('2 parcel shippment') . ':</strong>' . get_post_meta($post->id, '_some_field', true) . '</p>';
echo '<p><strong>' . __('You incorrect adress') . ':</strong>' . get_post_meta($post->id, '_another_field', true) . '</p>';
echo '<p><strong>' . __('Your payment failed') . ':</strong>' . get_post_meta($post->id, '_another_field', true) . '</p>'; ?>
</div>
<?php
}
}

Related

Change "no shipping available" message based on selected available shipping country

I'm currently adding the below code in woocommerce/cart/cart-shipping.php to change the no shipping method message for a country that is not in the shipping country list of WooCommerce.
<?php
add_filter( 'woocommerce_cart_no_shipping_available_html', 'custom_no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'custom_no_shipping_available_html' );
function custom_no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( "Unfortunately, we don't ship to %s. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
return sprintf( "Unfortunately, we don't ship to this location. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
?>
After this piece of code the default code in cart-shipping.php is:
$formatted_destination = isset($formatted_destination) ? $formatted_destination : WC()->countries->get_formatted_address($package['destination'], ', ');
$has_calculated_shipping = !empty($has_calculated_shipping);
$show_shipping_calculator = !empty($show_shipping_calculator);
$calculator_text = '';
?>
<tr class="woocommerce-shipping-totals shipping">
<th><?php echo wp_kses_post($package_name); ?></th>
<td data-title="<?php echo esc_attr($package_name); ?>">
<?php if ($available_methods) : ?>
<ul id="shipping_method" class="woocommerce-shipping-methods">
<?php foreach ($available_methods as $method) : ?>
<li>
<?php
if (1 < count($available_methods)) {
printf('<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id), checked($method->id, $chosen_method, false)); // WPCS: XSS ok.
} else {
printf('<input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id)); // WPCS: XSS ok.
}
printf('<label for="shipping_method_%1$s_%2$s">%3$s</label>', $index, esc_attr(sanitize_title($method->id)), wc_cart_totals_shipping_method_label($method)); // WPCS: XSS ok.
do_action('woocommerce_after_shipping_rate', $method, $index);
?>
</li>
<?php endforeach; ?>
</ul>
<?php if (is_cart()) : ?>
<p class="woocommerce-shipping-destination">
<?php
if ($formatted_destination) {
// Translators: $s shipping destination.
printf(esc_html__('Shipping to %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>');
$calculator_text = esc_html__('Change address', 'woocommerce');
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_estimate_html', __('Shipping options will be updated during checkout.', 'woocommerce')));
}
?>
</p>
<?php endif; ?>
<?php
elseif (!$has_calculated_shipping || !$formatted_destination) :
if (is_cart() && 'no' === get_option('woocommerce_enable_shipping_calc')) {
echo wp_kses_post(apply_filters('woocommerce_shipping_not_enabled_on_cart_html', __('Shipping costs are calculated during checkout.', 'woocommerce')));
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_may_be_available_html', __('Enter your address to view shipping options.', 'woocommerce')));
}
elseif (!is_cart()) :
echo wp_kses_post(apply_filters('woocommerce_no_shipping_available_html', __('There are no shipping options available. Please ensure that your address has been entered correctly, or Please contact our Support if you need any help.', 'woocommerce')));
else :
// Translators: $s shipping destination.
echo wp_kses_post(apply_filters('woocommerce_cart_no_shipping_available_html', sprintf(esc_html__('No shipping options were found for %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>')));
$calculator_text = esc_html__('Enter a different address', 'woocommerce');
endif;
?>
<?php if ($show_package_details) : ?>
<?php echo '<p class="woocommerce-shipping-contents"><small>' . esc_html($package_details) . '</small></p>'; ?>
<?php endif; ?>
<?php if ($show_shipping_calculator) : ?>
<?php woocommerce_shipping_calculator($calculator_text); ?>
<?php endif; ?>
</td>
</tr>
But the problem is that I have to complete all the billing address fields including phone and zip code to get my message that the selected country is not supported.
I think this happens because by default WooCommerce is checking the $formatted_destination variable set in the same PHP file.
My question is how can I have the message in a way that is directly shown once the country is selected and it is not part of the shipping country?
To answer your question, first some explanation. I copied a piece of code from templates/cart/cart-shipping.php line 66 - 72 #version 3.6.0
elseif ( ! is_cart() ) :
echo wp_kses_post( apply_filters( 'woocommerce_no_shipping_available_html', __( 'There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.', 'woocommerce' ) ) );
else :
// Translators: $s shipping destination.
echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for %s.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) );
$calculator_text = esc_html__( 'Enter a different address', 'woocommerce' );
endif;
As you can see this contains 2 filters hook, namely:
woocommerce_no_shipping_available_html
woocommerce_cart_no_shipping_available_html
While the second hook, in the message shows $formatted_destination, the first one doesn't.
So to answer your question, you can use the hooks like this:
The message displayed on the cart page
function filter_woocommerce_cart_no_shipping_available_html( $sprintf ) {
// Get packages
$packages = WC()->shipping()->get_packages();
// Loop through
foreach ( $packages as $i => $package ) {
$formatted_destination = WC()->countries->get_formatted_address( $package['destination'], ', ' );
}
// Message
$sprintf = sprintf( esc_html__( 'Unfortunately, we don\'t ship to %s. Please contact our Support if you need any help.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' );
return $sprintf;
}
add_filter( 'woocommerce_cart_no_shipping_available_html', 'filter_woocommerce_cart_no_shipping_available_html', 10, 1 );
The message displayed on the checkout page
function filter_woocommerce_no_shipping_available_html( $html ) {
// Message
$html = __( 'My new message', 'woocommerce' )
return $html;
}
add_filter( 'woocommerce_no_shipping_available_html', 'filter_woocommerce_no_shipping_available_html', 10, 1 );
With both answers there is no need to modify/overwrite template files, code goes in functions.php file of the active child theme (or active theme).

How to add nofollow to frontpage only? (Woocommerce products, internal links)

Currently it add nofollow at all products across website, how to make it at is_front_page() only?..
function woocommerce_template_loop_product_title() {
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Change the value of your rel attribute. So, when the user is at the frontpage, rel will have a value of nofollow. Otherwise, the value will be "" empty.
<?php
function woocommerce_template_loop_product_title() {
// Add the $nofollow
$nofollow = "";
if(is_front_page() ){
$nofollow = "nofollow";
}
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '"><a href="' . get_the_permalink() . '" rel='.$nofollow.'>' . get_the_title() . '</a></p>';
?>

Insert custom content in WooCommerce

I have some problem in WooCommerce Admin Order menu. I want to add some content in the column but I don't know where I can make it.
This is some example screenshoot about my woocommerce admin order view :
I want to add phone number bellow email address in Order column.
Please help me to do that.
Thank you.
I've had to do this once, i've followed this tutorial :
https://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934
add_action('manage_shop_order_posts_custom_column', 'match_order_woocommerce_custom_order_columns', 2);
function match_order_woocommerce_custom_order_columns( $column ) {
remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
global $post, $woocommerce;
$order = new WC_Order( $post->ID );
switch ($column) {
case "order_title" :
if ($order->user_id) $user_info = get_userdata($order->user_id);
if (isset($user_info) && $user_info) :
$user = '<a href="user-edit.php?user_id=' . esc_attr( $user_info->ID ) . '">';
if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.' '.$user_info->last_name;
else $user .= esc_html( $user_info->display_name );
$user .= '</a>';
else :
$user = __('Guest', 'woocommerce');
endif;
echo '<strong>'.sprintf( __('Order %s', 'woocommerce'), $order->get_order_number() ).'</strong> ' . __('made by', 'woocommerce') . ' ' . $user;
if ($order->billing_email) :
echo '<small class="meta">'.__('Email:', 'woocommerce') . ' ' . ''.esc_html( $order->billing_email ).'</small>';
endif;
if ($order->billing_phone) :
echo '<small class="meta">'.__('Tel:', 'woocommerce') . ' ' . esc_html( $order->billing_phone ) . '</small>';
endif;
break;
}
}
Please try this snippet in your active theme's functions.php

wordpress url structure

Is it possible in wordpress when I call example.com/category to have listed all available categories. If I request the url like in my example I'm getting an 404 page
I'm not sure if there is a default link to list all the categories.
But it doesn't mean you can't do it yourself. Create a new template file in your theme, name it for example category_list.php and add this code:
You might want to tweak it a bit to display it how you want.
<?php
/**
* Template Name: Category listing
* Description: list all the categories
*/
get_header(); ?>
<div class="container">
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category):
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
echo '<hr />';
endforeach;
?>
</div>
<?php get_footer(); ?>
Then go to Pages -> add new. Name the Page as "Category", so that the url will be example.com/category.
And in the template list select the template you just created. It will be named "Category listing" as you can see in the code above in the comments at the top.

wordpress recent categories widget

I have used the following code to display the recent categories <?php wp_list_categories( 'title_li=<h3>' . __('Recent Categories') . '</h3>' ); ?>. I need to exclude some categories from display. How can i do that?
You might try using 'get_categories' instead. It's a bit more complicated, but a lot more flexible. You can exclude specific cats with this function by including a comma-separated list of categories in the args. See below:
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => '1,4,9' <--- Add your cats to exclude here
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
Read more about get_categories at http://codex.wordpress.org/Function_Reference/get_categories
?>

Resources