WooCommerce Membership Cancel in WordPress - php

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' );

Related

Remove columns from WooCommerce admin coupon list

I am trying to remove some default columns (amount & products) from the WooCommerce admin coupon list.
For that I make use of the following code:
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
case 'shop_coupon':
unset(
$columns['amount'],
$columns['products']
);
break;
}
return $columns;
}
But it doesn't work and I'm not getting any errors. I think the code I'm using is just not being applied correctly.
You can use the manage_edit-{post type or taxonomy}_columns filter hook
So you get:
function filter_manage_edit_shop_coupon_columns( $columns ) {
// Remove
unset( $columns['products'] );
unset( $columns['amount'] );
return $columns;
}
add_filter( 'manage_edit-shop_coupon_columns', 'filter_manage_edit_shop_coupon_columns', 10, 1 );

WooCommerce My Account Dashboard: Direct link to edit billing address

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);

Admin notice for custom fields in users dashboard

In Wordpress, I added some fields in the users dashboard. I made them required but I'd like to display a message if they are empty.
Here is an example (just one field) of what I did :
function my_admin_notice() { ?>
<div class="error">
<p><?php _e( 'Error!', 'user_street' ); ?></p>
</div>
<?php
}
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if (!empty($_POST['user_street'])) {
update_user_meta( $user_id, 'user_street', $_POST['user_street'] );
}
else {
add_action('admin_notices', 'my_admin_notice');
return false;
}
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
Unfortunately, nothing happens.
I also tried with add_settings_error but I had the same problem.
Can someone give me a hand or just explain to me what I am doing wrong? It would be much appreciated! Thank you very much!
Looks like it's too late to add the admin_notice. An alternative is to use the action hook load-$page (that runs at the very beginning of a page load) and check if the user meta is empty. If so, trigger the notice.
add_action( 'load-profile.php', 'notice_so_23373245' );
add_action( 'load-user-edit.php', 'notice_so_23373245' );
function notice_so_23373245()
{
// Check to see if page is user-edit or profile
$user = isset( $_GET['user_id'] ) ? $_GET['user_id'] : get_current_user_id();
if( empty( get_user_meta( $user, 'user_street' ) ) )
add_action('admin_notices', 'my_admin_notice');
}

Woocommerce delivery&payment options - What´s wrong with my php code?

I am trying to hide some payment options in Woocommerce in case of specific delivery option. I tried to put this to my functions.php but It´s not working and I don´t know why.
Can you help me please?
function payment_gateway_disable_country( $available_gateways, $available_methods )
{
global $woocommerce;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
My research:
link 1
link 2
link 3
link 4
The available delivery methods do not get passed as a parameter in the filter woocommerce_available_payment_gateways - you need to load them in and check them.
The code below should remove the paypal payment option one if the user selects local delivery. If your checkout page is the AJAX based one then as the user changes the delivery method, the available payment options should also change.
function payment_gateway_disable_country($available_gateways) {
global $woocommerce;
$packages = $woocommerce->shipping->get_packages();
foreach ( $packages as $i => $package ) {
$chosen_method = isset( $woocommerce->session->chosen_shipping_methods[ $i ] ) ?
$woocommerce->session->chosen_shipping_methods[ $i ] : '';
if ('local_delivery' == $chosen_method) {
unset($available_gateways['paypal']);
break;
}
}
return $available_gateways;
}
add_filter(
'woocommerce_available_payment_gateways',
'payment_gateway_disable_country'
);
Let me know if you have any issues with the code; I did not have a chance to test it with woocommerce.
$available_methods will not be accessible inside your function. So first define it globally & access as global variable inside function, somewhat like this:
global $available_methods;
$available_methods = array( 'local_delivery' => 'yes' );
function payment_gateway_disable_country( $available_gateways )
{
global $woocommerce, $available_methods;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

How to remove "Quick edit" option in "register_post_meta" list page

I am using register_post_type in list page automatically it is four actions Edit/Quick Edit/trash/View. I want to remove "Quick edit" option from list page. How I can do this.
/*------------------------------------------------------------------------------------
remove quick edit for custom post type videos just to check if less mem consumption
------------------------------------------------------------------------------------*/
add_filter( 'post_row_actions', 'remove_row_actions', 10, 2 );
function remove_row_actions( $actions, $post )
{
global $current_screen;
if( $current_screen->post_type != 'videos' ) return $actions;
unset( $actions['edit'] );
unset( $actions['view'] );
unset( $actions['trash'] );
unset( $actions['inline hide-if-no-js'] );
//$actions['inline hide-if-no-js'] .= __( 'Quick Edit' );
return $actions;
}
It worked for me check with yours

Categories