- WordPress :5.5
- Statut : non résolu
- Ce sujet contient 0 réponse, 1 participant et a été mis à jour pour la dernière fois par
annahernandez99, le il y a 1 semaine et 3 jours.
-
AuteurMessages
-
12 janvier 2021 à 13 h 34 min #2365674
<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>-
Ce sujet a été modifié le il y a 1 semaine et 3 jours par
annahernandez99.
-
Ce sujet a été modifié le il y a 1 semaine et 3 jours par
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.