WooCommerce My Account Dashboard: Direct link to edit billing address - php

As I sell virtual products I don’t need the WooCommerce shipping address fields. I've already removed them from the checkout page and also want to remove them from the “My Account” page.
In the dashboard under the „Edit address“ tab the user should be directed directly to the page where he can edit the billing address. The page that shows the billing / shipping address and where the user has to click on "Edit address" is not longer needed.
Is there a way to achieve this? Any help is appreciated.

You can unset the Address endpoint and create a new one for Billing. For example, in your functions.php add the following code.
//1
function add_d_endpoint() {
add_rewrite_endpoint( 'billing', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_d_endpoint' );
//2
function d_query_vars( $vars ) {
$vars[] = 'billing';
return $vars;
}
add_filter( 'query_vars', 'd_query_vars', 0 );
//3
function add_d_link_my_account( $items ) {
$items[ 'billing' ] = 'Billing Address'; //The title of new endpoint
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_d_link_my_account' );
//4
function d_content() {
echo WC_Shortcode_My_Account::edit_address( 'billing' ); //The content of new endpoint
}
//5
add_action( 'woocommerce_account_billing_endpoint', 'd_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format
//6
/** Remove Address from My Account Menu **/
add_filter( 'woocommerce_account_menu_items', 'dsx_remove_my_account_dashboard' );
function dsx_remove_my_account_dashboard( $menu_links ) {
unset( $menu_links[ 'edit-address' ] ); //remove the address from endpoint
return $menu_links;
}
Then go to your Dashboard > Settings > Permalinks, then click the button Save, that should do it.
Alternatively, you can override the my-address.php located on woocommerce/templates/myaccount, just use WC_Shortcode_My_Account::edit_address( 'billing' );.
Or you can redirect into Billing when user(s) is trying to access the Edit-Address endpoint, try using the below code in your functions.php.
function redirect_to_billing( $wp ) {
$current_url = home_url(add_query_arg(array(),$wp->request));
$billing = home_url('/account/edit-address/billing');
/** If user is accessing edit-address endpoint and it's not the billing address**/
if(is_wc_endpoint_url('edit-address') && $current_url !== $billing){
wp_redirect($billing);
exit();
}
}
add_action( 'parse_request', 'redirect_to_billing' , 10);

Related

I can't purchase my custom product type in WordPress

I'm developing a plugin for custom product type. Here's my class that is being registered on plugins_loaded hook:
class WC_Product_Subscription extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'subscription';
$this->purchasable = true;
$this->downloadable = false;
$this->virtual = true;
$this->sold_individually = true;
$this->manage_stock = false;
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
public function is_purchasable() {
return true;
}
}
The problem is that I cannot see "Add to Cart" button on the product page which means my product cannot be purchased. I tried adding
public function add_to_cart_url() {
return apply_filters( 'woocommerce_product_add_to_cart_url', get_permalink( $this->get_id() ), $this );
}
public function add_to_cart_text() {
$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
}
to the class but without success. I'm stuck.
It appears there are some missing steps to make your custom product type work.
Try the steps below:
#1. Make sure that your plugin is active.
#2. Make sure the product is in stock and has a price set. WooCommerce checks both of these conditions before displaying the Add to Cart button.
#3. Check if the custom product type is registered correctly. Use the following code to check:
add_action( 'init', 'check_registered_product_types' );
function check_registered_product_types() {
$product_types = wc_get_product_types();
var_dump( $product_types );
}
#4. Make sure that the WooCommerce product type is supported. Use the following code to check:
add_filter( 'product_type_selector', 'custom_product_type_selector' );
function custom_product_type_selector( $product_types ) {
var_dump( $product_types );
return $product_types;
}
#5. Make sure that the product class is correctly loaded. Use the following code to check:
add_action( 'plugins_loaded', 'check_product_class' );
function check_product_class() {
$product_class = 'WC_Product_Subscription';
var_dump( class_exists( $product_class ) );
}
#6. Ensure you have a product template for your custom product type in your theme's WooCommerce folder (e.g. single-product-subscription.php).
#7. If everything else seems to be working, you might have to override the WooCommerce templates to display the Add to Cart button.
Edit:
You can create a template in your plugin directory by using the following code:
add_filter( 'woocommerce_locate_template', 'wc_subscription_template', 10, 3 );
function wc_subscription_template( $template, $template_name, $template_path ) {
if ( 'single-product-subscription.php' === $template_name ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/single-product-subscription.php';
}
return $template;
}
This will tell WooCommerce to use your custom template in the templates folder within your plugin directory. Make sure you put the code in a file that is included in your plugin, so it will run when the plugin is activated.

WooCommerce redirects to My Account not to Checkout after Sign Up

I'm using this functions.php code so when my clients want to check out they will redirect to Sign up or log in before they checkout, I tried this method below but after all it does not redirect to Checkout page but to My Account, any idea on how to fix the code so I will be redirected to Checkout and not to My Account.
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = 68; // your checkout page id
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
Try using this filter: woocommerce_registration_redirect
function woocommerce_register_redirect( $redirect ) {
return wc_get_page_permalink( 'cart' );
}
add_filter( 'woocommerce_registration_redirect', 'woocommerce_register_redirect' );

Custom my account endpoint in Woocommerce just for a specific user role

Following the woocommerce documentation, I added an endpoit to my-account page in woocommerce.
I want to make this endpoint visible only to a specific user role, lets say shop_manager.
Is there a way to redirect to a 404 page users who try to access directly that endpoint?
Thanks in advance.
Assuming that you have already created a custom endpoint to my account section (see this related answer), you can redirect all non allowed user roles to a specific page using template_redirect hook in this simple way:
add_action( 'template_redirect', 'custom_endpoint_redirection' );
function custom_endpoint_redirection() {
$allowed_user_role = 'administrator';
$custom_endpoint = 'my-custom-endpoint';
if ( is_wc_endpoint_url($custom_endpoint) && ! current_user_can($allowed_user_role) ) {
$redirection_url = home_url( '/404/' );
wp_redirect( $redirection_url );
exit;
}
}
You need to specify your custom end point, your allowed user role and the url redirection.
Code goes in functions.php file of your active child theme (or active theme). It could works.
Related:
WooCommerce - Assign endpoints to multiple custom templates in my-account page
WooCommerce: Assigning an endpoint to a custom template in my account pages
WooCommerce: Adding custom template to customer account pages
Custom my account new menu item for a specific user role in Woocommerce
Just add the follows code snippet in your active theme's functions.php and this is only for administrator user role, you can change it as per you -
function add_custom_my_account_endpoint() {
add_rewrite_endpoint( 'shop_manager', EP_PAGES );
}
add_action( 'init', 'add_custom_my_account_endpoint' );
function add_custom_wc_menu_items( $items ) {
$user = wp_get_current_user();
if( $user && in_array( 'administrator', $user->roles ) ){
$items[ 'shop_manager' ] = __( 'Shop Manager', 'text-domain' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_custom_wc_menu_items' );
function add_shop_manager_endpoint_content(){
$user = wp_get_current_user();
if( $user && !in_array( 'administrator', $user->roles ) ) return;
echo "Your content goes here";
}
add_action( 'woocommerce_account_shop_manager_endpoint', 'add_shop_manager_endpoint_content' );
After this just flush_rewrite_rules from Backend Settings > Permalinks. Thats it.

Remove cancel button from WooCommerce My account Orders conditionally

I want to make sure the cancel button is not visible in my-account> my-order when 'Payment Method Title' is 'Npay'.
The 'Npay' is an external payment gateway and does not work with the commerce. Therefore, payment cancellation must be done externally only.
add_filter('woocommerce_my_account_my_orders_actions', 'remove_my_cancel_button', 10, 2);
function remove_my_cancel_button($actions, $order){
if ( $payment_method->has_title( 'Npay' ) ) {
unset($actions['cancel']);
return $actions;
}
}
To remove the cancel button from My account Orders, we use the following:
add_filter('woocommerce_my_account_my_orders_actions', 'remove_myaccount_orders_cancel_button', 10, 2);
function remove_myaccount_orders_cancel_button( $actions, $order ){
unset($actions['cancel']);
return $actions;
}
But to remove the cancel button from My account Orders based on the payment title, you will use the WC_Order method get_payment_method_title() like:
add_filter('woocommerce_my_account_my_orders_actions', 'remove_myaccount_orders_cancel_button', 10, 2);
function remove_myaccount_orders_cancel_button( $actions, $order ){
if ( $order->get_payment_method_title() === 'Npay' ) {
unset($actions['cancel']);
}
return $actions;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
The main variable argument $actions need to be returned at the end outside the IF statement

WooCommerce Membership Cancel in WordPress

A customer can usually cancel a membership in his dashboard. How can I restrict this (hide the cancel button) for a specific membership?
I found this code for general hide the cancel button, work's so far:
function sv_edit_my_memberships_actions( $actions )
{
unset( $actions['cancel'] );
return $actions}
add_filter( 'wc_memberships_my_account_my_memberships_actions', 'sv_edit_my_memberships_actions' );
}
maybe with this function?
wc_memberships_is_user_active_member( $current_user_id, 'membership-name' )
You are almost there. You already have done some syntactical mistakes which #mujeeb specified. Try following code
function sv_edit_my_memberships_actions( $actions )
{
$user_id = get_current_user_id();
if(wc_memberships_is_user_active_member( $user_id, 'silver' )){// Instead of silver you can give your membership type
unset( $actions['cancel'] );
}
return $actions;
}
add_filter( 'wc_memberships_my_account_my_memberships_actions', 'sv_edit_my_memberships_actions' );
function sv_edit_my_memberships_actions( $actions ) {
unset( $actions['cancel'] );
return $actions;
}
add_filter( 'wc_memberships_members_area_my_memberships_actions', 'sv_edit_my_memberships_actions' );

Categories