Is there a Woocommerce Memberships function to get the "Cancel Subscription" url - php

I wonder if is there some method from WC_Memberships_User_Membership class to get a link to
cancel membership.
The way I'm getting the url takes me to the woocommerce my-account/memberships page, not to the real action of cancel subscription.
<?php
$memberships = wc_memberships_get_user_memberships();
if ( $memberships ) {
foreach( $memberships as $membership ) {
echo '<p>Plan: ' . $membership->plan->name . '</p>';
echo '<p>Since: ' . $membership->get_start_date('d-m-Y') . '</p>';
echo '<p>Until: ' . $membership->get_end_date('d-m-Y') . '</p>';
$url = 'https://poesicilina.com/?cancel_membership=' . $membership->id;
echo '<p>Cancel membership</p>';
}
}

<?php
function cancelbutton(){
$user_id = get_current_user_id();
$memberships = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships as $membership => $val) {
$url = $val->get_cancel_membership_url();
echo "<a href='$url'>Cancel</a>";
};
add_shortcode( 'cancelbutton', 'cancelbutton' ); ?>
Use shortcode [cancelbutton]

Related

Replace zero with FREE for WooCommerce shipping rates on single product page

Based on the answer on my previous question, Issue display shipping data on WooCommerce single product page, I'm trying to replace the 0 with FREE or whatever word I choose.
Problem is, whatever I do, it does not work. This is my attempt and I need help with it:
add_action('woocommerce_after_add_to_cart_form', 'display_shipping_on_product_page', 10, 0);
function display_shipping_on_product_page(){
// get all zones
$zones = WC_Shipping_Zones::get_zones();
// get the shop base country
$base_country = WC()->countries->get_base_country();
$base_city = WC()->countries->get_base_city();
// start display of table
echo '<div>' . __( '<b>Available Shipping</b>', 'woocommerce' );
echo '<br><small><span class="shipping-time-cutoff">All orders are shipped from the '.$base_country.'. Order before 12AM Mon-Fri for same day delivery within '.$base_city.'. Order before 3PM Mon-Thu for next day delivery.</span></small>';
echo '<small><table class="shipping-and-delivery-table">';
// get name of each zone and each shipping method for each zone
foreach ( $zones as $zone_id => $zone ) {
echo '<tr><td>';
echo '<strong>' . $zone['zone_name'] . '</strong>' . '</td><td>';
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
if ( isset( $instance['min_amount'] ) ) {
$instance_min_amount = $instance['min_amount'];
} else {
$instance_min_amount = 0;
}
if ( isset( $instance['cost'] ) ) {
$instance_cost = $instance['cost'];
} else {
$instance_cost = str_replace($instance_cost, "FREE" );
}
$cost = $instance_cost ? $instance_cost : $instance_min_amount;
$above = $instance_min_amount ? 'above ' : '';
echo $instance['title'] . ': ' . $above . '<strong>' . wc_price( $cost ) . '</strong>' . '<br>';
}
echo '</td></tr>';
}
echo '</table></small></div>';
}
This is what I tried to add / edit without success:
$instance_cost = str_replace($instance_cost, "FREE" );
To display 'free' instead of 0, you need to change your if/else conditions. Explanation via comment tags added to my answer.
So you get:
function action_woocommerce_after_add_to_cart_form() {
// get all zones
$zones = WC_Shipping_Zones::get_zones();
// get the shop base country
$base_country = WC()->countries->get_base_country();
$base_city = WC()->countries->get_base_city();
// start display of table
echo '<div>' . __( 'Available Shipping', 'woocommerce' );
echo '<br><small><span class="shipping-time-cutoff">All orders are shipped from the '.$base_country.'. Order before 12AM Mon-Fri for same day delivery within '.$base_city.'. Order before 3PM Mon-Thu for next day delivery.</span></small>';
echo '<small><table class="shipping-and-delivery-table">';
// get name of each zone and each shipping method for each zone
foreach ( $zones as $zone_id => $zone ) {
echo '<tr><td>';
echo '<strong>' . $zone['zone_name'] . '</strong>' . '</td><td>';
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
// Initialize
$above = '';
$output_cost = __( 'Free', 'woocommerce' );
// Cost isset
if ( isset( $instance['cost'] ) ) {
// NOT empty
if ( ! empty ( $instance['cost'] ) ) {
// Output
$output_cost = wc_price( $instance['cost'] );
}
}
// Min amount isset
if ( isset( $instance['min_amount'] ) ) {
// NOT empty
if ( ! empty ( $instance['min_amount'] ) ) {
// Above
$above = __( 'above ', 'woocommerce' );
// Output
$output_cost = wc_price( $instance['min_amount'] );
}
}
echo $instance['title'] . ': ' . $above . '<strong>' . $output_cost . '</strong>' . '<br>';
}
echo '</td></tr>';
}
echo '</table></small></div>';
}
add_action( 'woocommerce_after_add_to_cart_form', 'action_woocommerce_after_add_to_cart_form', 10, 0 );

Stripped HTML in product meta

I made a function for Woo store to display custom taxonomies. And somehow my span conatiners for each are destroyed. Here's the code:
add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );
function add_my_meta() {
$series = the_terms($post->ID, 'series');
if ($series) {
$meta_output = '<span style="display:block;">Series: ';
$meta_array = array();
foreach ($series as $serie) {
$meta_array[] = '' . $serie->name . '';
}
$meta_output .= join( ', ', $meta_array ) . '</span>';
}
return $meta_output;
}
So expected output is <span style="display:block;">Series: My Series</span>
Current output is My Series
Spans and text removed. Never faced that problem before, what's the problem and how to solve?
Found some mistakes — needed to use get_the_terms (was the_terms) and $serie->term_id (was $serie->slug)
add_action( 'woocommerce_product_meta_start', 'add_my_meta', 1 );
function add_my_meta() {
$series = get_the_terms($post->ID, 'series');
if ( is_array($series) ) {
$meta_array = array();
foreach ($series as $serie) {
$meta_array[] = '' . $serie->name . '';
}
echo '<span class="tagged_as">Series: ' . implode( ', ', $meta_array ) . '</span>';
}
}

Wordpress - using variable in menu URL

Is it possible to use php variable in menu URL, that I defined in function.php?
This is actually not working:
For example, in variable $path I store username of every user so they can visit their own profile.
You can use a hook and add the PHP programmatically.
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args ) {
if( is_admin() )
return $items;
foreach( $items as $item ) {
if( 'profil' == $item->post_title ) {
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$item->url .= '/' . $username;
}
}
return $items;
}
I found this code from another answer on this community.
In Wordpress You Can Do Direct Like this for get the current user details:
<?php
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
Reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user

WordPress Plugin Footer Menu Error

I am getting an out of the box error for a plugin and I am wondering if there is a quick fix.
I have googled the issue and checked the creator of the plugin's website. No help.
The plugin needs:
<?php do_shortcode('[print_wp_footer]'); ?>
Added to Footer.php for the theme.
WordPress then gives an error of:
Warning: Invalid argument supplied for foreach() in /.../wp-content/plugins/wp-footer-menu/main.php on line 192
Here is the line by itself
foreach ($values as $attr=>$val) {
Here is the section of code with line "192" with arrows.
<?php
}
function wp_footer_menu_confirm_delete ($delete) {
$base_uri = wp_footer_menu_base_uri();
// Find out about this menu item.
$menu_items = get_option ( 'footer_menu_links' );
$item_to_delete = explode( ',', $menu_items[$delete] );
$item_title = $item_to_delete[0];
$item_address = $item_to_delete[1];
// Create form for user to confirm option.
echo '<h3>Confirm Delete</h3>';
echo '<p>Are you sure you want to delete this menu item?</p>';
echo '<p>Title: ' . $item_title . '</p>' ;
echo '<p>Address: ' . $item_address . '</p>';
echo '<form method="post" action="' . $base_uri . '">';
echo '<input type="hidden" name="delete_key" value="' . $delete . '" />';
echo wp_nonce_field( 'confirm_delete', 'delete' );
echo '<input type="submit" class="button-primary" value="Delete item" />';
echo '</form>';
}
function wp_footer_menu_process() {
if ( isset( $_GET[ 'delete' ] ) ) {
$nonce = $_GET ['nonce'];
if ( wp_verify_nonce( $nonce, 'footer_delete' ) ) {
wp_footer_menu_confirm_delete ( $_GET[ 'delete' ] );
}
return 0;
} else if ( isset( $_POST[ 'delete_key' ] ) && check_admin_referer ( 'confirm_delete', 'delete' ) ) {
$menu_items = get_option ( 'footer_menu_links' );
$key = $_POST['delete_key'];
unset ( $menu_items[$key] );
update_option ( 'footer_menu_links', $menu_items );
}
if ( isset( $_POST[ 'link_title' ] ) && check_admin_referer( 'footer_menu', 'add' ) ) {
$link_title = $_POST ['link_title'];
$link_address = $_POST ['link_address'];
$link_order = $_POST ['link_order'];
$new_link = $link_title . ',' . $link_address . ',' . $link_order;
$footer_links = get_option( 'footer_menu_links' );
if ($footer_links == '') {
$footer_links = array();
}
$new_links = array_push( $footer_links, $new_link );
update_option ( 'footer_menu_links', $footer_links );
}
if ( isset( $_POST[ 'font-size' ] ) && check_admin_referer( 'footer_menu', 'save' ) ) {
if (empty($_POST['auto-footer'])) {
$_POST['auto-footer'] = 'no';
}
if (empty($_POST['auto-sticky'])) {
$_POST['auto-sticky'] = 'no';
}
update_option('wp_footer_values', $_POST);
echo '<div class="wp_footer_info" style="margin:0 auto;margin-top:5px;text-align:center;">Customizations Saved</div>';
}
return 1;
}
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
--192--> foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
if ($values['auto-sticky'] == 'yes') {
?>
<style type="text/css">
.wp_footer_sticky {
position:fixed;
bottom: 0;
width: 100%;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#wp_footer_menu').addClass('wp_footer_sticky');
});
</script>
<?php
It seems to depend to returned value of the following code :
$values = get_option('wp_footer_values');
Depending on the WordPress codex, if the option does not exist it return a boolean FALSE. To you will need to verify if that the $values variable is not empty. Check with the following code.
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
if (!empty($values)) { // <-- verify it's not empty
foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
// [...}
} // <-- don't forget to close the if statement just after the end of the foreach statement
Hope that help.
Source : get_option()

Can't get 'get_the_title' to work in a function

I've used this in other functions, but it doesn't seem to work in this particular one...
<?php
$page = get_the_title();
$blogusers = get_users('orderby=display_name');
foreach ($blogusers as $user) {
$cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );
if (!empty($cpt_count)) {
echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
}
}
?>
If I change $page = get_the_title(); to $page = 'title';then it works, so it's something with get_the_title(); but I'm not sure what because it has worked in other functions.
The most common reason why "get_the_title()" wont work is if it is not in "the loop". Make sure that you call the function from within the loop only. If called from elsewhere, you will need to pass the page/post id to the function.
You'll get more information here:
http://codex.wordpress.org/Function_Reference/get_the_title
Try This:
<?php
global $post;
$page = $post->post_title;
$blogusers = get_users('orderby=display_name');
foreach ($blogusers as $user) {
$cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );
if (!empty($cpt_count)) {
echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
}
}
?>

Categories