如何修改当前函数以执行以下操作:
1.从结帐处获取发货国家/地区(对于未登录用户和登录用户)-而不是IP地址
1.如果购物篮中的任何产品与数组中的任何产品匹配,并且发货国家/地区与数组中声明的任何产品匹配,则会将其从购物篮中删除-而不是禁用添加到购物车按钮
脚本-当前脚本获取产品ID,并获取用户IP地址,如果用户在X国家,则隐藏“添加到购物车”按钮...获取IP地址并非100%有效,通常返回null,并阻止用户购买,而实际上他们可能在例如英国,并且不限于购买所述产品...所以我们想改变它的运作方式
function filter_woocommerce_is_purchasable( $purchasable, $product ) {
// Validate product object.
if ( ! $product ) {
return $purchasable;
}
// Array of valid product IDs.
$valid_product_ids = array( 4003, 6104, 4004, 12651, 12647, 12644, 12643, 12640, 12639, 12637, 12633, 12630, 12429, 12427, 11538, 11536, 11534, 11299, 11300, 11301, 11302,11303, 11292, 11293, 11294, 11295, 11296, 11140, 10573, 10554, 10505, 10481, 10237, 9379, 9377, 9375, 9371, 9368, 7756, 7023, 6101, 5952, 5950, 5948, 4791, 4790, 4786, 4784, 4780, 4778, 4775, 4771, 4767, 3268, 3267, 3266, 3258, 12300, 12083, 11632, 11594, 11249, 11088, 10964, 10966, 10967, 10698, 10692, 10417, 10179, 9598, 9593, 9333, 9108, 6049, 6047, 6045, 5017, 5003, 4936, 4806, 4682, 4677, 4673, 4671, 4663, 4661, 4650, 4646, 4644, 4642, 4622, 4444, 3252, 3249, 3247, 3207, 3202, 3122, 3110, 3106, 12318, 12316, 12313, 12311, 12309, 12304, 12006, 12007, 12008, 12009, 12010, 12011, 11944, 11945, 11946, 11947, 11948, 19494, 11950, 11936, 11937, 11938, 11939, 11940,11941, 11942, 11928, 11929, 11930, 11931, 11932, 11933, 11934, 11920, 11921, 11922, 11923, 11924, 11925, 11926, 11912, 11913, 11914, 11915, 11916, 11917, 11918, 11904, 11905, 11906, 11907, 11908, 11909, 11910, 11896, 11897, 11898, 11899, 11900, 11901, 11902, 11888,11889, 11890, 11891, 11892, 11893, 11894, 11880, 11881, 11882, 11883, 11884, 11885, 11886, 11843, 1844, 11845, 11846, 11847, 11848, 11849, 11835, 11836, 11837, 11838, 11839, 11840, 11841, 11827, 11828, 11829, 11830, 11831, 11832, 11833, 11730, 11716, 11702,11706, 11707, 11708, 11709, 11710, 11711, 11712, 11713, 11714, 11698, 11610, 11600, 11278, 10631, 10483, 10416, 10413, 10411, 10402, 10183, 10172, 10170, 10154, 10055, 10051, 9909, 9911, 9912, 9913, 9162, 9159, 9158, 9156, 9106, 7698, 7700, 7701, 6696, 5961, 4930, 4928, 4926, 4889, 4890, 4891, 4892, 4893, 4894, 4895, 4881, 4882, 4883, 4884, 4885, 4886, 4887, 4686, 4682, 4659, 4650, 4622, 4420, 3395, 3399, 3400, 3401, 3379, 3319, 3289, 3299, 3297, 3298, 3193, 3176, 13321, 12465, 12443, 12013, 12012, 11687, 11688, 11689, 11690, 11691, 11681, 11628, 11683, 11684, 11685, 11675, 11676, 11677, 11678, 11679, 11307, 11305, 11177, 10720, 10711, 10712, 10713, 10714, 10388, 10175, 10167, 10162, 10049, 10047, 9767, 9306, 8228, 8235, 8236, 8237, 7815, 7241, 7018, 7019, 7020, 7021, 7013, 7004, 7005, 7006, 7007, 6841, 6776, 6773, 6770, 6682, 6683, 6684, 6685, 6686, 6667, 6669, 6670, 6671, 6672, 6104, 5012, 4924, 4919, 4912, 4913, 4914, 4916, 4917, 4918, 4905, 4906, 4907, 4908, 4909, 4910, 4911, 4987, 4898, 4899, 4900, 4901, 4902, 4903, 4873, 4835, 4831, 4829, 4823, 4804, 4652, 4011, 4012, 4013, 4008, 4009, 4010, 4005, 4006, 4007, 4002, 3999, 4000, 4001, 3996, 3997, 3998, 3254, 1701, 1699, 13747, 13659, 13527, 13608 ,13609, 13610, 13612, 13613, 13614, 13615, 13616, 13617, 13618, 31619, 13620, 13621, 13622, 12369, 12367, 12365, 12348, 12331, 12329, 12327, 12325, 12320, 12033, 11965, 11974, 11967, 11968, 11969, 11970, 11971, 11972, 11973, 11506, 11394, 11395, 11396, 11397, 11398, 11399, 11400, 11401, 11402, 11403, 11404, 11405, 11406, 11407, 11408, 11409, 11254, 10961, 10933, 10936, 10938, 10939, 10940, 10941, 10942, 10943, 10944, 10945, 10092, 9490, 9494, 9495, 9496, 9497, 9498, 9499, 9500, 9501, 9502, 9503, 9504, 9505, 9506, 9507, 9507, 9508, 9119, 9117, 9115, 8453, 8456, 8457, 8458, 8459, 8460, 8461, 8462, 8463, 8468, 8465, 8466, 8467, 8468, 8469, 8470, 8864, 8857, 8733, 8734, 8735, 8736, 8737, 8738, 8739, 8740, 8741, 8742, 8743, 8744, 8745, 8746, 8747, 8748, 8579, 8642, 8643, 8644, 8646, 8647, 8517, 8518, 8519, 8520, 8521, 8522, 8523, 8524, 8525, 8526, 8527, 8528, 8529, 8530, 8531, 8532, 8533, 7110, 7111, 7112, 7113, 7114, 7115, 7116, 7117, 7118, 7119, 7120, 7121, 7122, 7123, 7124, 7125, 6540, 8357, 6541, 6542, 6543, 6544, 6545, 6546, 6547, 6548, 6549, 6551, 6552, 6553, 6554, 6454, 8358, 6461, 6462, 6464, 6465, 6466, 6467, 6468, 6469, 6470, 6471, 6472, 6473, 6474, 6475, 6446, 6082, 4570, 4518, 4550, 4551, 4552, 4553, 4554, 4555, 4556, 4557, 4558, 4559, 4560, 4561, 4562, 4563, 4564, 4479, 4469, 4456, 4444, 12475, 12302, 11944, 11945, 11946, 11947, 11948, 11949, 119450, 11936, 11937, 11938, 11939, 11940, 11942, 11928, 11929, 11930, 11931, 11932, 11933, 11934, 11920, 11921, 11922, 11923, 11924, 11925, 11926, 11912, 11913, 11914, 11915, 11916, 11917, 11918, 11904, 11905, 11906, 11907, 11908, 11909, 11910, 11896, 11897, 11898, 11899, 11900, 11901, 11902, 11888, 11889, 11890, 11891, 11892, 11893, 11894, 11894, 11880, 11881, 11883, 11884, 11885, 11886, 11871, 11872, 11873, 11874, 11875, 11876, 11877, 11861,11862, 11863, 11864, 11865, 11866, 11867, 11863, 11854, 11855, 11856, 11857, 11858, 11859, 11843, 11845, 11846, 11847, 11848, 11849, 11835, 11836, 11837, 11838, 11839, 11840, 11841, 11827, 11828, 11829, 11830, 11831, 11832, 11833, 11819, 11820, 11821, 11822, 11823, 11824, 11825, 11810, 11811, 11812, 11813, 11814, 11815, 11816, 11802, 11804, 11805, 11806, 11807, 11808, 11809, 11725, 11727, 11728, 11729, 11702, 11706, 11707, 11708, 11709, 11710, 11711, 11712, 11713, 11714, 11578, 11280, 11281, 11282, 10416, 10413, 10411, 10402, 10062, 9781, 9779, 9777, 9775, 9773, 9771, 9750, 9742, 9732, 9727, 9484, 8212, 7678, 7676, 7674, 7672, 7670, 7668, 4822, 4820, 4813, 4808, 4802, 4737, 4735, 4733, 4731, 4729, 2727, 4721, 4717, 4715, 4711, 4710, 4709, 4708, 4707, 4706, 4697, 3289, 10928, 5990, 12024, 12002, 7014, 7015, 7016, 6996, 4934, 4932, 4915, 4874, 4875, 4878, 4879, 4837, 4838, 4839, 14760, 6857, 6855, 14800, 15031, 3386, 3387, 5069, 5007, 5008, 5069, 7769, 5076, 5081, 5082, 5052, 5070, 5071, 5072, 3138, 15075, 15057, 15177, 3319, 12630, 14016, 9811, 9822, 9825, 3113, 15507, 15427, 15426, 15425, 15424, 15422, 15421, 15420, 15419, 3106, 3107, 3108, 3109, 13310, 16371, 16369, 16364, 16357, 16372 );
// Array of valid country codes.
$valid_country_codes = array( 'AT', 'AU', 'BE', 'CA', 'CH', 'DE', 'DK', 'ES', 'FO', 'FR', 'GG', 'GI', 'GR', 'HR', 'IM', 'IE', 'IN', 'IS', 'IT', 'LT', 'LV', 'MT', 'MY', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK', 'US' );
// Get current product ID.
$product_id = $product->get_id();
// Only for specific products.
if ( in_array( $product_id, $valid_product_ids, true ) ) {
// Set default location empty.
$location = '';
// Geolocate location
// External IP fallback and API fallback for the country both are true here.
$geolocation = WC_Geolocation::geolocate_ip( '', true, true );
if ( ! empty( $geolocation['country'] ) ) {
$location = $geolocation;
file_put_contents( ABSPATH . 'debug.txt', print_r( $location, true ) );
}
// For specific countries.
if ( ! empty( $location ) && in_array( strtoupper( $location ), array( $valid_country_codes ), true ) ) {
// NOT purchasable.
$purchasable = false;
}
}
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 );
1条答案
按热度按时间jexiocij1#
您可以直接使用
WC_Geolocation::geolocate_ip
进行当前用户/访问者的地理定位。您还可以启用外部API回退,即在IP地址API上发送远程请求并获取用户IP地址。您还可以启用地理定位API回退,即再次使用IP地址发送API请求,如果您没有从请求标头获取地理定位,则获取地理定位。为什么您的代码有问题?
由于您在这种情况下传递IP地址,
get_country_code_from_headers()
类函数不会执行,并且如果外部API回退默认为false
,因此如果您获得了错误的IP,那么现在您可能会获得正确的位置。WooCommerce地理位置类代码如何工作
1.如果您不传递IP地址,那么它会从请求报头中获取IP地址和国家代码。
1.然后,如果GeoIP API回退(第3个参数)为
true
,则如果您未从中获取国家/地区,则它将从外部GeoIP API获取位置1.如果External IP fallback(第2个参数)为真,那么如果您没有从上述步骤中获得县,则它将从外部API获取IP地址,然后调用
geolocate_ip
传递IP地址,这是一个递归调用。你可以试试下面的代码,并检查如果你得到的国家或没有.