简体   繁体   English

在“添加到购物车”按钮后添加个性化内容? Woocommerce

[英]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? Woocommerce:如何在单个产品页面中添加以下添加到购物车中的html代码? 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" 我为metabox使用了类似的工具,但是在“添加到购物车”后找不到适应它的方法


---- 1. Backend ---- ---- 1.后端----

// Adding a custom Meta container to admin products pages //将自定义Meta容器添加到管理产品页面

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 //保存Meta字段的数据

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 ---- ---- 2.前端----

// 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.php中添加代码add_action('woocommerce_after_add_add_to_cart_button','show_custom_text');

function show_custom_text() { call the custom text inside this function } 函数show_custom_text(){调用此函数内的自定义文本}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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