首页 > 解决方案 > Woocommerce中基于地理位置的可变产品目录模式

问题描述

我正在尝试为除澳大利亚以外的任何地方的用户启用 WooCommerce 商店的目录模式。

本店有2种产品

我试过将 Woocommerce 商店变成地理定位国家的目录?答案代码,它适用于“简单”产品

问题是可变产品仍然显示数量字段并添加到购物车按钮,尽管它们不可购买。


我的理解是目录模式本质上是使产品无法购买。

如果我可以通知代码使用类似的东西来查找产品类型“变量”,那么$product->is_type( 'variable' )它可以适用于两种类型的产品。


我试过这个显然没有用。该函数很可能不能接受多个变量:

//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;
}

所以我想我可以添加另一个代码实例,但这次是针对可变产品。我尝试了以下方法,但也没有用:

//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;
}

标签: phpwordpresswoocommercegeolocationhook-woocommerce

解决方案


推荐阅读