【问题标题】:How to display cart items upsells on WooCommerce checkout page?如何在 WooCommerce 结帐页面上显示购物车商品加售?
【发布时间】:2020-11-03 14:17:42
【问题描述】:

我需要一些帮助才能在结帐页面上显示我购物车中所有产品的加售情况,下面的代码给了我以下错误:

调用数组中的成员函数 get_upsell_ids()...

这是我的功能:

function show_cross_sell( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) {
    global $woocommerce;
    $items      = WC()->cart->get_cart();

    if ( !  $items ) {
        echo 'nothing';
      return;
    }

    // Handle the legacy filter which controlled posts per page etc.
    $args = apply_filters(
      'woocommerce_upsell_display_args',
      array(
        'posts_per_page' => $limit,
        'orderby'        => $orderby,
        'order'          => $order,
        'columns'        => $columns,
      )
    );
    
     foreach($items as $product) { 
    wc_set_loop_prop( 'name', 'up-sells' );
    wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );

    $orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
    $order   = apply_filters( 'woocommerce_upsells_order', isset( $args['order'] ) ? $args['order'] : $order );
    $limit   = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
    
    

    // Get visible upsells then sort them at random, then limit result set.
    $upsells = wc_products_array_orderby( array_filter( array_map( 'wc_get_product', $product->get_upsell_ids() ), 'wc_products_array_filter_visible' ), $orderby, $order );
    $upsells = $limit > 0 ? array_slice( $upsells, 0, $limit ) : $upsells;

    wc_get_template(
      'single-product/up-sells.php',
      array(
        'upsells'        => $upsells,

        // Not used now, but used in previous version of up-sells.php.
        'posts_per_page' => $limit,
        'orderby'        => $orderby,
        'columns'        => $columns,
      )
    );
         
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    您的代码中有一些错误...以下将合并购物车商品中的所有加售 ID(不重复),将它们显示为唯一的加售块:

    function show_items_upsells( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) {
        $cart  = WC()->cart;
    
        if ( WC()->cart->is_empty() ) {
            echo 'nothing';
            return;
        }
    
        $upsell_ids = $cart_item_ids = array();
        
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ) { 
            // Merge all cart items upsells ids
            $upsell_ids      = array_merge( $upsell_ids, $cart_item['data']->get_upsell_ids() );
            $cart_item_ids[] = $cart_item['product_id'];
        }
        
        // Remove cart item ids from upsells
        $upsell_ids = array_diff($upsell_ids, $cart_item_ids);
        $upsell_ids = array_unique($upsell_ids); // Remove duplicated Ids
        
        // Handle the legacy filter which controlled posts per page etc.
        $args = apply_filters( 'woocommerce_upsell_display_args', array(
            'posts_per_page' => $limit,
            'orderby'        => $orderby,
            'order'          => $order,
            'columns'        => $columns,
        ));
        
        wc_set_loop_prop( 'name', 'up-sells' );
        wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );
    
        $orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
        $_order  = apply_filters( 'woocommerce_upsells_order', isset( $args['order'] ) ? $args['order'] : $order );
        $limit   = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
        
        // Get visible upsells then sort them at random, then limit result set.
        $upsells = wc_products_array_orderby( array_filter( array_map( 'wc_get_product', $upsell_ids ), 'wc_products_array_filter_visible' ), $orderby, $_order );
        $upsells = $limit > 0 ? array_slice( $upsells, 0, $limit ) : $upsells;
    
        wc_get_template( 'single-product/up-sells.php', array(
            'upsells'        => $upsells,
    
            // Not used now, but used in previous version of up-sells.php.
            'posts_per_page' => $limit,
            'orderby'        => $orderby,
            'columns'        => $columns,
        ) );
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    用法: echo show_items_upsells();

    【讨论】:

    • @louisefourie 如果你喜欢/想要,你也可以请upvote这个答案。还是谢谢你。
    • 你的答案永远是最好的
    猜你喜欢
    • 2015-03-17
    • 2013-12-13
    • 2018-02-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多