我正在尝试根据woocommerce产品页面中的颜色创建一个动态产品库。当我单击一种颜色(例如红色)时,我应该看到Red Gallery的照片。 为此,我将所有woocommerce画廊块替换为ajax(具有相同类的旧画廊)创建的新块。
新照片的加载工作正常,我会根据颜色获得画廊照片。 但是,当ajax加载新库时,滑块不起作用,我认为是因为创建滑块的woocommere js仅在页面加载时读取。
我认为我应该重新加载一些Woocommerce JS函数以使用其函数重新创建滑块,但是我不知道如何。
这是php文件,其中一个是我创建的新库,从ajax调用:
global $product;
$current_id = "";
if(isset($_POST['prodid']) && $_POST['prodid'] != "" ) {
$current_id = $_POST['prodid'];
$product = new WC_Product($current_id);
}
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes = apply_filters( 'woocommerce_single_product_image_gallery_classes', array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . ( $product->get_image_id() ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
) );
?>
<figure class="woocommerce-product-gallery__wrapper">
<?php
if ( $product->get_image_id() ) {
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src( 'woocommerce_single' ) ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '</div>';
}
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id ); // phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_product_thumbnails' );
?>
</figure>
这是在框颜色单击上调用的ajax函数
function changeGallery(selected_gallery, productID) {
jQuery(function($) {
var select_color = selected_gallery;
var xhttp;
$.ajax({
url : 'https://mysite.it/wp-admin/admin-ajax.php', // AJAX handler
data : { action : 'load_gallery', gallery : select_color, prodid : productID },
type : 'POST',
beforeSend: function() {
},
success : function( result ){
if( result ) {
$('.woocommerce-product-gallery').html(result);
//Reload here some woocommerce JS funcions?
}
}
});
});
}
答案 0 :(得分:0)
解决此类问题的方法是查看WooCommerce源代码,以了解插件如何初始化画廊。基于此,我认为您需要执行以下操作:
jQuery( '.woocommerce-product-gallery' ).each( function() {
jQuery( this ).wc_product_gallery();
} );
请参阅Github: single-product.js以供参考。