问题
I am trying to enable catalogue mode for a WooCommerce shop for users located everywhere but Australia.
The store has 2 types of products
- simple
- variable
I have tried "Turn Woocommerce shop into catalog for geolocated countries?" answer code, and it works well for "simple" products
The issue is that variable products still show the quantity field and add to cart button although they are not purchasable.
My understanding is that catalogue mode is essentially making products non purchasable.
If I could inform the code to look for product type "variable" using something like $product->is_type( 'variable' )
it could apply to both types of product.
I tried this which obviously did not work. Most likely the function cannot accept multiple variables:
//Disable purchasing of products if the country is not Australia
add_filter('woocommerce_is_purchasable', 'purchasable_country_based', 10, 2);
function purchasable_country_based( $is_purchasable, $product, $product->is_type( 'variable' ) ) {
// Enable for "AU" only geolocated users country
if( get_geolocated_user_country() ==='AU' )
return true;
else
return false;
}
So I am thinking I could add another instance of the code but to target variable products this time. I tried the following but it also did not work:
//Disable variable products if the country is not Australia
add_filter('woocommerce_is_purchasable', 'purchasable_country_based', 10, 2);
function purchasable_country_based( $is_purchasable, $product->is_type( 'variable' ) ) {
// Enable for "AU" only geolocated users country
if( get_geolocated_user_country() ==='AU' )
return true;
else
return false;
}
来源:https://stackoverflow.com/questions/61595606/catalogue-mode-for-variable-products-based-on-geolocation-in-woocommerce