Добавить персонализированный контент после кнопки «Добавить в корзину»? WooCommerce - PullRequest
0 голосов
/ 24 июня 2019

Woocommerce: Как добавить несколько HTML-кодов ниже, чтобы добавить в корзину на странице одного продукта? и я хочу добавить разные коды для каждого продукта!

Я использовал аналогичный для metabox, но не могу найти способ адаптировать его после «добавить в корзину»


---- 1. Бэкэнд ----

// Добавление пользовательского мета-контейнера на страницы продуктов администратора

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() . '">';
    }
}

// Сохранить данные мета-поля

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. Интерфейс ----

// 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>';
}

Я поставил код по частям, но он объединен в функциях темы.

Backend IMAGE: введите описание изображения здесь

1 Ответ

1 голос
/ 24 июня 2019

В function.php вашей темы добавьте код, add_action ( 'woocommerce_after_add_to_cart_button', 'show_custom_text');

функция show_custom_text () { вызвать пользовательский текст внутри этой функции }

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...