Display used coupons description on WooCommerce admin orders list - php

i want to display used coupons on WooCommerce admin orders list with the coupon description. So i'm using Display used coupons on WooCommerce admin orders list answer code and the used coupon code is shown, but can't include the used coupon description.
How is that possible?

You can coupon object by WC_Coupon class and use $coupon->get_description() to get coupon description. try the below code.
function woo_customer_order_coupon_column_for_orders( $columns ) {
$new_columns = array();
foreach ( $columns as $column_key => $column_label ) {
if ( 'order_total' === $column_key ) {
$new_columns['order_coupons'] = __('coupons', 'woocommerce');
}
$new_columns[$column_key] = $column_label;
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'woo_customer_order_coupon_column_for_orders' );
function woo_display_customer_order_coupon_in_column_for_orders( $column ) {
global $the_order;
if( $column == 'order_coupons' ) {
if( $coupons = $the_order->get_used_coupons() ) {
foreach( $coupons as $coupon_code ){
$coupon = new WC_Coupon($coupon_code);
if( $coupon ){
echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
echo "<p class='coupon-description'>".$coupon->get_description()."</p>";
}
}
} else {
echo '<small><em>'. __('No Coupon') . '</em></small>';
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'woo_display_customer_order_coupon_in_column_for_orders' );
Tested and works

Related

Add column total order weight to WooCommerce My account Orders

I want to add weight metadata to an order in the Frontend: My Account - Orders. I tried some things but it is not working.
I want to add is $order->get_weight(); as meta data to the order but I am getting an error.
I am already half way using this code to add a new column and show product description and quantity:
add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
$new_columns = [];
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
if ( 'order-status' === $key ) {
$new_columns['order-items'] = __( 'Descripción', 'woocommerce' );
}
}
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
$details = array();
foreach( $order->get_items() as $item )
$details[] = $item->get_name() . ' × ' . $item->get_quantity();
echo count( $details ) > 0 ? implode( '<br>', $details ) : '–';
}
Hope someone can help me get in the right direction.
This snippet inserts a new, custom column in the table of orders shown in My Account > Orders populated with the total weight of the order so the customer is aware how heavy their order was.
Specifically, this snippet has two blocks of code. The first block inserts the column. In this example, we have inserted this column between the Order Total and Order Actions column. This can be changed by changing the column key in the code. Your custom will appear after the column key you define.
The second block of code is where the magic happens. It first loops through each item in the order and gets it weight and times this by the quantity of this product. It then adds this weight of each product to a variable we have called $total_weight. The total weight is then output to the new column followed by the weight unit you have defined in your store settings under WordPress Dashboard > WooCommerce > Settings > Products > General > Measurements > Weight Unit.
/**
* Snippet Name: WooCommerce Show Order Weight Column In My Account Order View Table
* Snippet Author: ecommercehints.com
*/
// First, create the new table column between Total and Actions columns
add_filter( 'woocommerce_my_account_my_orders_columns', 'ecommercehints_weight_column_my_account_orders_table', 10, 1 );
function ecommercehints_weight_column_my_account_orders_table( $columns ) {
$weight_column = [];
foreach ( $columns as $key => $name ) {
$weight_column[ $key ] = $name;
if ( 'order-total' === $key ) { // Insert new column after Total column
$weight_column['order-items'] = __( 'Order Weight', 'woocommerce' );
}
}
return $weight_column;
}
// Second, insert the data from the order into the new column
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'ecommercehints_get_order_weight', 10, 1 );
function ecommercehints_get_order_weight( $order ) {
$weight_unit = get_option('woocommerce_weight_unit');
$total_weight = 0;
foreach( $order->get_items() as $item_id => $item ){
$quantity = $item->get_quantity();
$product = $item->get_product();
$product_weight = $product->get_weight();
$total_weight += floatval( $product_weight * $quantity );
}
echo $total_weight . $weight_unit;
}
Just came across this, have you tried it? https://gist.github.com/kloon/5299119?permalink_comment_id=1415838
Here is a copy of the code in case the link eventually dies:
<?php
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
function woo_order_weight_column( $columns ) {
$columns['total_weight'] = __( 'Weight', 'woocommerce' );
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
global $post, $woocommerce, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
$the_order = new WC_Order( $post->ID );
if ( $column == 'total_weight' ) {
$weight = 0;
if ( sizeof( $the_order->get_items() ) > 0 ) {
foreach( $the_order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $item->get_product();
if ( ! $_product->is_virtual() ) {
$weight += $_product->get_weight() * $item['qty'];
}
}
}
}
if ( $weight > 0 ) {
print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
} else {
print 'N/A';
}
}
}
?>

Add "Pay for order" Button in WooCommerce My account Orders Custom Column

Based on the answer code by LoicTheAztec for Add a pay order button on WooCommerce My account view order for pending orders, I have added additional code in an attempt in getting this button to show up in a custom column directly on the "View orders" endpoint.
The column is there, but the button is not. I have tried switching $order with $order_id in the function as well, without success.
This is the code I am working with:
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_payment_column_to_myaccount', 10, 1 );
function add_payment_column_to_myaccount( $columns ) {
$new_columns = [];
foreach ($columns as $key => $name){
$new_columns[$key] = $name;
if ('order-actions' === $key){
$new_columns['pay-order'] = __('Payment', 'woocommerce');
}
}
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_order-items', 'add_pay_for_order_to_payment_column_myaccount', 10, 1);
function add_pay_for_order_to_payment_column_myaccount( $order ) {
$order = wc_get_order( $order_id );
if ( $order->get_status() == "pending" || $order->get_status() == "on-hold" ) {
printf('<a class="woocommerce-button button pay" href="%s/order-pay/%s/?pay_for_order=true&key=%s">%s</a>',
wc_get_checkout_url(), $order_id, $order->get_order_key(), __("Pay for this order", "woocommerce")
);
}
}
You missed pay-order in the composite hook from your 2nd function:
woocommerce_my_account_my_orders_column_{$column_key}
where $column_key need to be replaced by pay-order (but not order-items).
There are also some other mistakes. Try the following:
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_payment_column_to_myaccount' );
function add_payment_column_to_myaccount( $columns ) {
$new_columns = [];
foreach ($columns as $key => $name){
$new_columns[$key] = $name;
if ('order-actions' === $key){
$new_columns['pay-order'] = __('Payment', 'woocommerce');
}
}
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_pay-order', 'add_pay_for_order_to_payment_column_myaccount' );
function add_pay_for_order_to_payment_column_myaccount( $order ) {
if( in_array( $order->get_status(), array( 'pending', 'on-hold' ) ) ) {
printf( '<a class="woocommerce-button button pay" href="%s/order-pay/%s/?pay_for_order=true&key=%s">%s</a>',
wc_get_checkout_url(), $order->get_id(), $order->get_order_key(), __("Pay for this order", "woocommerce")
);
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Displaying data in WooCommerce admin orders list custom columns

I'm adding the following custom columns and they both appear:
function add_order_new_column_header( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
$new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
}
}
return $new_columns;
Then I'm adding the following to generate data:
add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20);
add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content' );
function add_wc_order_admin_list_column_content( $column ) {
global $post;
if ( 'order_shipping' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_shipping_method();
if ( 'order_payment' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_payment_method_title();
}
}
}
order_shipping is showing correct shipping method, but order_payment is empty.
I'm not using multiple languages on the site. WooCommerce 4.1.1, PHP 7.3.19
view from WooCommerce orders admin panel:
I've tried both get_payment_method and get_payment_method_list. I have orders with BACS mostly and a few Cash on Pickup which is disabled for now. What am I missing here?
There are some mistakes and missing things in your code, try the following instead:
add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
function add_custom_columns_to_admin_orders( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
$new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
}
}
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders' );
function custom_columns_content_in_admin_orders( $column ) {
global $post, $the_order;
if ( 'order_shipping' === $column ) {
echo $the_order->get_shipping_method();
}
if ( 'order_payment' === $column ) {
echo $the_order->get_payment_method_title();
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

How to change the variable product price in cart also delete remove particular product form cart in Woocommerce

I my site i added one variable product in cart.At that time another variable product also added into that cart that product is gift product.Now i want to change the gift variable product price into 0 its working only at the time of condition meets the products that offers gifts in cart. Also i want to remove both product form same by clicking products that offers gifts.Below my code is not working for me.
add_action( 'woocommerce_before_calculate_totals', 'change_custom_price' );
function change_custom_price( $cart_object ) {
$custom_price = 0; // This will be your custome price
$gift_variation_id = 2046;
foreach ( $cart_object->cart_contents as $value ) {
if ( $value['variation_id'] == $gift_variation_id ) {
$value['data']->price = $custom_price;
}
}
}
Gift item can be added as per this solution - Buy one get one in woocommerce with out coupon code.
You can add the following to your theme's 'functions.php' to remove the gift item added automatically by another product.
function remove_gift_product($cart_item_key) {
global $woocommerce;
$cat_in_cart = false;
$coupon_in_cart = false;
$autocoupon = array( 123411 ); // variation ids of products that offers gifts
$freecoupon = array( 2046 ); // variation ids of products that are gift coupons
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $autocoupon ) ) {
$cat_in_cart = true;
}
}
if ( !$cat_in_cart ) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ( in_array( $cart_item['variation_id'], $freecoupon )) {
$woocommerce->cart->remove_cart_item($cart_item_key);
}
}
}
}
add_action( 'woocommerce_cart_item_removed', 'remove_gift_product' );
Add this if you want reduced price for your gift item.
function add_discount_price( $cart_object ) {
global $woocommerce;
$cat_in_cart = false;
$autocoupon = array( 123411 ); // variation ids of products that offers gifts
$freecoupon = array( 2046 ); // variation ids of products that are gift coupons
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $autocoupon ) ) {
$cat_in_cart = true;
}
}
if ( $cat_in_cart ) {
$custom_price = 0; // This will be your custome price
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ( in_array( $cart_item['variation_id'], $freecoupon )) {
$cart_item['data']->set_price($custom_price);
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_discount_price' );

Disabling Amazon Payments Advanced method from WooCommerce checkout page

I'm trying to disable a couple of payment gateways based on a user's role. The function & hook I found works on the Paypal method but not Amazon Payments Advanced. Here's my code:
function wk_disable_gateways( $available_gateways ) {
global $woocommerce;
$wholesale_cust = check_user_role( array( 'wholesale', 'orig-wholesale' ) );
if ( isset( $available_gateways['paypal'] ) && $wholesale_cust ) {
unset( $available_gateways['paypal'] );
}
if ( isset( $available_gateways['amazon_payments_advanced'] ) && $wholesale_cust ) {
unset( $available_gateways['amazon_payments_advanced'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wk_disable_gateways' );
The "Pay with Amazon" code is still running on the checkout page. Any ideas?
This is not the best solution, but I have not been able to find a more viable answer.
First off I did what you did and disabled all gateways expect the one I wanted the user to use. In my case I only want someone to check out with the nmigateway.
This goes inside of your theme functions.php
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
foreach ( $gateways as $gateway_key => $gateway ) {
if ( $gateway_key !== 'nmipay' ) {
unset( $gateways[ $gateway_key ] );
}
}
}
}
return $gateways;
}
Next here is the part that makes this not the best solution editing the plugins source code.
Change the following two functions inside of the plugins/woocommerce-gateway-amazon-payments-advanced/amazon-payments-advanced.php
/**
* Checkout Button
*
* Triggered from the 'woocommerce_proceed_to_checkout' action.
*/
function checkout_button() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
?><div id="pay_with_amazon"></div><?php
}
}
/**
* Checkout Message
*/
function checkout_message() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
if ( empty( $this->reference_id ) ) {
echo '<div class="woocommerce-info info"><div id="pay_with_amazon"></div> ' . apply_filters( 'woocommerce_amazon_pa_checkout_message', __( 'Have an Amazon account?', 'woocommerce-gateway-amazon-payments-advanced' ) ) . '</div>';
}
}
}
Keep in mind that when the plugin is updated all of your changes will be lost.

Categories