Show a string depending on shipping country in WooCommerce checkout - wordpress

i want to show a string in the checkout in woocommerce which depends on the shipping country. I use the following code:
<?php global $woocommerce; ?>
<?php $current_cc = $woocommerce->customer->get_shipping_country() ?>
<?php if ($current_cc = DE) { ?>
<p><?php echo var_dump($current_cc);?></p>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">inkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } else { ?>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">exkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } ?>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
The var_dump always shows DE also when i choose another country. Where is the problem?
i hope somebody can help.
Thanks

PHP is a server-side language. If the page is not reloaded, the content processed by PHP will not be displayed.
You can get what you want by using a JQuery script.
The following code will only be shown on the checkout page and will display custom text after the shipping country paragraph.
// adds custom text based on shipping country value in checkout
add_action( 'wp_footer', 'add_custom_text_based_on_shipping_country' );
function add_custom_text_based_on_shipping_country() {
// only in the checkout
if ( ! is_checkout() ) {
return;
}
?>
<script type="text/javascript">
// show custom text on page load (based on shipping country value)
jQuery('<div id="custom_string"><span>'+jQuery("#shipping_country").find("option:selected").val()+'</span></div>').insertAfter('#shipping_country_field');
// replaces the text of the #custom_string element based on the value of the selected shipping country
jQuery('#shipping_country').change(function() {
var shippingCountry = jQuery(this).find('option:selected').val();
var customContent = '';
switch ( shippingCountry ) {
case 'FR':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'DE':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'IT':
customContent = '<span>'+shippingCountry+'</span>';
break;
default:
customContent = '';
break;
}
jQuery('#custom_string').html(customContent);
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.

Related

Hide coupon discount on Woocommerce checkout [duplicate]

I'm using a coupon for a calculation in the Woocommerce cart. It automatically adds a discount to the total so the right amount can be sent to payments gateways.
I'd like to hide all infos about this coupons/discount from visitors.
Problem: The only method I've found (see below) hides the coupon field, row (from totals) and messages, but also disable the coupon...
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
if ( is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
Is there a hook allowing to hide everything related to the discount without canceling the coupon?
EDIT:
Looks like it's impossible to simply remove the discount line in the order-details. So a simple solution, inspired by helgatheviking suggestion, might be to remove all the totals generated by this part
<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php endforeach; ?>
And then to echo them one by one the way I need it. I'm already able to show the order total with this
<td><?php echo number_format($order->get_total(),2,'.','')."€"; ?></td>
but now I'm trying to retrieve the order subtotal, and this code
<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."€"; ?></td>
gives me a Warning: Missing argument 1 for WC_Order::get_item_subtotal().
I'm not sure if get_item_subtotal() is the right way to get the order subtotal. And if so, what argument is missing? Or should I search around get_line_subtotal or get_subtotal_to_display?
No, there does not seem to be as there is no filter in the get_coupons() method of the cart class. If you went to the WooCommerce git repo and sent a pull request with a filter here and an explanation as to why it should be there, they might consider merging it in. I've done that a few times.
You could also, copy the checkout/review-order.php and cart/cart-totals.php templates into your theme and remove the following two blocks of code:
<?php foreach ( WC()->cart->get_coupons( 'cart' ) as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
and
<?php foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : ?>
<tr class="order-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
Keep in mind that this prevents the display of ALL coupon discounts and will end up looking like the following screenshots:
I'm not a fan of overriding the more complex WC templates... especially not the ones pertaining to the checkout process. I've had to fix many sites that stopped working when their theme template overrides became obsolete as WooCommerce develops.
Edit
I tracked down the Discount row in the order/order-details.php template. It is from the function $order->get_order_item_totals()... this returns an array of rows and can be filtered. So, this removes the row from the order received page:
function so_25714509_get_order_item_totals( $total_rows ){
unset( $total_rows['order_discount'] );
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );

Hide discount infos without canceling the coupon in woocommerce

I'm using a coupon for a calculation in the Woocommerce cart. It automatically adds a discount to the total so the right amount can be sent to payments gateways.
I'd like to hide all infos about this coupons/discount from visitors.
Problem: The only method I've found (see below) hides the coupon field, row (from totals) and messages, but also disable the coupon...
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
if ( is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
Is there a hook allowing to hide everything related to the discount without canceling the coupon?
EDIT:
Looks like it's impossible to simply remove the discount line in the order-details. So a simple solution, inspired by helgatheviking suggestion, might be to remove all the totals generated by this part
<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php endforeach; ?>
And then to echo them one by one the way I need it. I'm already able to show the order total with this
<td><?php echo number_format($order->get_total(),2,'.','')."€"; ?></td>
but now I'm trying to retrieve the order subtotal, and this code
<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."€"; ?></td>
gives me a Warning: Missing argument 1 for WC_Order::get_item_subtotal().
I'm not sure if get_item_subtotal() is the right way to get the order subtotal. And if so, what argument is missing? Or should I search around get_line_subtotal or get_subtotal_to_display?
No, there does not seem to be as there is no filter in the get_coupons() method of the cart class. If you went to the WooCommerce git repo and sent a pull request with a filter here and an explanation as to why it should be there, they might consider merging it in. I've done that a few times.
You could also, copy the checkout/review-order.php and cart/cart-totals.php templates into your theme and remove the following two blocks of code:
<?php foreach ( WC()->cart->get_coupons( 'cart' ) as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
and
<?php foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : ?>
<tr class="order-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
Keep in mind that this prevents the display of ALL coupon discounts and will end up looking like the following screenshots:
I'm not a fan of overriding the more complex WC templates... especially not the ones pertaining to the checkout process. I've had to fix many sites that stopped working when their theme template overrides became obsolete as WooCommerce develops.
Edit
I tracked down the Discount row in the order/order-details.php template. It is from the function $order->get_order_item_totals()... this returns an array of rows and can be filtered. So, this removes the row from the order received page:
function so_25714509_get_order_item_totals( $total_rows ){
unset( $total_rows['order_discount'] );
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );

Remove subtotal in woocommerce

Using woocommerce, I need a way to remove the subtotal in the checkout if there is only 1 product in the cart.
Anyone know what the best way to do this and how?
You could always put a if statement around, cart->get_total(); ?>, in you cart.php file. Not sure if it's cart php but something like that.
Just be aware. After an update this will be gone. So please create this in your child theme.
Something like:
$something = echo $woocommerce->cart->get_total();
global $woocommerce;
if ( sizeof( $woocommerce->cart->cart_contents) == 1 ) :
$somthing = '';
endif;
<strong>$somthing</strong>
You can override, 'review-order.php' template file located in, 'checkout' directory.
You can add following condition above 'cart-subtotal' class,
<?php if(WC()->cart->cart_contents_count != 1) : ?>
<tr class="cart-subtotal">
<th><?php _e( 'Subtotal', 'woocommerce' ); ?></th>
<td><?php wc_cart_totals_subtotal_html(); ?></td>
</tr>
<?php endif; ?>

Meta Box Not Showing up in The Admin Page

Using WordPress 3.7.1 and PHP 5.4.12 I am trying to add a Meta box - text field to my Custom Post Type. My custom Post Type name is "news" and this is my code:
<?php
/* Custom Meta Boxex */
add_action('add_meta_boxes', 'my_cmbox_add');
add_action('save_post', 'save_options');
function my_cmbox_add()
{
add_meta_box(
"prodInfo-meta",
"News Source ",
"news_source",
"news",
"normal",
"low"
);
}
function news_source()
{
global $post;
$custom = get_post_custom($post->ID);
$source = $custom['source'][0];
?>
<table>
<tr>
<td><?php echo '<label>News Source :</label>'; ?></td>
<td><?php echo '<input name="source" value="'. $source . '" style="width:250px;" />'; ?></td>
</tr>
</table>
<?php
}
function save_options()
{
global $post;
if (!isset($_POST['source']) || $post->post_type != 'news')
{
return $post;
}
update_post_meta($post->ID, "source", $_POST['source']);
}
I am not getting any error but as I said nothing show up in the page. Can you please let me know what I am doing wrong here?
Try to pass parameters .
function my_cmbox_add()( $post_type, $post );
You can also try and use add_meta_boxes_{post_type} for best practice.

Add multiple checkbox value to user meta box in wordpress

I found a post regarding the add extra field to the user profile page. After that i want to save the extra field to the usermeta table. Which i did by the following code.
add_action( 'personal_options_update', 'my_save_badge_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_badge_profile_fields' );
function my_save_badge_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'badge', $_POST['badge'] );
}
But now i want to store an array of value to the 'badge' name because i added multiple check box in user profile page in admin and my code for adding more check box in user profile page is written bellow.
add_action( 'show_user_profile', 'my_show_badge_profile_fields' );
add_action( 'edit_user_profile', 'my_show_badge_profile_fields' );
function my_show_badge_profile_fields( $user ) {
?> <h3>User Badges</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Badges:</label></th>
<?php
$args = array(
'post_type' => 'badge',
);
// The query itself
$sb_user_query = new WP_Query( $args );
// The loop
while ( $sb_user_query->have_posts() ) : $sb_user_query->the_post();
$badge_id = $sb_user_query->post->ID;
$badge_title = get_the_title();
$badge_image_small = get_the_post_thumbnail( $badge_id, array(16,16) );
?>
<td>
<?php echo $badge_image_small; ?> <input type="checkbox" value="<?php echo $badge_title; ?>" name="badge">
</td>
<?php
endwhile;
?>
</tr>
</table>
<?php
}
Now i want to save the data in the usermeta table in database like an array, just like the name in the check box is 'badge' and the value is an array like { 'a','b','c'} .

Resources