P粉5433443812023-08-01 11:44:52
You can use the `woocommerce_product_meta_start` hook. Please see the code below.
// Add a checkbox field to the Product tab in the admin area function add_checkbox_to_product_tab() { // Add checkbox field to the General tab woocommerce_wp_checkbox( array( 'id' => 'add_to_cart_checkbox', 'label' => 'Warenkorb Hinzufügen', 'desc_tip' => false, 'description' => 'ja' ) ); } add_action( 'woocommerce_product_options_general_product_data', 'add_checkbox_to_product_tab' ); // Save checkbox field value function save_checkbox_value( $product ) { $checkbox = isset( $_POST['add_to_cart_checkbox'] ) ? 'yes' : 'no'; $product->update_meta_data( 'add_to_cart_checkbox', $checkbox ); } add_action( 'woocommerce_admin_process_product_object', 'save_checkbox_value' ); // Remove standard "Add to Cart" button if checkbox value is 'no' function add_custom_add_to_cart_button() { global $product; $checkbox_value = $product->get_meta( 'add_to_cart_checkbox' ); if ( $checkbox_value == 'yes' ) { // Add your custom "Add to Cart" button code here echo '<a href="' . esc_url( $product->add_to_cart_url() ) . '" class="button alt">Custom Add to Cart</a>'; } } add_action( 'woocommerce_product_meta_start', 'add_custom_add_to_cart_button', 40 );
After testing, the code is valid.