Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 11, 2024 13:32
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save damiencarbery/fd02b001b1943651573d294dc9374a48 to your computer and use it in GitHub Desktop.
Refund Requests for WooCommerce Customers - using Contact Form 7 - As a response to a comment on Beka Rice's "Add Refund Requests for WooCommerce Customers" post on sellwp.com I decided to implement the same concept with Contact Form 7. https://www.damiencarbery.com/2016/11/refund-requests-for-woocommerce-customers-using-contact-form-7/
<?php
// To the CF7 form add:
// [dynamic_hidden dynamichidden-cart "cart_contents"]
// In the CF7 Mail add:
// [dynamichidden-cart]
// The "cart_contents" uses this code
add_shortcode( 'cart_contents', 'dcwd_cart_contents' );
function dcwd_cart_contents($atts, $content, $code) {
$cart_contents = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_contents[] = '* ' . $cart_item['data']->get_name();
}
if ( empty( $cart_contents ) ) {
return 'Cart is empty.';
}
else {
return implode( "\n", $cart_contents );
}
}
<label>Name (required)
[dynamic_text* dynamictext-customer-name "customer_name"]</label>
<label>Order Number (required)
[dynamic_text* dynamictext-order-number "order_number"]</label>
<label>Which Product(s)? (required)
[dynamic_text* dynamictext-products "refund_products"]</label>
<label>Why are you requesting a refund?
[textarea refund-reason] </label>
[submit "Send"]
From: [dynamictext-customer-name]
Order number: [dynamictext-order-number]
Products: [dynamictext-products]
Refund reason: [refund-reason]
<?php
/*
Plugin Name: Refund Requests for WooCommerce Customers – using Contact Form 7
Plugin URI: https://www.damiencarbery.com/2016/11/refund-requests-for-woocommerce-customers-using-contact-form-7/
Description: As a response to a comment on Beka Rice's "Add Refund Requests for WooCommerce Customers" post on sellwp.com I decided to implement the same concept with Contact Form 7.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.3
WC tested up to: 8.0.2
*/
// These shortcodes are used in the Contact Form 7 and Dynamic Text Extension.
add_shortcode('customer_name', 'rr_order_details');
add_shortcode('order_number', 'rr_order_details');
add_shortcode('refund_products', 'rr_order_details');
function rr_order_details($atts, $content, $code) {
switch ($code) {
// Use order ID to retrieve customer first and last name.
case 'customer_name':
if (array_key_exists('order', $_GET)) {
$order_id = $_GET['order'];
$order = new WC_Order( $order_id );
if (is_object($order)) {
$name = $order->billing_first_name.' '.$order->billing_last_name;
return $name;
}
}
return '-No customer name-'; // To confirm if plugin working. Leave blank otherwise.
break;
// Retrieve the order ID from the url.
case 'order_number':
if (array_key_exists('order', $_GET)) {
$order_id = $_GET['order'];
return $order_id;
}
return '-No order number-'; // To confirm if plugin working. Leave blank otherwise.
break;
case 'refund_products':
// Retrieve the list of products from the url.
if (array_key_exists('products', $_GET)) {
$products = $_GET['products'];
return $products;
}
return '-No products-'; // To confirm if plugin working. Leave blank otherwise.
break;
default:
break;
}
}
// Add "Request Refund" button to View Order page.
add_action( 'woocommerce_order_details_after_order_table', 'dcwd_add_refund_request_button_in_view_order', 12 );
function dcwd_add_refund_request_button_in_view_order( $order ) {
if ( $order->is_paid() ) {
?>
<p><a href="<?php echo esc_url( sww_get_order_refund_request_url( $order ) ); ?>" class="button">Request Refund</a></p>
<?php
}
}
// The following code is taken directly from: https://jilt.com/blog/add-refund-requests-woocommerce-customers/
function sww_add_account_refund_order_action( $actions, $order ) {
if ( $order->is_paid() ) {
$actions['refund_request'] = array(
'url' => sww_get_order_refund_request_url( $order ),
'name' => __( 'Request Refund', 'my-textdomain' ),
);
}
return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'sww_add_account_refund_order_action', 10, 2 );
function sww_get_order_refund_request_url( $order ) {
// enter the ID for your page that contains the refund request form
$page_id = 7994;
// now we'll build the right refund URL from it
$refund_url = trailingslashit( get_page_link( $page_id ) ) . '?order=' . $order->get_order_number();
$products = array();
foreach ( $order->get_items() as $item_id => $item ) {
if ( $product = wc_get_product( $item['product_id'] ) ) {
$products[] = $product->get_title();
}
}
if ( ! empty( $products ) ) {
$refund_url .= '&products=' . urlencode( implode( ', ', $products ) );
}
return $refund_url;
}
// Optionally add a 'Request Refund' link in emails.
function sww_add_refund_request_email_link( $order ) {
if ( $order->is_paid() ) {
echo '<br /><a class="link" href="' . esc_url( sww_get_order_refund_request_url( $order ) ) . '">Request Refund</a>';
}
}
add_action( 'woocommerce_email_after_order_table', 'sww_add_refund_request_email_link', 15, 1 );
@hurabg
Copy link

hurabg commented Apr 23, 2019

Hi Damien i tried inserting your but unable to see the result could you please guide further

@damiencarbery
Copy link
Author

Where did you insert the code?

@helemaaldebom
Copy link

Where did you insert the code?

functions.php

@damiencarbery
Copy link
Author

Where did you insert the code?

functions.php

FYI: You can also upload the file to wp-content/plugins - https://www.damiencarbery.com/2018/10/how-to-use-my-code-snippets/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment