PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] WooCommerce Subscription hide buttons for specific products

 March 03, 2022     php, subscription, woocommerce, wordpress     No comments   

Issue

Based on that question: Disable cancel subscription for a specific product in WooCommerce we try to adjust the code becasue this code here does not 100% fit for our needs.

We created the code below to hide subscription buttons on myaccount page but only for specific subscription products. For all variation from a variable product. For that we want work with a array to define all variation ID's. But I think it will also work, if we just add the objekt ID from the product and for every variation. However, this is what I have so far. Currently it hides the buttons for every subscription. So I think there is a logic issue in my code.

Its strange.. when I set the ID then it somehow hide the buttons for the other products and not for the product with the specific ID. So the code is applied to all other products and not to the one with the exact ID. Why?

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
   $is_my_product = false;
   if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
     foreach ( $subscription_items as $item_id => $item ) {
         $product = $item->get_product();
         if ( $product->get_id() == 16189, 16190 ) {
            $is_my_product = true;
            break;
         }
     }
   }
   if ( $is_my_product ) return $actions;

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
          case 'change_payment_method':   // Hide "Change Payment Method" button?
         // case 'change_address':      // Hide "Change Address" button?
          case 'switch':          // Hide "Switch Subscription" button?
          case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
          case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
          case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
          case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }

    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

Any idea whats wrong here? :)

Cheers & Thanks


Solution

First, there is error within this line if ( $product->get_id() == 16189, 16190 ) {. You are looking for is in_array function.

Define your specific product ids array.

$specific_products = array( 16189, 16190 );

Then add a condition whether your product is in your define $specific_products using the in_array function.

if ( in_array( $product->get_id(), $specific_products ) ) {

also you have to use woocommerce_my_account_my_orders_actions filter hook.

Complete code for 'woocommerce_my_account_my_orders_actions' filter hook.

function eg_remove_my_subscriptions_button( $actions, $subscription ) {

    $is_my_product = false;
    $specific_products = array( 16189, 16190 );
    if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
        foreach ( $subscription_items as $item_id => $item ) {
            $product = $item->get_product();
            if ( in_array( $product->get_id(), $specific_products ) ) {
                $is_my_product = true;
                break;
            }
        }
    }

    if ( !$is_my_product ) return $actions;

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            case 'change_payment_method':   // Hide "Change Payment Method" button?
            // case 'change_address':      // Hide "Change Address" button?
            case 'switch':          // Hide "Switch Subscription" button?
            case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
            case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
            case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
            case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
            }
    }

    return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

Before

enter image description here

After

enter image description here

Complete code for 'woocommerce_my_account_my_orders_actions' filter hook.

function eg_remove_my_subscriptions_button( $actions, $subscription ) {

    $is_my_product = false;
    $specific_products = array( 16189, 16190 );
    if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
        foreach ( $subscription_items as $item_id => $item ) {
            $product = $item->get_product();
            if ( in_array( $product->get_id(), $specific_products ) ) {
                $is_my_product = true;
                break;
            }
        }
    }

    if ( !$is_my_product ) return $actions;

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            case 'change_payment_method':   // Hide "Change Payment Method" button?
            // case 'change_address':      // Hide "Change Address" button?
            case 'switch':          // Hide "Switch Subscription" button?
            case 'resubscribe':     // Hide "Resubscribe" button from an expired or cancelled subscription?
            case 'pay':         // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
            case 'reactivate':      // Hide "Reactive" button on subscriptions that are "on-hold"?
            case 'cancel':          // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
            }
    }

    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

Before

enter image description here

After

enter image description here



Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing