Getting product_id – Woocommerce – Ajax pop-up (Créer un compte)

  • WordPress :5.5
  • Statut : non résolu
Affichage de 1 message (sur 1 au total)
  • Auteur
    Messages
  • #2365674
    annahernandez99
    Bloqué
    Initié WordPress
    1 contributions

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>I have this code below, but just don’t seem to managed to get “product_id” into html to show the right product. </span><span style=”vertical-align: inherit;”>Can somebody point me into the right direction?</span></span>

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>I’m new to Ajax, so just don’t know how to fetch data from ajax into html.</span></span>

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>How could I get “product_id” into this form: <div> echo $ product_id </ div>?</span></span>

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”><script> </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>jQuery (document) .ready (function ($) { </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>var modal = document.getElementById (‘myModal’); </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>var span = document.getElementsByClassName (“close”) [0];</span></span>

    span.onclick = function() {
    modal.style.display = “none”;
    }
    window.onclick = function(event) {
    if (event.target == modal) {
    modal.style.display = “none”;
    }
    }
    // Let’s do the magic
    // make sure to change #myBtn to read more button selector
    $(‘.products > li .add_to_cart_button’).click(function(e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    // get url
    var url = $(this).attr(‘href’);
    var container = $(‘#myModal’).find(‘.popup’);
    var data = {
    action: ‘show_product’,
    url: url,

    };
    $.post(‘<?php echo esc_url( home_url() ); ?>/wp-admin/admin-ajax.php’, data, function(response) {
    // display the response
    console.log(response);
    $(container).empty();
    $(container).html(response);
    modal.style.display = “block”;
    });
    });
    });
    </script>
    <?php
    }
    add_action( ‘wp_footer’, ‘pop_up’ );

    add_action(‘wp_ajax_show_product’, ‘show_product_callback_wp’);
    add_action( ‘wp_ajax_nopriv_show_product’, ‘show_product_callback_wp’ );
    function show_product_callback_wp() {
    $url = $_POST[‘url’];
    $product_id = url_to_postid( $url );

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>// product content </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ content_post = get_post ($ product_id); </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ content = $ content_post-> post_content; </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ content = apply_filters (‘the_content’, $ content); </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ content = str_replace (‘]]>’, ‘]] & gt;’, $ content);</span></span>

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ output = “”; </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ output. = get_the_post_thumbnail ($ product_id, ‘medium’) ;; </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>$ output. = $ content;</span></span>

    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>echo $ output; </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>exit (); </span></span>
    <span style=”vertical-align: inherit;”><span style=”vertical-align: inherit;”>}</span></span>

Affichage de 1 message (sur 1 au total)
  • Vous devez être connecté pour répondre à ce sujet.