customise endpoint urls in woocommerce - php

I'm trying to create a tabbed version of "My Account" in woocommerce.
I have built my pages in php using bootstrap css and included the wordpress header and footer so the page loads correctly.
The tabs display as expected by I am having an issue with the endpoint urls in woocommerce:
- 'edit-address'
- 'view-order'
These endpoints are generated dynamically and appended to the url: www.site-name.com/my-account/edit-address
Here's the function call (in file .plugins/woocommerce/myaccount/form-edit-address.php):
<a href="<?php echo wc_get_endpoint_url( 'edit-address', $name ); ?>
I've included 'my-addresses' in my tabbed page which dispays ok. However, the link to edit the shipping and billing addresses is generated by the above call to the endpoints. When the link is clicked the page fails to load and returns a 404 error.
My custom php page is basically www.site-name.com/account/edit-address
The problem is:
The 'edit-address' page content is not loading and I get a 404 error
I'm guessing the issue is caused because my pages are external php pages and not stored with wp database?
Is there a way I can customise the endpoint url so it appends correctly and loads the page?
Link to the page on my site: www.thecookerytutor dot co dot uk/account
Origional woocommerce my-account page: www.thecookerytutor dot co dot uk/my-account
(You will need to create a login to view both pages)
It's had me stumped for days!
Thanks in advance.
Code for my addresses tab on my custom php page as promised...
echo "<div id = 'edit-addresses' class='tab-pane fade'>";
echo "<div id = 'content' class = 'page col-full'>";
echo "<section id = 'main' class = 'col-left'>";
echo "<BR>";
include "my-address.php";
echo "</section>";
echo "</div>";
echo "</div>";`
As you can see I'm including the wc page that loads the addresses (I've copied to the local folder)
Here's the content of edit-address.php (I've copied to my local folder and added wp header). The page loads the header but the get address function fails at line 50.
<?php
define('WP_USE_THEMES', false);
require('../wp-blog-header.php');
add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );
function wp_title_so_18381106( $title, $sep, $seplocation )
{
return 'Your Account | ';
}
get_header();
?>
<?php
/**
* Edit address form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $current_user;
$page_title = ( $load_address === 'billing' ) ? __( 'Billing Address', 'woocommerce' ) : __( 'Shipping Address', 'woocommerce' );
get_currentuserinfo();
?>
<?php wc_print_notices(); ?>
<?php //if ( ! $load_address ) : ?>
<?php //wc_get_template( 'myaccount/my-address.php' ); ?>
<?php //else : ?>
<form method="post">
<h3><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title ); ?></h3>
<?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?>
/fails to load here/
<?php foreach ( $address as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : $field['value'] ); ?>
<?php endforeach; ?>
<?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?>
<p>
<input type="submit" class="button" name="save_address" value="<?php _e( 'Save Address', 'woocommerce' ); ?>" />
<?php wp_nonce_field( 'woocommerce-edit_address' ); ?>
<input type="hidden" name="action" value="edit_address" />
</p>
</form>
<?php //endif; ?>

WooCommerce is a plugin for WordPress, you can't use it without WordPress... for example /my-account/edit-address/, that my-account is actually the page you are rendering and edit-address is just a variable. edit-address was added as an endpoint to my-account. you need to do the same for your account.
You should get an idea of what endpoints are.
Other thoughts about this. If you really want to use account and not my-account, look into your pages for "My Account" and change it's permalink. You can also create another page that can use account in it's permalink. Then use that page in your WooCommerce > Settings > Account tab > My Account Page.
If you have some custom php codes, you can just copy the template from your woocommerce plugin to your theme folder and edit the file you need. Read Template Structure + Overriding Templates via a Theme.
And if I have not addressed your problem, comment down below.

Related

how to redirect non logged users to login page and redirect to another page with saved data?

im developing a page and using this free simple Favorite Post plugin to add favorites to users. currently its working for logged in users only. i need to show it to non logged users and when click on its button redirect to login or register page and after logged in redirect to same page or custom page.
this is for show favorite button code on front end : or short code :[favorite-post-btn]
this is for short for code display favorited posts :[favorite-post]
if anyone can help me big it would me grateful..! thank you i will put the button code here :
/**
* Favorite post link button
*
* #param int $post_id
* #return void
*/
function link_button( $post_id ) {
if ( !is_user_logged_in() ) {
return;
}
$status = $this->get_post_status( $post_id, get_current_user_id() );
?>
<a class="wpf-favorite-link" href="#" data-id="<?php echo $post_id; ?>">
<?php if ( $status ) { ?>
<span class="wpf-favorite"> </span> <?php _e( 'Remove from favorite', 'wfp' ); ?>
<?php } else { ?>
<span class="wpf-not-favorite"> </span> <?php _e( 'Add to favorite', 'wfp' ); ?>
<?php } ?>
</a>
<?php
}
you can add some if statmen like
if (isset($_SESSION['username'])
or something like that, which if the condition is true makes you click the button, otherwise it redirects you to the login page
You are looking for https://developer.wordpress.org/reference/functions/wp_redirect/
Checking: Redirect after Login on WordPress
This has been answered here: https://stackoverflow.com/a/65380999/5214689

display Woocommerce subscription actions to any of custom page or dashboard

I am using WooCommerce subscriptions and am trying to create a custom flow in the dhasboard for the user.
Currently user logs in > dashboard shows, I have custom code to show if subscription status is "active" or "on-hold". If it is on hold, the user currently has to click on view subscription, then click on a listed subscription and then click renew in the actions section.
I want to move this actions button to renew an "on-hold" subscription out of the subscription-details.php file and into the dashboard.php file to reduce these steps.
Here is the snippet i've found that I think relates to the renew action button:
<?php do_action( 'woocommerce_subscription_before_actions', $subscription ); ?>
<?php $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() ); ?>
<?php if ( ! empty( $actions ) ) : ?>
<tr>
<td><?php esc_html_e( 'Actions', 'woocommerce-subscriptions' ); ?></td>
<td>
<?php foreach ( $actions as $key => $action ) : ?>
<?php echo esc_html( $action['name'] ); ?>
<?php endforeach; ?>
</td>
</tr>
<?php endif; ?>
<?php do_action( 'woocommerce_subscription_after_actions', $subscription ); ?>
I tried to bring this into the dashboard.php file, however I get an error saying the site is experiencing technical difficulties.
Any ideas on how I could bring this renewal action button into the dashboard.php file instead?
Thanks in advance for any help!!!
If you want to show any of action in your custom page or dashboard then use below code and set action according to your requirements
function addCancelButton($subscription) {
$actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() );
if(!empty($actions)){
foreach ( $actions as $key => $action ){
if(strtolower($action['name']) == "cancel"){
$cancelLink = esc_url( $action['url'] );
echo "<a href='$cancelLink' class='button cancel'>".$action['name']."</a>";
}
}
}
}
add_action( 'woocommerce_my_subscriptions_actions', 'addCancelButton', 10 );
If you want to edit My account page then i suggest to use this hook in your child theme
woocommerce_account_dashboard
Here is code for same
add_action( 'woocommerce_account_dashboard','add_account_content_kiki' );
function add_account_content_kiki() {
if( has_active_subscription() ){ // Current user has an active subscription
echo '<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info"><a class="woocommerce-Button button" href="www.google.com">Test Now</a>Test link - shop now</div>';
// Example of displaying something
echo 'You have active subscription';
}
}
you have to add this function also in function file
function has_active_subscription( $user_id='' ) {
// When a $user_id is not specified, get the current user Id
if( '' == $user_id && is_user_logged_in() )
$user_id = get_current_user_id();
// User not logged in we return false
if( $user_id == 0 )
return false;
return wcs_user_has_subscription( $user_id, '', 'active' );
}
Reference of hook - https://docs.woocommerce.com/wc-apidocs/hook-docs.html
All hook you can use from here for woo commerce my account page - https://businessbloomer.com/woocommerce-visual-hook-guide-account-pages/

How to add new Custom Title in Woocommerce My Account sidebar?

Hi i want to add new custom title and links into My Account page into my WooCommerce site.. Searched all documentation, and Stackoverflow topics, but not found sollution for my request.
This is title for what im asking for
Text Muj Ucet is My Account in english. :)
i want to add new title like is shown in image bellow:
This is WooCommerce Template Code for that part:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<p><?php
/* translators: 1: user display name 2: logout url */
printf(
__( 'Hello %1$s (not %1$s? Log out)', 'woocommerce'
),
'<strong>' . esc_html( $current_user->display_name ) . '</strong>',
esc_url( wc_logout_url( wc_get_page_permalink( 'myaccount' ) ) )
);
?></p>
<p>Na nástěnce svého uživatelského účtu si můžete stáhnout své zakoupené
produkty a faktury, upravit své osobní informace, změnit heslo nebo
fakturační adresu.</p>
<?php
/**
* My Account dashboard.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_dashboard' );
/**
* Deprecated woocommerce_before_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_before_my_account' );
/**
* Deprecated woocommerce_after_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_after_my_account' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already
sent" issues. */
I want to add new one title bellow that sidebar. How to register a new title ?
Thanks
Here is the solution.
add_filter('woocommerce_account_menu_items', 'display_account_new_link');
function display_account_new_link( $items ) {
$items['new_link'] = __( 'New Link', 'text-domain' );
return $items;
}
add_action( 'woocommerce_account_new_link_endpoint', 'new_account_link_content' );
function new_account_link_content() {
//include your display template here
echo "Here goes you content";
}
After pasting this code in your plugin or in your theme function.php file this code will make a new link in the my account navigation sidebar, along with the template you want to assign this link. Here new_link is the slug for this navigation link. If you want to give some different slug you must rename new_link written everywhere in the given code. As soon as you click on this New Link it will redirect you to the Page Not Found Page. It can be solved by adding this code.
add_action( 'init', 'register_new_link_endpoint');
function register_new_link_endpoint() {
add_rewrite_endpoint( 'new_link', EP_PAGES );
}
After pasting this code you must save your permalink once, by going to WordPress Dashboard->Settings->Permalinks and hit the save changes button.

removing action in wordpress where action has an array with $this

I'm trying to simply replace a message that is added by a plugin called 'sensei' from woothemes.
For that my plan was to remove the action that places the message, and then add an action where i copied the normal function and edited to what i wanted it to be.
However the message was never removed and instead my new message is placed on the page next to the default message.
i used this code in functions.php:
add_action( 'sensei_loop_course_inside_before', 'remove_old_course_message', 15 );
add_action( 'sensei_loop_course_inside_before', 'active_no_course_message_output2', 11 );
function remove_old_course_message(){
global $Sensei_Shortcode_User_Courses;
remove_action( 'sensei_loop_course_inside_before', array( 'Sensei_Shortcode_User_Courses', 'sensei_loop_course_inside_before' ), 10);
remove_action( 'sensei_loop_course_inside_before', array( $Sensei_Shortcode_User_Courses, 'sensei_loop_course_inside_before' ), 10);
}
function active_no_course_message_output2(){
?>
<li class="user-active">
<div class="sensei-message info">
This is the message to be shown:
<?php _e( 'You have no active courses.', 'woothemes-sensei' ); ?>
<a href="www.google.com">
<?php _e( 'Start a Course!', 'woothemes-sensei' ); ?>
</a>
</div>
</li>
<?php
}
(all that changed is that i changed the button in the message to link to google instead of another page, this is so the code is a bit more understandable.)
In the plugin file there is a class named 'sensei shortcode-user-courses' and it contains the code:
if( empty( $active_ids ) ){
add_action( 'sensei_loop_course_inside_before', array( $this, 'active_no_course_message_output' ) );
}

Link title to external link instead of permalink

I'm trying to get the same effect as the plugin Pages Link To where the title of the post is linked to an external link. The reason I don't want to use the plugin is the link is generated dynamically when the post is saved, but I'm unable to update the the permalink of the post with the external link.
Below is my code in functions.php:
function savepost( $post_id ) {
if( $_POST['post_type'] == 'books' ){
$genre = strip_tags(get_field('genre'));
$author = strip_tags(get_field('author'));
$extlink = "http://www.".$genre."/".$author.".com";
update_post_meta( $post_id, 'extlink', $extlink);
$url = strip_tags(get_field('extlink',$post));
update_post_meta( $post_id, 'post_link', $url);
}
}
add_action( 'save_post', 'savepost' );
i m trying another method in which i assigned a template to the post so that when the post loads it redirects to the link but it doesn't redirect
my code
<?php
ob_start();
?>
<?php
/**
* Template Name: post Redirection Template
*/
get_header();
$redirecturl = strip_tags(get_field('extlink',$post));
wp_redirect($redirect_url);
get_sidebar();
get_footer();
?>
<?php
ob_end_flush();
?>
What you should do, is insert the external link as a custom field in the post editor, then display the custom field value in place of the_permalink(). You could use a plugin such as Advanced Custom Fields to grab the URL from the custom field.
EDIT 1: More clarification using the Advanced Custom Fields plugin as an example. The field name for this example is url.
You should use this wherever you want the custom permalink to appear throughout your site, such as in your archive.php, category.php, etc. Replace the code that looks something like this:
<?php the_title(); ?>
with this:
<?php
$value = get_field( "url" );
if( $value ) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
EDIT 2: Clarifying additional information.
You can add a function to the header.php of your theme that checks if the url is set, then redirects to the external link that way, if your user goes directly to the permalink, it will still redirect them. In fact, you could use this code without using the above code to display the external link.
<?php
$value = get_field( "url" );
if( $value ) {
header('Location: '.$value);
die();
} else {} ?>
Warning: make sure to use this code before any HTML (or text) has been passed to the browser, or it will not work correctly.

Categories