Mysql - Woocommerce Cross Sell&向上销售单品

时间:2017-08-31 11:00:03

标签: php mysql wordpress woocommerce product

我想在单品页面中回应交叉销售和追加销售的产品。 对于Cross Sell我得到MYSQL语法错误

PHP:7.0.21 MySQL的:35年6月5日

我的代码

<?php
                    /* crossells */
            $crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 

            $crosssell_ids=$crosssell_ids[0];

            ?>

    <?php

        $args = array( 'post_type' => 'product', 'posts_per_page' => 8, 'post__in' => '$crosssell_ids', 'orderby' => 'rand' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <li class="product">    

                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                        <?php woocommerce_show_product_sale_flash( $post, $product ); ?>

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>

                        <h3><?php the_title(); ?></h3>

                        <span class="price"><?php echo $product->get_price_html(); ?></span>                    

                    </a>

                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

                </li>

    <?php endwhile;  ?>

前端显示

WordPress databasefout:[You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp' at line 1]

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.ID IN () AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'wc-gplsquote-req' OR wp_posts.post_status = 'complete' OR wp_posts.post_status = 'paid' OR wp_posts.post_status = 'confirmed' OR wp_posts.post_status = 'unpaid' OR wp_posts.post_status = 'pending-confirmation' OR wp_posts.post_status = 'cancelled' OR wp_posts.post_status = 'wc-gplsquote-sent' OR wp_posts.post_status = 'private') ORDER BY RAND() LIMIT 0, 8

1 个答案:

答案 0 :(得分:2)

看起来问题在于您获得交叉销售ID的方式,我相信您拥有的是旧版本的WC,您可以按如下方式更改代码:

<强>替换

$crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 
$crosssell_ids = $crosssell_ids[0];

。通过

global $product;
$crosssell_ids = $product->get_cross_sell_ids();

<强>更新

您的$crosssell_ids数组中的$args周围不需要引号,请尝试删除它们:

'post__in' => '$crosssell_ids'

应该是

'post__in' => $crosssell_ids