Skip to main content

WC custom guest checkout create account

https://usersinsights.com/woocommerce-auto-register-users/
https://usersinsights.com/woocommerce-custom-fields-registration/

https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/
https://wordpress.stackexchange.com/questions/85485/hooks-are-not-executing

https://stackoverflow.com/questions/27112461/woocommerce-send-custom-email-on-custom-order-status-change
https://cloudredux.com/adding-sending-custom-woocommerce-email/

https://www.wpdesk.net/blog/woocommerce-checkout-hooks/

The problem with the logic flow Manual create customer then add order/sub to this new account is that:
1. Customer in Stripe side (in case use Stripe) does not synced to WC. So next time user login and buy subs product => Stripe create new acc.
2.

add_action( 'woocommerce_before_checkout_process', 'wpse_woocommerce_checkout_process' );
function wpse_woocommerce_checkout_process() {
// Do something...
}
//woocommerce_checkout_update_user_meta
//woocommerce_checkout_update_customer

Class wc-checkout.php
This class contain some magnificent logic handle, from reload cart session, discount and handle multi_site account... As many other Core Application code, in this time is WC.
/**
* Create a new customer account if needed.
*
* @throws Exception When not able to create customer.
* @param array $data Posted data.
*/
protected function process_customer( $data ) {
        }

Yith-gateway-advanced.php
/**
* Get customer of Stripe account or create a new one if not exists
*
* @param $order WC_Order
* @return \Stripe\Customer
* @since 1.0.0
*/
public function get_customer( $order ) { }


//add_action( 'woocommerce_order_amount_line_total', 'action_woocommerce_checkout_after_order_review', 10, 5 );
function action_woocommerce_checkout_after_order_review($total, $sub, $item, $inc_tax, $round ){
define('WP_DEBUG_LOG', true);
ini_set( 'error_log', WP_CONTENT_DIR . '/test2.log' );
error_log(print_r( $total, true ));
error_log(print_r( $item, true ));
}
function my_log($data) {
$log = WP_CONTENT_DIR . "/log2.txt";
$ln = "\r\n";
file_put_contents($log, json_encode($data).$ln, FILE_APPEND); //json_encode.$ln
}

add_action( 'woocommerce_after_order_notes', 'test13', 10, 5 );
function test13($data) {
my_log($data);
my_log('in side test13');
}
add_action( 'ywcsb_after_calculate_totals', 'test14', 10, 5 );
function test14($data) {
my_log($data);
my_log('in side test14');
}

add_action( 'woocommerce_checkout_update_order_meta', 'test15', 10, 5 );
function test15($data) {
global $messages;
my_log($messages);
my_log($data);
my_log('in side test15');
}

// alter the subscriptions error
function my_woocommerce_add_error( $error ) {
my_log($error);
if( $error ) {
// order_id
add_action( 'woocommerce_order_amount_line_total', function(){return 0 ;});
}
define('WP_DEBUG_LOG', true);
ini_set( 'error_log', WP_CONTENT_DIR . '/test2.log' );
error_log(print_r( $error, true ));
return $error;
}
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );

function my_yith_wcstripe_metadata($data, $data2) {
define('WP_DEBUG_LOG', true);
ini_set( 'error_log', WP_CONTENT_DIR . '/test2.log' );
error_log(print_r( $data, true ));
error_log(print_r( $data2, true ));
}
add_filter('yith_wcstripe_metadata', 'my_yith_wcstripe_metadata', 10, 2);

OOP here:
<?php
/**
 * Main class
 *
 * @author Your Inspiration Themes
 * @package YITH WooCommerce Stripe
 * @version 1.0.0
 */

if ( ! defined( 'YITH_WCSTRIPE' ) ) {
exit;
} // Exit if accessed directly

if( ! class_exists( 'YITH_WCStripe_Customer' ) ){
/**
* WooCommerce Stripe main class
*
* @since 1.0.0
*/
class YITH_WCStripe_Customer {
/**
* Single instance of the class
*
* @var \YITH_WCStripe_Customer
* @since 1.0.0
*/
protected static $instance;

/**
* Returns single instance of the class
*
* @return \YITH_WCStripe_Customer
* @since 1.0.0
*/
public static function get_instance(){
if( is_null( self::$instance ) ){
self::$instance = new self;
}

return self::$instance;
}

/**
* Constructor.
*
* @return string
* @since 1.0.0
*/
public function get_env() {
if ( empty( $this->env ) ) {
// Load form_field settings
$settings = get_option( 'woocommerce_' . YITH_WCStripe::$gateway_id . '_settings', null );
$this->env = isset( $settings[ 'enabled_test_mode' ] ) && $settings[ 'enabled_test_mode' ] == 'yes' ? 'test' : 'live';
}

return $this->env;
}

/**
* Get customer info for a user into DB
*
* @since 1.0.0
*/
public function get_usermeta_info( $user_id ) {
return get_user_meta( $user_id, $this->get_customer_usermeta_key(), true );
}

/**
* Update customer info for a user into DB
*
* @since 1.0.0
*/
public function update_usermeta_info( $user_id, $params = array() ) {
return update_user_meta( $user_id, $this->get_customer_usermeta_key(), $params );
}

/**
* Delete customer info for a user into DB
*
* @since 1.0.0
*/
public function delete_usermeta_info( $user_id ) {
return delete_user_meta( $user_id, $this->get_customer_usermeta_key() );
}

/**
* Update customer info for a user into DB
*
* @since 1.0.0
*/
public function want_save_cards( $user_id ) {
$info = $this->get_usermeta_info( $user_id );
return (bool)( 'yes' == $info['save_cards'] ? true : false );
}

/**
* Return the name of user meta for the customer info
*
* @return string
* @since 1.0.0
*/
protected function get_customer_usermeta_key() {
return '_' . $this->get_env() . '_stripe_customer_id';
}
}
}

/**
 * Unique access to instance of YITH_WCStripe_Customer class
 *
 * @return \YITH_WCStripe_Customer
 * @since 1.0.0
 */
function YITH_WCStripe_Customer(){
return YITH_WCStripe_Customer::get_instance();
}

Buy full vs subs
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
After order processed event done => Did WC update Stripe customer data or it has been done before this event ?
In case buy subs, after this event, Yith take control and create subs... So it will call get_customer(Stripe) again (or first call?).

Class wc-emails.php is one of the many good example ab OOP and Refactoring.
Try apply Refactoring to this class to reveal magnificent OOP.


function my_log( $data ) {
$log = WP_CONTENT_DIR . '/log2.txt';
$ln  = "\r\n";
file_put_contents( $log, json_encode( $data ) . $ln, FILE_APPEND );
}

Comments

Popular posts from this blog

Rand mm 10

https://stackoverflow.com/questions/2447791/define-vs-const Oh const vs define, many time I got unexpected interview question. As this one, I do not know much or try to study this. My work flow, and I believe of many programmer is that search topic only when we have task or job to tackle. We ignore many 'basic', 'fundamental' documents, RTFM is boring. So I think it is a trade off between the two way of study language. And I think there are a bridge or balanced way to extract both advantage of two method. There are some huge issue with programmer like me that prevent we master some technique that take only little time if doing properly. For example, some Red Hat certificate program, lesson, course that I have learned during Collage gave our exceptional useful when it cover almost all topic while working with Linux. I remember it called something like RHEL (RedHat Enterprise Linux) Certificate... I think there are many tons of documents, guide n books about Linux bu

Martin Fowler - Software Architecture - Making Architecture matter

  https://martinfowler.com/architecture/ One can appreciate the point of this presentation when one's sense of code smell is trained, functional and utilized. Those controlling the budget as well as developer leads should understand the design stamina hypothesis, so that the appropriate focus and priority is given to internal quality - otherwise pay a high price soon. Andrew Farrell 8 months ago I love that he was able to give an important lesson on the “How?” of software architecture at the very end: delegate decisions to those with the time to focus on them. Very nice and straight-forward talk about the value of software architecture For me, architecture is the distribution of complexity in a system. And also, how subsystems communicate with each other. A battle between craftmanship and the economics and economics always win... https://hackernoon.com/applying-clean-architecture-on-web-application-with-modular-pattern-7b11f1b89011 1. Independent of Frameworks 2. Testable 3. Indepe