WooCommerce 在类别页面的弹出窗口中显示加售产品 [英] WooCommerce display upsells products in a popup on category page

查看:33
本文介绍了WooCommerce 在类别页面的弹出窗口中显示加售产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类别页面上,当用户单击添加到购物车"按钮时,我想在弹出窗口中显示与该特定产品相关的加售产品,以便用户可以选择将加售产品与该产品一起添加到购物车中.

On a category page, when user click on "add to cart" button i want to show upsell products related to that specific product in a poupup, so that user has the option to add upsells products in cart along with that product.

<script>
jQuery(function() {
    jQuery(".single_add_to_cart_button, .add_to_cart_button").click(function(evt) {
        //evt.preventDefault();
        var product_id = jQuery(this).attr("data-product_id");

        jQuery.ajax({
            type: "POST",
            url: "<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php",
            data: {action: 'myajax-submit', id: product_id},
            cache: false,
            success: function(data) {
                jQuery("#result").html(data);
            }
        });
        //return false;
    })

})

在functions.php中我写了以下代码

in the functions.php i have written the following code

add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );
function myajax_submit(){
  $product_id = $_REQUEST['id'];
}

推荐答案

在探索插件核心文件后,我自己解决了这个问题.这是代码片段,它可以帮助寻找相同的人

After exploring the the plugin core files i have solve this myself. Here is the snippet of the code, which may help someone looking for the same

$product = new WC_Product($product_id);
    $upsells = $product->get_upsells();
   if (!$upsells)
        return;

$meta_query = WC()->query->get_meta_query();

    $args = array(
        'post_type' => 'product',
        'ignore_sticky_posts' => 1,
        'no_found_rows' => 1,
        'posts_per_page' => $posts_per_page,
        'orderby' => $orderby,
        'post__in' => $upsells,
        'post__not_in' => array($product->id),
        'meta_query' => $meta_query
    );

    $products = new WP_Query($args);
    if ($products->have_posts()) :
// Iterate over the each product

   endif;

这篇关于WooCommerce 在类别页面的弹出窗口中显示加售产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆