![](/img/trans.png)
[英]Add class to WooCommerce pages via custom checkbox in WooCommerce product settings
[英]Disable add to cart button via custom checkbox in WooCommerce product settings
我們希望防止將某些即將推出的產品添加到購物車。
我們希望有一個復選框 select 我們要防止添加到購物車的特定產品。 我們現在有復選框並保存代碼。
我還發現了這一點: 刪除 Woocommerce 中特定產品類別的添加購物車按鈕和https://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/
我不確定,防止將特定產品添加到購物車的最佳方法是什么。 有沒有人建議最好的方法是什么?
// Add new checkbox to product edit page (General tab)
add_action( 'woocommerce_product_options_general_product_data', 'upcoming_checkbox_to_products' );
function upcoming_checkbox_to_products() {
woocommerce_wp_checkbox( array(
'id' => 'custom_upcoming',
'class' => '',
'label' => 'Prevent add to cart'
)
);
}
// -----------------------------------------
// Save checkbox via custom field
add_action( 'save_post', 'save_upcoming_checkbox_to_post_meta' );
function save_upcoming_checkbox_to_post_meta( $product_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['custom_upcoming'] ) ) {
update_post_meta( $product_id, 'custom_upcoming', $_POST['custom_upcoming'] );
} else delete_post_meta( $product_id, 'custom_upcoming' );
}
// -----------------------------------------
// Prevent add to cart
要向庫存產品選項添加復選框,請使用:
// Add checkbox
function action_woocommerce_product_options_inventory_product_data() {
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not)
'label' => __( 'My label', 'woocommerce' ), // Text in the editor label
'desc_tip' => false, // true or false, show description directly or as tooltip
'description' => __( 'Prevent add to cart', 'woocommerce' ) // Provide something useful here
) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data', 10, 0 );
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset, yes or no
$checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';
// Update meta
$product->update_meta_data( '_prevent_add_to_cart_button', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
要禁用簡單和可變產品的添加到購物車按鈕,請使用:
// Is_purchasable (simple)
function filter_woocommerce_is_purchasable( $purchasable, $product ) {
// Get meta
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
// Compare
if ( $hide_add_to_cart_button == 'yes' ) {
$purchasable = false;
}
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 );
// Is_purchasable (variable)
function filter_woocommerce_variation_is_purchasable( $purchasable, $product ) {
$hide_add_to_cart_button = get_post_meta( $product->get_parent_id(), '_prevent_add_to_cart_button', true );
// Compare
if ( $hide_add_to_cart_button == 'yes' ) {
$purchasable = false;
}
return $purchasable;
}
add_filter( 'woocommerce_variation_is_purchasable', 'filter_woocommerce_variation_is_purchasable', 10, 2 );
注意:有幾種方法可以禁用/刪除添加到購物車按鈕,因此這取決於您是要隱藏還是完全禁用該按鈕。
是的,您可以輕松添加一個復選框,該復選框將禁用添加到購物車按鈕,您還可以在本指南中查看其他禁用添加到購物車按鈕的方法。 希望你會發現它有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.