简体   繁体   中英

Add personalized content after the “Add to Cart” button? Woocommerce

Woocommerce: How to add some html codes below add to cart in single product page? and i want add different codes for each single product!

I used a similar one for metabox, but I can not find a way to adapt it after "add to cart"


---- 1. Backend ----

// Adding a custom Meta container to admin products pages

add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
    function create_custom_meta_box()
    {
        add_meta_box(
            'custom_product_meta_box',
            __( 'Additional Product text <em>(optional)</em>', 'woocommerce' ),
            'add_custom_product_content_meta_box',
            'product',
            'normal',
            'default'
        );
    }
}

//  Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){
    function add_custom_product_content_meta_box( $post ){
        $text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : '';
        $args['textarea_rows'] = 6;

        echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>';

        wp_editor( $text_area, 'custom_text', $args );

        echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">';
    }
}

//Save the data of the Meta field

add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 );
if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){
    function save_custom_product_content_meta_box( $post_id, $post, $update  ) {

        if ( $post->post_type != 'product') return; // Only products

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) )
            return $post_id;

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_nonce' ] ) )
            return $post_id;

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        // Check the user's permissions.
        if ( ! current_user_can( 'edit_product', $post_id ) )
            return $post_id;

        // Sanitize user input and update the meta field in the database.
        if ( isset( $_POST[ 'custom_text' ] ) )
            update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) );
    }
}

---- 2. Frontend ----

// Add custom text under single product meta
add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 );
function add_custom_product_text() {
    global $product;

    $custom_text = get_post_meta( $product->get_id(), '_custom_text', true );

    if( empty($custom_text) ) return;

    echo '<div class="product-extra-text" style="margin-top:30px;">';

    echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>';

    // Updated to apply the_content filter to WYSIWYG content
    echo apply_filters( 'the_content', $custom_text );

    echo '</div>';
}

I put the code in parts, but it is united in functions of the theme.

Backend IMAGE: enter image description here

In your theme's function.php add the code, add_action('woocommerce_after_add_to_cart_button','show_custom_text');

function show_custom_text() { call the custom text inside this function }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM