【发布时间】:2021-05-01 12:55:07
【问题描述】:
我们希望阻止某些即将推出的产品加入购物车。
我们希望有一个复选框来选择我们想要阻止添加到购物车的特定产品。我们现在有复选框并保存代码。
我还发现了这个:Remove add cart button in Woocommerce for a specific product category 和 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
【问题讨论】:
标签: php wordpress woocommerce product custom-fields