I need some of your Help, mamy thanks for your efforts.!
My problem is, that a plugin which is installed replaces the standard add to cart on Product page. I don’t want to Change it and i need to add an second Add to cart below it. The Case is, that a other plugin for free prosuct samples needs the regular Add to carry Button.
When i Add the global Script it works well, but on all products.
Add_action( 'woocommerce_product_meta_start', 'woocommerce_template_single_add_to_cart, 1 );
I want this on the product Tab where i can Turn IT on and off with a checkbox for the selected product.
My Code so far, but it doesn’t do anything. The checkbox works & checkbox saving also. But the snippet is not doing the command for the Adding to cart on meta start of Product frontend Page.
// Add 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, // true or false, show description directly or as tooltip
'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' );
// Add action when checkbox is selected with 'yes'
function add_action_when_checkbox_selected( $product_id ) {
$checkbox_value = get_post_meta( $product_id, '_add_to_cart_checkbox', true );
if ( $checkbox_value == 'yes' ) {
do_action( 'woocommerce_product_meta_start' );
do_action( 'woocommerce_template_single_add_to_cart' );
}
}
add_action( 'woocommerce_template_single_add_to_cart', 'add_action_when_checkbox_selected', 1 );
2
Answers
I resolved it, tested and works well. If someone will need it in Future:
You can use the
woocommerce_product_meta_start
hook. check the below code.Tested and works