我想在woocommerce产品页面后端添加一个自定义复选框,隐藏前端的"添加到购物车"按钮.我不想完全删除购买该项目的能力(仍然希望能够使用直接添加到购物车网址将项目添加到购物车并购买)所以我不想使用add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);或类似.
我目前取得的成就是:
添加自定义复选框
// Display Checkbox
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
global $post;
echo '<div class="product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_no_addcart_product',
'desc' => __('show or hide add to cart', 'woocommerce'),
'label' => __('Hide Add To Cart', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save Checkbox
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
// Custom Product Text Field
$no_addcart_product = isset( $_POST['_no_addcart_product'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_no_addcart_product', esc_attr( $no_addcart_product ));
} …Run Code Online (Sandbox Code Playgroud)