skip to Main Content

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


  1. Chosen as BEST ANSWER

    I resolved it, tested and works well. If someone will need it in Future:

    // Add checkbox in the Product settings
    function add_custom_product_checkbox() {
        global $post;
        $checked = get_post_meta($post->ID, '_checkbox', true);
        ?>
        <div class="options_group">
            <p class="form-field">
                <label for="checkbox"><?php _e( 'Your description', 'textdomain' ); ?></label>
                <input type="checkbox" name="checkbox" id="checkbox" value="1" <?php checked( $checked, '1' ); ?> />
            </p>
        </div>
        <?php
    }
    add_action( 'woocommerce_product_options_general_product_data', 'add_custom_product_checkbox' );
    
    // Save the value of the checkbox
    function save_custom_product_checkbox( $post_id ) {
        $checkbox = isset( $_POST['checkbox'] ) ? '1' : '0';
        update_post_meta( $post_id, '_checkbox', $checkbox );
    }
    add_action( 'woocommerce_process_product_meta', 'save_custom_product_checkbox' );
    
    // function to add a Second add to cart on meta start
    trigger_woocommerce_template_single_add_to_cart() {
        $checked = get_post_meta( get_the_ID(), '_checkbox', true );
        if ( $checked === '1' ) {
            do_action( 'woocommerce_meta_start' );
            woocommerce_template_single_add_to_cart();
        }
    }
    add_action( 'woocommerce_single_product_summary', 'trigger_woocommerce_template_single_add_to_cart', 15 );
    

  2. You can use the woocommerce_product_meta_start hook. check the below code.

    // 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 );
    

    Tested and works

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search