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

AWS Elasticache Memcached connection

https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/accessing-elasticache.html#access-from-outside-aws http://hourlyapps.blogspot.com/2010/06/examples-of-memcached-commands.html Access memcached https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/GettingStarted.AuthorizeAccess.html Zip include hidden file https://stackoverflow.com/questions/12493206/zip-including-hidden-files phpmemcachedadmin ~ phpMyAdmin or phpPgAdmin ... telnet mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com 11211 stats items stats cachedump 27 100 https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/VPCs.EC.html https://lzone.de/cheat-sheet/memcached VPC ID Security Group ID (sg-...) Cluster: The identifier for the cluster memcached1 Creation Time: The time (UTC) when the cluster was created January 9, 2019 at 11:47:16 AM UTC+7 Configuration Endpoint: The configuration endpoint of the cluster memcached1.ahgofe.cfg.usw1.cache.amazonaws.com:11211 St...

Notes Windows 10 Virtualbox config, PHP Storm Japanese, custom PHP, Apache build, Postgresql

 cmd => Ctrl + Shift + Enter mklink "C:\Users\HauNT\Videos\host3" "C:\Windows\System32\drivers\etc\hosts" https://www.quora.com/How-to-create-a-router-in-php https://serverfault.com/questions/225155/virtualbox-how-to-set-up-networking-so-both-host-and-guest-can-access-internet 1 NAT + 1 host only config https://unix.stackexchange.com/questions/115464/how-to-properly-set-up-2-network-interfaces-in-centos-running-in-virtualbox DEVICE=eth0 TYPE=Ethernet #BOOTPROTO=dhcp BOOTPROTO=none #IPADDR=10.9.11.246 #PREFIX=24 #GATEWAY=10.9.11.1 #IPV4_FAILURE_FATAL=yes #HWADDR=08:00:27:CC:AC:AC ONBOOT=yes NAME="System eth0" [root@localhost www]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 # Advanced Micro Devices, Inc. [AMD] 79c970 [PCnet32 LANCE] DEVICE=eth1 IPADDR=192.168.56.28 <= no eff => auto like DHCP #GATEWAY=192.168.56.1 #BOOTPROTO=dhcp BOOTPROTO=static <= no eff ONBOOT=yes HWADDR=08:00:27:b4:20:10 [root@localhost www]# ...

Rocket.Chat DB schema

_raix_push_notifications avatars.chunks avatars.files instances meteor_accounts_loginServiceConfiguration meteor_oauth_pendingCredentials meteor_oauth_pendingRequestTokens migrations rocketchat__trash rocketchat_cron_history rocketchat_custom_emoji rocketchat_custom_sounds rocketchat_import rocketchat_integration_history rocketchat_integrations rocketchat_livechat_custom_field rocketchat_livechat_department rocketchat_livechat_department_agents rocketchat_livechat_external_message rocketchat_livechat_inquiry rocketchat_livechat_office_hour rocketchat_livechat_page_visited rocketchat_livechat_trigger rocketchat_message rocketchat_oauth_apps rocketchat_oembed_cache rocketchat_permissions rocketchat_raw_imports rocketchat_reports rocketchat_roles rocketchat_room rocketchat_settings rocketchat_smarsh_history rocketchat_statistics rocketchat_subscription rocketchat_uploads system.indexes users usersSessions https://rocket.chat/docs/developer-guides/sc...