在 WooCommerce 中,我尝试禁用一系列产品 ID 的“添加到购物车”按钮,但我找不到问题。
我正在尝试使用这个功能:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$id=check(); // This function return an array of IDs
foreach ($id as $id_p){
return ($product->id = $id_p ? false : $is_purchasable);
}
}
这是我的
check()
功能代码(更新):
function check() {
$listproduit = get_woocommerce_product_list();
$score = get_score_user();
foreach ($listproduit as $products) {
if ($products[1] >= 5000) {
$listid = $products[0];
return $listid;
// print_r($listid);
}
}
return $listid;
}
但这行不通。
我做错了什么?
谢谢
针对 WooCommerce 3+ 进行了更新 (在 WooCommerce 8.1+ 中仍然有效)
使用
in_array()
代替,例如:
// Here your settings: The array of non-purchasable product IDs
function non_purchasable_product_ids() {
return array( 37, 53, 128, 129 );
}
// The active function: removing/disabling add to cart for the defined products
add_filter( 'woocommerce_variation_is_purchasable', 'filter_is_purchasable', 10, 2 );
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable', 10, 2);
function filter_is_purchasable($is_purchasable, $product ) {
if( in_array( $product->get_id(), non_purchasable_product_ids() ) {
return false;
}
return $is_purchasable;
}
其中
non_purchasable_product_ids()
是返回不可购买产品 ID 数组的函数(简化)。
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
我为此苦苦挣扎,但最终找到了答案。希望有帮助:
add_action('woocommerce_single_product_summary',
'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if(in_array($product->get_id(), array(414, 427))){
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
}
}