Woocommerce BACS add bank account number on processing email - php

I added an override function of the extended class of WC_Gateway_BACS. The function will update the status of the order from on-hold to processing. The problem is the email is missing the bank details now. Before I have the bank account number included on the email but after the customization, the email does not include it and I think it is because the order status is now processing.
Has anyone here did the same thing and come up with a solution? I included some images here of on-hold and processing emails. I like to add the account number to processing-email
class WC_Gateway_BACS_custom extends WC_Gateway_BACS {
/**
* Process the payment and return the result
*
* #access public
* #param int $order_id
* #return array
*/
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as processing (or anything you want)
$order->update_status('processing', __( 'Awaiting BACS payment', 'woocommerce' ));
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
/**
* Add content to the WC emails.
*
* #param WC_Order $order
* #param bool $sent_to_admin
* #param bool $plain_text
*/
// public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
// if ( ! $sent_to_admin && 'bacs' === $order->payment_method && ($order->has_status( 'on-hold' ) || $order->has_status( 'processing' )) ) {
// if ( $this->instructions ) {
// echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
// }
// $this->bank_details( $order->id );
// }
// }
}
on-hold email
processing email

I actually ran into the same issue today. I came up with two possible solutions:
Extending the class, as you did above:
/* override gateway for BACS */
function my_core_gateways($methods)
{
foreach ($methods as &$method){
if($method == 'WC_Gateway_BACS')
{
$method = 'WC_Gateway_BACS_custom';
}
}
return $methods;
}
/* custom gateway processor for BACS */
class WC_Gateway_BACS_custom extends WC_Gateway_BACS
{
/**
* Add content to the WC emails.
*
* #param WC_Order $order
* #param bool $sent_to_admin
* #param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
if ( $this->instructions ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
/* dirty hack to get access to bank_details */
$reflector = new ReflectionObject($this);
$method = $reflector->getMethod('bank_details');
$method->setAccessible(true);
$result = $method->invoke($this, $order->id);
}
}
/**
* Process the payment and return the result.
*
* #param int $order_id
* #return array
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Mark as on-hold (we're awaiting the payment)
$order->update_status( 'processing', __( 'Awaiting BACS payment', 'woocommerce' ) );
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
WC()->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
Or, cleaner in my opinion, by adding two actions
add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id )
{
if ( ! $order_id ) {
return;
}
$order = new WC_Order( $order_id );
if ('bacs' === $order->payment_method && ('on-hold' == $order->status || 'pending' == $order->status)) {
$order->update_status('processing');
} else {
return;
}
}
function add_order_email_instructions( $order, $sent_to_admin ) {
if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
$gw = new WC_Gateway_BACS();
$reflector = new ReflectionObject($gw);
$method = $reflector->getMethod('bank_details');
$method->setAccessible(true);
$result = $method->invoke($gw, $order->id);
}
}
The second solution has the least maintenance requirements in the long run.

Related

Add WooCommerce product to cart by SKU

Maybe someone could give me a hand resolving this challenge.
I was looking for a solution which would allow me to add products to cart by SKU's instead of WooCommerce generated ID's as I would like to use the same products across different CMS systems.
I have stumbled upon the below code, but it seems not to be compatible anymore? Any advice?
<?php
/**
* Plugin Name: WooCommerce: Add Product to Cart by SKU
* Plugin URI: http://remicorson.com
* Description: Just a demo!
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
}
/**
* get_product_id_by_product_sku()
*
* Return product ID from product SKU
*/
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) return $product_id;
return $add_to_cart;
}
}
add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'get_product_id_by_product_sku' ) );
Source: https://gist.github.com/corsonr/c02b46bd34a8471327bbf3adee6507c8
There is no need to use a custom SQL query, as you can use the wc_get_product_id_by_sku() WooCommerce function.
Using this function is much more lighter and effective but also takes into account products that are not in trash. Something your current code doesn't do.
So, this snippet will suffice:
function filter_woocommerce_add_to_cart_product_id( $product_id ) {
// Retrieves the post type of the current post or of a given post
if ( get_post_type( $product_id ) === 'product' ) {
return $product_id;
} else {
$sku = $product_id;
}
// Get product ID by SKU
$product_id = wc_get_product_id_by_sku( $sku );
return $product_id;
}
add_filter( 'woocommerce_add_to_cart_product_id', 'filter_woocommerce_add_to_cart_product_id', 10, 1 );
Note: SKU is assumed to be a numerical value
As you can read in the note, the above answer will only work for numerical values, to make this work for all SKU values, ​​you can use as custom query string = /?add-to-cart-sku=THE-SKU
So you get:
function action_wp_loaded( $url = false ) {
// Make sure WC is installed and add-to-cart-sku query arg exists
if ( ! class_exists( 'WC_Form_Handler' ) || ! isset( $_REQUEST['add-to-cart-sku'] ) || ! is_string( $_REQUEST['add-to-cart-sku'] ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
wc_nocache_headers();
$product_id = wc_get_product_id_by_sku( wp_unslash( $_REQUEST['add-to-cart-sku'] ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
return;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
if ( 'variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler ) {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id );
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) {
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Custom handler.
} else {
$was_added_to_cart = woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id );
}
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && 0 === wc_notice_count( 'error' ) ) {
$url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
if ( $url ) {
wp_safe_redirect( $url );
exit;
} elseif ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'action_wp_loaded', 15 );
/**
* Invoke class private method
*
* #since 0.1.0
*
* #param string $class_name
* #param string $methodName
*
* #return mixed
*/
function woo_hack_invoke_private_method( $class_name, $methodName ) {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()' );
}
$args = func_get_args();
unset( $args[0], $args[1] );
$reflection = new ReflectionClass( $class_name );
$method = $reflection->getMethod( $methodName );
$method->setAccessible( true );
$args = array_merge( array( $reflection ), $args );
return call_user_func_array( array( $method, 'invoke' ), $args );
}
Based on: Allow adding multiple products to the cart via the add-to-cart query string & /includes/class-wc-form-handler.php
please try this one, i have include the filter call inside __constructor:
<?php
/**
* Plugin Name: WooCommerce: Add Product to Cart by SKU
* Plugin URI: http://remicorson.com
* Description: Just a demo!
* Version: 1.0
* Author: Remi Corson
* Author URI: http://remicorson.com/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* WC Product Add to Cart by SKU class
*/
class WC_Add_to_Cart_by_SKU {
/**
* Constructor
*/
public function __construct() {
define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
add_filter( 'woocommerce_add_to_cart_product_id', array(
$this,
'get_product_id_by_product_sku'
) );
}
/**
* get_product_id_by_product_sku()
*
* Return product ID from product SKU
*/
public function get_product_id_by_product_sku( $add_to_cart ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
if ( $product_id ) {
return $product_id;
}
return $add_to_cart;
}
}
new WC_Add_to_Cart_by_SKU();

Add Woocommerce Subscription switch to cart programmatically

I'm looking for a way to programmatically add a switch between two Woocommerce subscription variations to the cart.
We're building a headless WP site, so I don't want to do it with a link, as described in this question
I've tried the following simple way, but it won't allow it that way, since the user is already subscribed:
WC()->cart->add_to_cart( 1907, 1, 1908 );
I want to mimic what is happening when the user goes to the upgrade/downgrade page, selects a new variation and presses Switch subscription. Usually that means that the user is sent to checkout with the switch in cart, I just want it to be added to the cart, since we have our own checkout.
The main issue is that Woocommerce thinks that a subscription product is unpurchaseable when it is added to the cart any other way than through the subscription switch form. The form sends a couple of GET-parameters which sends WC through some extra validation, and then allows the item to be added to the cart if everything else is valid.
My solution to this is to mimic this validation with some functions of my own, mostly stolen from the subscription plugin.
/**
* Must be set to true before programmatically adding subscription switch to cart.
*
* #var bool
*/
$kbhl_adding_switch_to_cart = false;
/**
* Array filled with data about current subscription.
* Should only be retrieved by kbhl_get_current_subscription_data().
*
* #var array
*/
$kbhl_global_current_subscription_data = array();
/**
* Cache whether a given product is purchasable or not to save running lots of queries for the same product in the same request
*
* $is_purchasable_cache taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #var array
*/
$kbhl_purchasable_cache = array();
/**
* $user_subscriptions_to_product taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #var array
*/
$kbhl_user_subscriptions_to_product = array();
/**
* If a product is being marked as not purchasable because it is limited and the customer has a subscription,
* but the current request is to switch the subscription, then mark it as purchasable.
*
* Function is_purchasable_switch() taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #param bool $is_purchasable Current purchasable status.
* #param obj $product Product being checked.
*
* #return bool New purchasable status.
*/
function kbhl_is_purchasable_switch( $is_purchasable, $product ) {
global $kbhl_purchasable_cache;
$kbhl_current_subscription_data = kbhl_get_current_subscription_data();
// Only process this filter if running custom add switch function.
if ( ! empty( $kbhl_current_subscription_data['id'] ) ) {
return $is_purchasable;
}
$product_key = wcs_get_canonical_product_id( $product );
// Set an empty cache if one isn't set yet.
if ( ! isset( $kbhl_purchasable_cache[ $product_key ] ) ) {
$kbhl_purchasable_cache[ $product_key ] = array();
}
// Exit early if we've already determined this product's purchasability via switching.
if ( isset( $kbhl_purchasable_cache[ $product_key ]['switch'] ) ) {
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
// If the product is already purchasble, we don't need to determine it's purchasibility via switching/auto-switching.
if ( true === $is_purchasable || ! is_user_logged_in() || ! wcs_is_product_switchable_type( $product ) || ! WC_Subscriptions_Product::is_subscription( $product->get_id() ) ) {
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
$user_id = get_current_user_id();
$product_limitation = wcs_get_product_limitation( $product );
if ( 'no' == $product_limitation || ! wcs_user_has_subscription( $user_id, $product->get_id(), wcs_get_product_limitation( $product ) ) ) {
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
// Adding to cart.
if ( array_key_exists( $kbhl_current_subscription_data['id'], kbhl_get_user_subscriptions_to_product( $product, $user_id, $product_limitation ) ) ) {
$is_purchasable = true;
}
$kbhl_purchasable_cache[ $product_key ]['switch'] = $is_purchasable;
return $kbhl_purchasable_cache[ $product_key ]['switch'];
}
add_filter( 'woocommerce_subscription_is_purchasable', 'kbhl_is_purchasable_switch', 13, 2 );
/**
* Gets a list of the customer subscriptions to a product with a particular limited status.
*
* Function get_user_subscriptions_to_product() taken from plugins\woocommerce-subscriptions\includes\class-wcs-limiter.php
*
* #param WC_Product|int $product The product object or product ID.
* #param int $user_id The user's ID.
* #param string $limit_status The limit status.
*
* #return WC_Subscription[] An array of a customer's subscriptions with a specific status and product.
*/
function kbhl_get_user_subscriptions_to_product( $product, $user_id, $limit_status ) {
global $user_subscriptions_to_product;
$product_id = is_object( $product ) ? $product->get_id() : $product;
$cache_key = "{$product_id}_{$user_id}_{$limit_status}";
if ( ! isset( $user_subscriptions_to_product[ $cache_key ] ) ) {
// Getting all the customers subscriptions and removing ones without the product is more performant than querying for subscriptions with the product.
$subscriptions = wcs_get_subscriptions(
array(
'customer_id' => $user_id,
'status' => $limit_status,
)
);
foreach ( $subscriptions as $subscription_id => $subscription ) {
if ( ! $subscription->has_product( $product_id ) ) {
unset( $subscriptions[ $subscription_id ] );
}
}
$user_subscriptions_to_product[ $cache_key ] = $subscriptions;
}
return $user_subscriptions_to_product[ $cache_key ];
}
/**
* When a subscription switch is added to the cart, store a record of pertinent meta about the switch.
*
* #since 1.4
*/
/**
* When a subscription switch is added to the cart, store a record of pertinent meta about the switch.
*
* Function set_switch_details_in_cart() taken from plugins\woocommerce-subscriptions\includes\class-wc-subscriptions-switcher.php
*
* #param array $cart_item_data Current cart item data.
* #param int $product_id ID of current product.
* #param int $variation_id ID of current product variation.
*
* #return array Updated cart item data.
*/
function kbhl_set_switch_details_in_cart( $cart_item_data, $product_id, $variation_id ) {
try {
$kbhl_current_subscription_data = kbhl_get_current_subscription_data();
if ( empty( $kbhl_current_subscription_data['id'] ) || empty( $kbhl_current_subscription_data['item'] ) ) {
return $cart_item_data;
}
$subscription = wcs_get_subscription( $kbhl_current_subscription_data['id'] );
// Requesting a switch for someone elses subscription.
if ( ! current_user_can( 'switch_shop_subscription', $subscription->get_id() ) ) {
wc_add_notice( __( 'You can not switch this subscription. It appears you do not own the subscription.', 'woocommerce-subscriptions' ), 'error' );
WC()->cart->empty_cart( true );
return array();
}
$item = wcs_get_order_item( absint( $kbhl_current_subscription_data['item'] ), $subscription );
// Else it's a valid switch.
$product = wc_get_product( $item['product_id'] );
$parent_products = WC_Subscriptions_Product::get_parent_ids( $product );
$child_products = array();
if ( ! empty( $parent_products ) ) {
foreach ( $parent_products as $parent_id ) {
$child_products = array_unique( array_merge( $child_products, wc_get_product( $parent_id )->get_children() ) );
}
}
if ( $product_id != $item['product_id'] && ! in_array( $item['product_id'], $child_products ) ) {
return $cart_item_data;
}
$next_payment_timestamp = $subscription->get_time( 'next_payment' );
// If there are no more payments due on the subscription, because we're in the last billing period, we need to use the subscription's expiration date, not next payment date.
if ( false == $next_payment_timestamp ) {
$next_payment_timestamp = $subscription->get_time( 'end' );
}
$cart_item_data['subscription_switch'] = array(
'subscription_id' => $subscription->get_id(),
'item_id' => absint( $kbhl_current_subscription_data['item'] ),
'next_payment_timestamp' => $next_payment_timestamp,
'upgraded_or_downgraded' => '',
);
return $cart_item_data;
} catch ( Exception $e ) {
wc_add_notice( __( 'There was an error locating the switch details.', 'woocommerce-subscriptions' ), 'error' );
WC()->cart->empty_cart( true );
return array();
}
}
add_filter( 'woocommerce_add_cart_item_data', 'kbhl_set_switch_details_in_cart', 11, 3 );
/**
* Gets subscription data for current user.
*
* #return array Subscription data, also stored to global variable $kbhl_global_current_subscription_data
*/
function kbhl_get_current_subscription_data() {
global $kbhl_adding_switch_to_cart, $kbhl_global_current_subscription_data;
if ( ! $kbhl_adding_switch_to_cart ) {
return array();
}
if ( ! empty( $kbhl_global_current_subscription_data ) ) {
return $kbhl_global_current_subscription_data;
}
$subscription_data = array();
$subs = wcs_get_users_subscriptions();
if ( ! empty( $subs ) ) {
foreach ( $subs as $sub ) {
$subscription_data['id'] = $sub->get_id();
foreach ( $sub->get_items() as $item_id => $item ) {
$subscription_data['item'] = $item_id;
break; // There should only be 1 order item.
}
break; // There should only be 1 subscription.
}
}
$kbhl_global_current_subscription_data = $subscription_data;
return $kbhl_global_current_subscription_data;
}
With this added you can add the switch to the cart as follows:
global $kbhl_adding_switch_to_cart; // If run inside a function.
WC()->cart->empty_cart( true );
$kbhl_adding_switch_to_cart = true;
WC()->cart->add_to_cart( 1907, 1, 1927 );
$kbhl_adding_switch_to_cart = false; // Reset after to get back to default validation.

Change subscription status to active based on order id WooCommerce

First of all, Thanks to a guys who helped me write a script which changes order status to completed(script below).
Unfortunately it doesn't trigger subscription to go active. When I manually change order status in WooCommerce it does. So my idea was to activate a subscription based on given order_id.
How to change subscription status from "Pending" to "Active" based on order_id in WooCommerce?
Here is a code which changes the order status when I use 100% coupon and it works: in functions.php
<?php
add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
if ($order->get_total() == 0) {
$order->update_status('processing');
$order->update_status('completed');
}
}
I have found smth like this to activate those subscriptions, but I cannot implement this, maybe somebody can help me solve this?
Here is the code I have found on github
/**
* Activates all the subscriptions created by a given order.
*
* #param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as activated.
* #since 1.0
*/
public static function activate_subscriptions_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
$subscription->update_status( 'active' );
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to activate subscription status for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'subscriptions_activated_for_order', $order );
}
}
I tried this but it doesn't work:
add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
if ($order->get_total() == 0) {
$order->update_status('processing');
$order->update_status('completed');
}
}
do_action('woocommerce_checkout_order_processed', 'subscriptions_activated_for_order', $order_id );
function activate_subscriptions_for_order( $order_id ) {
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
$subscription->update_status( 'active' );
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to activate subscription status for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order_id ) ? $order->get_order_number() : $order_id, $e->getMessage() ) );
}
}
}
}
Best Regards
There are errors and mistakes in your code. You could try to merge everything together like:
add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
if ($order->get_total() == 0) {
// $order->update_status('processing'); // Unneeded as you complete the order
$order->update_status('completed');
}
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
activate_subscriptions_for_order( $order );
}
}
Code goes in functions.php file of the active child theme (or active theme). It could work.
Or you could keep:
add_action('woocommerce_checkout_order_processed', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order($order_id)
{
if (!$order_id) {
return;
}
$order = wc_get_order($order_id);
if ($order->get_total() == 0) {
// $order->update_status('processing'); // Unneeded as you complete the order
$order->update_status('completed');
}
}
and use the hook woocommerce_order_status_completed triggered when order status is "completed" as follows to activate the subscription related to the order:
add_action( 'woocommerce_order_status_completed', 'order_complete_activate_subscription', 10, 2 );
function order_complete_activate_subscription( $order_id, $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
activate_subscriptions_for_order( $order );
}
}
Code goes in functions.php file of the active child theme (or active theme). It could work.

Don't understand WooCommerce Paypal Standard Gateway

So I'm building a custom payment gateway for WooCommerce which has been... challenging, mostly because I'm not a PHP developer so I have been learning as I go. I've started by dissecting the PayPal Standard Gateway which ships with WooCommerce. That gateway has an included class for handling IPN callbacks and I'm having trouble understanding why their code is set up the following way:
/**
* Constructor.
*
* #param bool $sandbox Use sandbox or not.
* #param string $receiver_email Email to receive IPN from.
*/
public function __construct( $sandbox = false, $receiver_email = '' ) {
add_action( 'woocommerce_api_wc_gateway_paypal', array( $this, 'check_response' ) );
add_action( 'valid-paypal-standard-ipn-request', array( $this, 'valid_response' ) );
$this->receiver_email = $receiver_email;
$this->sandbox = $sandbox;
}
/**
* Check for PayPal IPN Response.
*/
public function check_response() {
if ( ! empty( $_POST ) && $this->validate_ipn() ) { // WPCS: CSRF ok.
$posted = wp_unslash( $_POST ); // WPCS: CSRF ok, input var ok.
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'valid-paypal-standard-ipn-request', $posted );
exit;
}
wp_die( 'PayPal IPN Request Failure', 'PayPal IPN', array( 'response' => 500 ) );
}
/**
* There was a valid response.
*
* #param array $posted Post data after wp_unslash.
*/
public function valid_response( $posted ) {
$order = ! empty( $posted['custom'] ) ? $this->get_paypal_order( $posted['custom'] ) : false;
if ( $order ) {
// Lowercase returned variables.
$posted['payment_status'] = strtolower( $posted['payment_status'] );
WC_Gateway_Paypal::log( 'Found order #' . $order->get_id() );
WC_Gateway_Paypal::log( 'Payment status: ' . $posted['payment_status'] );
if ( method_exists( $this, 'payment_status_' . $posted['payment_status'] ) ) {
call_user_func( array( $this, 'payment_status_' . $posted['payment_status'] ), $order, $posted );
}
}
}
The first add_action registers a hook with a WooCommerce action and I totally get that - it's a way to extend functionality in an external package. However, the second add_action( 'valid-paypal-standard-ipn-request'...) seems to simply call valid_response() - which is a local function - inside the next function check_response(). Why couldn't they just call valid_response() directly within check_response()?

Get Paypal Payment Details on WooCommerce

How can I get the payment details from Paypal like PaymentID, PaymentFirstName/LastName, and other details?
The code PayPal Standard integration uses valid-paypal-standard-ipn-request action to process the valid IPN response. You can use the same action to hook into the IPN and get/store any information you want.
To save additional information:
// Hook before the code has processed the order
add_action( 'valid-paypal-standard-ipn-request', 'prefix_process_valid_ipn_response', 9 );
function prefix_process_valid_ipn_response( $posted ) {
if ( ! empty( $posted['custom'] ) && ( $order = prefix_get_paypal_order( $posted['custom'] ) ) ) {
// Lowercase returned variables.
$posted['payment_status'] = strtolower( $posted['payment_status'] );
// Any status can be checked here
if ( 'completed' == $posted['payment_status'] ) {
// Save additional information you want
}
}
}
/**
* From the Abstract "WC_Gateway_Paypal_Response" class
*
* #param $raw_custom
*
* #return bool|WC_Order|WC_Refund
*/
function prefix_get_paypal_order( $raw_custom ) {
// We have the data in the correct format, so get the order.
if ( ( $custom = json_decode( $raw_custom ) ) && is_object( $custom ) ) {
$order_id = $custom->order_id;
$order_key = $custom->order_key;
// Nothing was found.
} else {
return false;
}
if ( ! $order = wc_get_order( $order_id ) ) {
// We have an invalid $order_id, probably because invoice_prefix has changed.
$order_id = wc_get_order_id_by_order_key( $order_key );
$order = wc_get_order( $order_id );
}
if ( ! $order || $order->get_order_key() !== $order_key ) {
return false;
}
return $order;
}
You can find the PayPal variables here: https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNIntro/#id08CKFJ00JYK
The WC core also saves to the order a lot of the IPN data already. All data is saved to the order meta, so you can access it using get_post_meta or $order->get_meta('meta_key').
List by meta_key:
'Payer PayPal address' - The payer address
'Payer first name' - Payer first name
'Payer last name' - Payer last name
'Payment type' - Payment Type
'_paypal_status' - PayPal payment status

Categories