Comments (14)
From store owner:
We have found that no matter how remote the chance may seem, our users will find a way to do it. And there is no note too big to miss. ;) There's little more frustrating for the user than telling them the child they sponsored is actually not available.
I'm now thinking that we could add a disabled class to the "Add to cart button"
Something like this would remove it:
function remove_product_description_add_cart_button() {
global $product;
// add conditional based on $product->id
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
or just filter the text using woocommerce_product_add_to_cart_text
from woocommerce-subscriptions-restrict-product.
Change to using _is_purchasable (instead of _is_visible) on all woocommerce pages except Cart and Checkout. Have not added any notification text at this time, since I'm not sure it's necessary. Closing for now unless this comes up again.
from woocommerce-subscriptions-restrict-product.
Change to using _is_purchasable (instead of _is_visible) on all woocommerce pages except Cart and Checkout.
@NickGreen you might want to invert that condition and make sure it's not purchasable only on certain pages, like the product page, shop page etc. Just in case there are other pages/conditions where the cart is being used (like the change payment method customer flow for example?).
I think it also still makes sense to make sure it's not visible so it doesn't show up in the catalogue/shop page, given it's not purchasable.
from woocommerce-subscriptions-restrict-product.
Reopening to put further thought into how to disable the product and make it invisible.
from woocommerce-subscriptions-restrict-product.
@james-allan Would you have a moment to help me strategize?
I would like to set a product to be not purchasable, except still allow manual renewals and manual payment of failed orders (using the 'pay now' links) to go through.
I was thinking that I could check if it is associated with a renewal order in any way and then allow it to be purchasable. Is there some characteristic that comes along with manual renewals and failed order payments that I could check against? Could I just check if there's a cart hash in the URL?
I'm happy to jump on a chat to clarify what I'm trying to do if you want. 🙇
from woocommerce-subscriptions-restrict-product.
I would like to set a product to be not purchasable, except still allow manual renewals and manual payment of failed orders (using the 'pay now' links) to go through.
Subscriptions does something kind of similar to this when we make limited products purchasable while customers are resubscribing, switching and renewing.
I was thinking that I could check if it is associated with a renewal order in any way and then allow it to be purchasable. Is there some characteristic that comes along with manual renewals and failed order payments that I could check against?
Yep you can check if a cart item and therefore a product is associated with a manual renewal via the cart item meta we set.
Some points for reference:
The hook to use: https://github.com/Prospress/woocommerce-subscriptions/blob/2.2.16/includes/class-wcs-limiter.php#L26
Function contents which makes products purchasable while renewing and resubscribing: https://github.com/Prospress/woocommerce-subscriptions/blob/2.2.16/includes/class-wcs-limiter.php#L169-L196
I'm happy to jump on a chat to clarify what I'm trying to do if you want
Ok, no worries. I'm happy to do that if you would like. :)
from woocommerce-subscriptions-restrict-product.
Thanks! It totally makes sense that this would've already been accomplished with limiting. This is exactly what I'm trying to do!
It looks like if the product is set to be not purchasable, this will check if it's part of one of the above situations and make it purchasable just for those.
It did just occur to me that I might be able to directly reference that function... which would be an amazing shortcut. I don't see anything in that function which is specific to limiting, and it appears to be reusable for my use case! Thoughts?
from woocommerce-subscriptions-restrict-product.
It did just occur to me that I might be able to directly reference that function... which would be an amazing shortcut. I don't see anything in that function which is specific to limiting, and it appears to be reusable for my use case! Thoughts?
I don't think you'll be able to use that function, unfortunately. It only falls through to the logic which makes the product purchasable if false === self::is_purchasable_product( $is_purchasable, $product )
and is_purchasable_product()
will only return false
if the product is limited. So you'll need to add your own function. Let me know if you need a hand with that. I can see you're already hooked onto woocommerce_is_purchasable
with [wcs_restriction_is_purchasable()
](https://github.com/Prospress/woocommerce-subscriptions-restrict-product/blob/master/wcs-restrict-product.php#L158-L161) so you'll need to add an extra condition which checks if the cart contains a renewal to that product.
How should resubscribing behave by the way? The subscription could be pending-cancel
, expired
, cancelled
if the customer is resubscribing so it could have already freed up the products purchasable quantity and so resubscribing shouldn't bypass the restricted check, right?
from woocommerce-subscriptions-restrict-product.
you'll need to add your own function. Let me know if you need a hand with that.
Thanks. Once I have it mostly done, I'd really love it if you could help code review the whole thing to help me find loopholes that I might've missed. Also, getting some tips on code standards would be great. I'll yell when I get there :)
How should resubscribing behave by the way? The subscription could be pending-cancel, expired, cancelled if the customer is resubscribing so it could have already freed up the products purchasable quantity and so resubscribing shouldn't bypass the restricted check, right?
That's right, and that's a very helpful question. pending-cancel
is OK (I'm still including that as an "uncancelled" status), but canceled
or expired
should not bypass the check.
It looks like resubscribe logic is built into the function I'm going to be copying from, so I will just need to add in an extra conditional based on ended status.
from woocommerce-subscriptions-restrict-product.
Renewal/resubscribe logic added. Need to add switching logic still, so leaving this open.
from woocommerce-subscriptions-restrict-product.
@james-allan I think I need a little help rubber ducking the switching logic. What I'm trying to do is allow switching between variations of a variable product. This is because my restriction happens on a product level, so if they are switching to another variation of the same product, that means that they already took up one slot, and they are just replacing that.
Could I simply say "if a variation, then let the switch happen?" Can you think of any conditions where people might be trying to switch to a variation that this would not be good? Maybe if they are trying to switch between grouped variable products..? (though ostensibly that's not supported)
The more thorough version would be to say "If the variation I'm switching to and the variation I'm switching from have the same parent product, then allow it." I'm not sure how to get the variation I'm switching from, though, or even if that's necessary. You know what I mean?
from woocommerce-subscriptions-restrict-product.
So there are a couple of things you'll need to look out for to support switching:
- Firstly, making the product purchasable if there are switch args in
$_GET
(https://cloudup.com/cG9KZc7vOFC). - WC will also check if a product is purchasable during the cart calculations and so you'll need to make sure the product is then purchasable while the cart contains that switch.
So to do that you can do something like:
function wcs_restriction_is_purchasable_switch( $is_purchasable, $product ) {
// Check if the product is restricted first
$is_purchasable = wcs_restricted_is_purchasable( $is_purchasable, $product->get_id() );
// We're only concerned with variable products during switch because individual grouped products should be handled on their own.
if ( false === $is_purchasable && 'variable-subscription' === $product->get_type() ) {
// If the customer has requested to switch
if ( isset( $_GET['switch-subscription'] ) ) {
$is_purchasable = true;
// If the cart contains a switch to this product
} elseif ( WC_Subscriptions_Switcher::cart_contains_switch_for_product( $product ) ) {
$is_purchasable = true;
// If restoring cart from session, the cart doesn't exist so cart_contains_switch_for_product will fail.
} elseif ( isset( WC()->session->cart ) ) {
foreach ( WC()->session->cart as $cart_item_key => $cart_item ) {
if ( $product->get_id() == $cart_item['product_id'] && isset( $cart_item['subscription_switch'] ) ) {
$is_purchasable = true;
break;
}
}
}
}
return $is_purchasable;
}
add_filter( 'woocommerce_subscription_is_purchasable', 'wcs_restriction_is_purchasable_switch', 12, 2 );
Like you said, you'll also need to think about what happens when switching between grouped products. Grouped products could have different restriction settings since they are individual products right so their is_purchasable
status should be handled on their own. So along with that, I've only handled variable subscriptions here. I haven't really tested this in the context of this plugin but it's very similar to WCS_Limiter::is_purchasable_switch()
from woocommerce-subscriptions-restrict-product.
@james-allan Thanks for the example! The version I came up with was very similar, but slightly less lucid :)
you'll also need to think about what happens when switching between grouped products
I see in issue 2406 switching between grouped variable products will be fixed, so I will need to account for that. Thanks for the tip to look in the _GET args for the sub being switched from, I think that will solve the grouped product problem, because I can just check if they are switching between variations within the same variable product or not. 👍
from woocommerce-subscriptions-restrict-product.
Added all switch logic, and passed basic testing. I'm closing out this issue in favor of the testing thread, since I'm moving to that phase.
from woocommerce-subscriptions-restrict-product.
Related Issues (17)
- subs cancelled in admin don't decrement product count in wcs_restriction_cache HOT 1
- No way to turn off product-level restriction if sitewide restriction is activated HOT 1
- Batch the cache query
- Add Cache display page HOT 1
- Doesn't allow manual renewals with recent version of WCS HOT 2
- Add visibility toggle HOT 1
- Refresh cache after sub is created in admin area HOT 2
- Actually i see is not working anymore
- Allow for n-to-1 relationship HOT 6
- Limit subscriptions but enable up/downgrades
- Add UI HOT 1
- Doesn't prevent purchase of more than the number specified HOT 1
- Testing and Review HOT 5
- Account for multiple quantities being purchased at once HOT 6
- Allow for customization/translation of warning messages HOT 2
- Not restricting at all. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from woocommerce-subscriptions-restrict-product.