|
<?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 ); |
functions.php