Catalogue mode for variable products based on geolocation in Woocommerce

我的未来我决定 提交于 2020-05-17 06:23:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!