Created
March 13, 2020 01:16
-
-
Save cocodrino/d4d891f236ed625d7dc78a146dfe5196 to your computer and use it in GitHub Desktop.
wordpress and woocommerce hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//=======LOGIN LOGOUT IN MENU | |
add_filter( 'wp_nav_menu_items', 'ia_custom_menu_item', 10, 2 ); | |
function ia_custom_menu_item ( $items, $args ) { | |
//var_dump($args); | |
if (is_user_logged_in()) { | |
$items .= '<li id="menu-item-logout" class="menu-item menu-item-type-custom menu-item-object-custom aiv_sign_button"><a href="/mi-cuenta/salir/">Salir</a></li>'; | |
} else { | |
$items .= '<li id="menu-item-login" class="menu-item menu-item-type-custom menu-item-object-custom aiv_sign_button"><a href="/mi-cuenta/">Ingreso | Registro</a></li>'; | |
} | |
return $items; | |
} | |
//====LOGIN/LOGOUT IN MENU | |
//==== REDIRECT TO CART AFTER REGISTRATION IF CONTAINS ITEM | |
function iconic_register_redirect( $redirect ) { | |
if (sizeof( WC()->cart->get_cart() ) > 0 ) { | |
return wc_get_page_permalink( 'checkout' ); | |
// return wp_redirect('/finalizar-compra/'); | |
} | |
} | |
add_filter( 'woocommerce_registration_redirect', 'iconic_register_redirect' ); | |
//==== REDIRECT TO CART AFTER REGISTRATION IF CONTAINS ITEM | |
//==== REDIRECT USER FROM CHECKOUT TO REGISTRATION WHEN THEY ARE NOT SIGN IN | |
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access'); | |
function checkout_redirect_non_logged_to_login_access() { | |
// Here the conditions (woocommerce checkout page and unlogged user) | |
if( is_checkout() && !is_user_logged_in()){ | |
// Redirecting to your custom login area | |
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); | |
// always use exit after wp_redirect() function. | |
exit; | |
} | |
} | |
//==== REDIRECT USER FROM CHECKOUT TO REGISTRATION WHEN THEY ARE NOT SIGN IN | |
//=====REDIRECT TO CART AFTER LOGIN IF THEY'VE ITEMS IN IT | |
function login_redirect( $redirect ) { | |
if (sizeof( WC()->cart->get_cart() ) > 0 ) { | |
return wc_get_page_permalink( 'checkout' ); | |
// return wp_redirect('/finalizar-compra/'); | |
} | |
} | |
add_filter( 'woocommerce_login_redirect', 'login_redirect' ); | |
//=====REDIRECT TO CART AFTER LOGIN IF THEY'VE ITEMS IN IT |
// **LOAD JQUERY**
function theme_scripts() {
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'theme_scripts');
// YITH INVOICE FIX
if ( ! function_exists('yith_ywpi_company_image_path_call_back' ) ) {
function yith_ywpi_company_image_path_call_back($company_logo_path)
{
$uploads = wp_upload_dir();
$array_company_logo_path = explode("uploads", $company_logo_path);
return $uploads['basedir'] . $array_company_logo_path[1];
}
add_filter('yith_ywpi_company_image_path', 'yith_ywpi_company_image_path_call_back', 10, 1);
}
// SHOW HTML IN CHECKOUT
/**
* @snippet agregar imagen bancos - WooCommerce
* @author Carlos Laguna
* @testedwith WooCommerce 3.5.1
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_before_checkout_billing_form', 'add_banks_info');
function add_banks_info() {
$html = <<<'EOD'
<div class="banks_box">
<h3>DATOS PARA TRANSFERENCIA:</h3>
<h4>Bank AAA</h4>
</div>
EOD;
echo $html;
}
// ADD BUTTON TO ORDERS TABLE INSIDE MY ACCOUNT
function sv_add_my_account_order_actions( $actions, $order ) {
//var_dump($order->get_status());
if($order->get_status() == "on-hold"){
$actions['help'] = array(
// adjust URL as needed
'url' => '/account/notificar-orden/?&order=' . $order->get_order_number(),
'name' => __( 'Notificar Pago', 'notificar_pago' ),
);
return $actions;
}
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'sv_add_my_account_order_actions', 10, 2 );
// REDIRECT TO PENDING ORDERS IF USER HAS PENDING ORDERS
function get_user_orders_on_hold_total() {
// Get current user
if( $user = wp_get_current_user() ){
// Get 'on-hold' customer ORDERS wc_get_orders
$on_hold_orders = wc_get_orders( array(
'limit' => -1,
'customer_id' => $user->ID,
'status' => 'on-hold',
) );
return count($on_hold_orders);
}
return 0;
}
add_action( 'parse_request', 'redirect_to_my_account_orders' );
function redirect_to_my_account_orders( $wp ) {
// All other endpoints such as change-password will redirect to
// my-account/orders
$allowed_endpoints = [ 'pedidos', 'editar-cuenta','editar-direccion', 'salir' ];
if (get_user_orders_on_hold_total()>0 &&
preg_match( '%^mi\-cuenta(?:/([^/]+)|)/?$%', $wp->request, $m ) &&
( empty( $m[1] ) || ! in_array( $m[1], $allowed_endpoints ) )
) {
wp_redirect( '/mi-cuenta/pedidos/' );
exit;
}
}
// VALIDATE DEPOSIT STATUS
// Register new status
function register_awaiting_shipment_order_status() {
register_post_status( 'wc-validando-deposito', array(
'label' => 'Validando Depósito',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Validando Depósito (%s)', 'Validando Depósito (%s)' )
) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
//==================AGREGAMOS NUESTRO NUEVA OPCION
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-validando-deposito'] = 'validando depósito';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );
// CHANGE STATE AFTER POST FORM GRAVITY FORM
add_action("gform_post_submission", "set_post_content", 10, 2);
function set_post_content($entry, $form){
$order_id = get_post($entry["order"]);
$order = new WC_Order($order_id);
$order->update_status('validando-deposito', 'formulario llenado');
}
// ADD INFO TO MY ACCOUNT
function my_custom_endpoint_content() {
$html = <<<'EOD'
<div class="banks_box">
<h3>DATOS PARA TRANSFERENCIA:</h3>
<h4>Venmo</h4>
<h5>@AIV-AIV</h5>
</div>
EOD;
$usuario = get_current_user_id();
$datos = get_user_meta($usuario);
// echo '<h2>Bienvenido ' . $datos['nickname'][0] . '</h2>';
echo '<h4>Socio: ' . $datos['num_aiv'][0] . '</h4>';
echo '<h6>Nuestros Datos Bancarios</h6>';
echo $html;
// var_dump($datos);
}
//add_action( 'woocommerce_before_account_orders', 'my_custom_endpoint_content' );
add_action( 'woocommerce_account_content', 'my_custom_endpoint_content' );
// SHOW SKU IN PRODUCT PAGE
//MOSTRAR SKU EN SIMPLE_PRODUCT Y PRODUCT_PAGE
add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
global $product;
echo '<div class="product-meta">SKU:' . $product->get_sku() . '</div>';
}
function ssd_shop_display_skus() {
global $product;
if ( $product->get_sku() ) {
echo '<div class="product-meta-sku">SKU:' . $product->get_sku() . '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'ssd_shop_display_skus', 9 );
/* add_action( 'woocommerce_product_options_general_product_data', 'ssd_shop_display_skus', 9 ); */
add_action('woocommerce_before_add_to_cart_button', 'ssd_shop_display_skus', 9 );
// ADD SKU TO PDF
/**
* Add SKU to PDF order heading
*/
add_filter ( 'pdf_template_table_headings' , 'custom_pdf_table_headers' );
function custom_pdf_table_headers() {
$headers = '<table class="shop_table orderdetails" width="100%">' .
'<thead>' .
'<tr><th colspan="7" align="left"><h2>' . esc_html__('Order Details', PDFLANGUAGE) . '</h2></th></tr>' .
'<tr>' .
'<th width="5%" valign="top" align="right">' . __( 'Qty', PDFLANGUAGE ) . '</th>' .
'<th width="10%" valign="top" align="left">' . __( 'SKU', PDFLANGUAGE ) . '</th>' .
'<th width="40%" valign="top" align="left">' . __( 'Product', PDFLANGUAGE ) . '</th>' .
'<th width="9%" valign="top" align="right">' . __( 'Price Ex', PDFLANGUAGE ) . '</th>' .
'<th width="9%" valign="top" align="right">' . __( 'Total Ex.', PDFLANGUAGE ) . '</th>' .
'<th width="7%" valign="top" align="right">' . __( 'Tax', PDFLANGUAGE ) . '</th>' .
'<th width="10%" valign="top" align="right">' . __( 'Price Inc', PDFLANGUAGE ) . '</th>' .
'<th width="10%" valign="top" align="right">' . __( 'Total Inc', PDFLANGUAGE ) . '</th>' .
'</tr>' .
'</thead>' .
'</table>';
return $headers;
}
/**
* Add SKU to order line
*/
add_filter ( 'pdf_template_line_output' , 'custom_pdf_lines', 10, 2 );
function custom_pdf_lines ( $line, $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Check WC version - changes for WC 3.0.0
$pre_wc_30 = version_compare( WC_VERSION, '3.0', '<' );
$order_currency = $pre_wc_30 ? $order->get_order_currency() : $order->get_currency();
$pdflines = '<table width="100%">';
$pdflines .= '<tbody>';
if ( sizeof( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $item['qty'] ) {
$line = '';
// $item_loop++;
$_product = $order->get_product_from_item( $item );
$item_name = $item['name'];
$item_id = $pre_wc_30 ? $item['variation_id'] : $item->get_id();
$meta_display = '';
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$item_meta = new WC_Order_Item_Meta( $item );
$meta_display = $item_meta->display( true, true );
$meta_display = $meta_display ? ( ' ( ' . $meta_display . ' )' ) : '';
} else {
foreach ( $item->get_formatted_meta_data() as $meta_key => $meta ) {
$meta_display .= '<br /><small>(' . $meta->display_key . ':' . wp_kses_post( strip_tags( $meta->display_value ) ) . ')</small>';
}
}
if ( $meta_display ) {
$meta_output = apply_filters( 'pdf_invoice_meta_output', $meta_display );
$item_name .= $meta_output;
}
$line = '<tr>' .
'<td valign="top" width="5%" align="right">' . $item['qty'] . ' x</td>' .
'<td valign="top" width="10%">' . $_product->get_sku() . '</td>' .
'<td valign="top" width="40%">' . stripslashes( $item_name ) . '</td>' .
'<td valign="top" width="9%" align="right">' . wc_price( $item['line_subtotal'] / $item['qty'], array( 'currency' => $order_currency ) ) . '</td>' .
'<td valign="top" width="9%" align="right">' . wc_price( $item['line_subtotal'], array( 'currency' => $order_currency ) ) . '</td>' .
'<td valign="top" width="7%" align="right">' . wc_price( $item['line_subtotal_tax'] / $item['qty'], array( 'currency' => $order_currency ) ). '</td>' .
'<td valign="top" width="10%" align="right">' . wc_price( ( $item['line_subtotal'] + $item['line_subtotal_tax'] ) / $item['qty'], array( 'currency' => $order_currency ) ). '</td>' .
'<td valign="top" width="10%" align="right">' . wc_price( $item['line_subtotal'] + $item['line_subtotal_tax'], array( 'currency' => $order_currency ) ). '</td>' .
'</tr>';
$pdflines .= $line;
}
}
}
$pdflines .= '</tbody>';
$pdflines .= '</table>';
return $pdflines;
}
// REG USER APPROVAL
function ws_new_user_approve_autologout(){
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if ( get_user_meta($user_id, 'pw_user_status', true ) === 'approved' ){ $approved = true; }
else{ $approved = false; }
if ( $approved ){
return $redirect_url;
}
else{ //when user not approved yet, log them out
wp_logout();
WC()->session->set( 'user_not_verified' , true );
$_SESSION['not_verified'] = 'NO VERIFICADO';
//return wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
//return add_query_arg( 'approved', 'false', get_permalink( get_option('woocommerce_myaccount_page_id') ) );
return get_permalink(woocommerce_get_page_id('myaccount')) . "?approved=false";
}
}
}
add_action('woocommerce_registration_redirect', 'ws_new_user_approve_autologout', 2);
//add_action('storefront_before_site', 'ws_new_user_approve_autologout', 2);
//add_action('woocommerce_before_main_content', 'ws_new_user_approve_autologout', 2);
//add_action('woocommerce_before_main_content', 'woocommerce_product_read', 2);
function ws_new_user_approve_registration_message(){
$not_approved_message = '<p class="not_verified_warning">Registro pendiente de Aprobación<br /> Pronto un administrador validará tu cuenta';
if( isset($_REQUEST['approved']) ){
$approved = $_REQUEST['approved'];
if ($approved == 'false') echo '<p class="registration successful">Registro completado! serás contactado una vez tu registro sea aprobado</p>';
else echo $not_approved_message;
}
else echo $not_approved_message;
}
add_action('woocommerce_before_customer_login_form', 'ws_new_user_approve_registration_message', 2);
function show_alert_new_user() {
$retrive_data = WC()->session->get( 'user_not_verified');
// echo '<h1>HELLO</h1>';
if (isset($retrive_data)) {
echo '<div class="not_verified_warning">USUARIO NO APROBADO AUN, DEBERÁ ESPERAR QUE SU CUENTA SEA VERIFICADA ANTES DE COMPRAR</div>';
//unset($_SESSION['not_verified']);
//WC()->session->__unset( 'user_not_verified' );
}
}
//add_action('after_body_open_tag', 'show_alert_new_user',10);
//add_action('wp_head', 'show_alert_new_user',10);
add_action('wp_body_open', 'show_alert_new_user');
// FIX USER REGISTER
function is_user_not_approved(){
if ( ! is_user_logged_in() ) return false;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$status = get_user_meta($user_id, 'pw_user_status', true );
return $status === 'approved' ? false : true;
}
function show_alert_user_not_approved() {
if ( ! is_user_logged_in() ) return false;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// var_dump(get_user_meta($user_id, 'pw_user_status', true ));
if (is_user_not_approved()) {
echo '<div class="not_verified_warning">USUARIO NO VERIFICADO , DEBERÁ ESPERAR QUE SU CUENTA SEA APROBADA ANTES DE REALIZAR COMPRAS EN EL SITIO WEB</div>';
unset($_SESSION['not_verified']);
WC()->session->__unset( 'user_not_verified' );
}
}
//add_action('after_body_open_tag', 'show_alert_new_user',10);
//add_action('wp_head', 'show_alert_new_user',10);
add_action('wp_body_open', 'show_alert_user_not_approved');
add_action( 'template_redirect', 'checkout_not_approved_user');
function checkout_not_approved_user() {
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && is_user_not_approved()){
wp_logout();
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
// SEND EMAIL NEW USER
//Email Notifications
//Content parsing borrowed from: woocommerce/classes/class-wc-email.php
function ws_new_user_approve_send_approved_email($user_id){
global $woocommerce;
//Instantiate mailer
$mailer = $woocommerce->mailer();
if (!$user_id) return;
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$user_pass = "As specified during registration";
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = apply_filters( 'woocommerce_email_subject_customer_new_account', sprintf( __( 'TU CUENTA EN %s HA SIDO APROBADA!', 'woocommerce'), $blogname ), $user );
$email_heading = "Usuario $user_login ha sido aprobado, ya puedes disfrutar de nuestra tienda";
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer-account-approved.php', array(
'user_login' => $user_login,
'user_pass' => $user_pass,
'blogname' => $blogname,
'email_heading' => $email_heading
));
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $user_email, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" );
}
add_action('new_user_approve_approve_user', 'ws_new_user_approve_send_approved_email', 10, 1);
function ws_new_user_approve_send_denied_email($user_id){
return;
}
add_action('new_user_approve_deny_user', 'ws_new_user_approve_send_denied_email', 10, 1);
// ADD SHIPPING PLACE TO INVOICE
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_attendee_list', 10, 2 );
function wpo_wcpdf_attendee_list ($template_type, $order) {
$place = $order->get_meta( 'shipping_site', true );
?>
<tr class="shipping_place">
<th>Lugar para Retirar:</th>
<td><?php echo $place; ?></td>
</tr>
<?php
}
// MOSTRAR SKU EN CARRITO Y CHECKOUT
add_filter( 'woocommerce_cart_item_name', 'showing_sku_in_cart_items', 99, 3 );
function showing_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
// The WC_Product object
$product = $cart_item['data'];
// Get the SKU
$sku = $product->get_sku();
// When sku doesn't exist
if(empty($sku)) return $item_name;
// Add the sku
$item_name .= '<br><small class="product-sku">' . __( "SKU: ", "woocommerce") . $sku . '</small>';
return $item_name;
}
// SHOW SKU IN EMAIL
/**
* Adds SKUs and product images to WooCommerce order emails
*/
function sww_add_sku_to_wc_emails( $args ) {
$args['show_sku'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'sww_add_sku_to_wc_emails' );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add custom register fields to admin edit area