WooCommerce Dipertingkat: Tunjukkan peningkatan berdasarkan atribut produk
P粉107991030
P粉107991030 2024-03-27 22:14:56
0
1
441

Saya menjumpai penyelesaian hebat ini untuk menunjukkan produk berkaitan berdasarkan atribut produk, tetapi tambahan pula kami ingin melakukan perkara yang sama untuk penjualan tinggi.

Apa yang saya cuba setakat ini tidak berkesan kerana saya percaya kita perlu menambah beberapa maklumat tambahan kerana fungsi related_products tidak sama dengan upsell.

add_filter( 'woocommerce_upsell_products', 'upsell_products_by_attribute', 10, 3 );
function upsell_products_by_attribute( $upsells, $product_id, $args ) {
    $taxonomy   = 'pa_attribute';

    $term_slugs = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'slugs'] );

    if ( empty($term_slugs) )
        return $upsells;

    $posts_ids = get_posts( array(
        'post_type'            => 'product',
        'ignore_sticky_posts'  => 1,
        'posts_per_page'       => 6,
        'post__not_in'         => array( $product_id ),
        'tax_query'            => array( array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug',
            'terms'    => $term_slugs,
        ) ),
        'fields'  => 'ids',
        'orderby' => 'rand',
    ) );

    return count($posts_ids) > 0 ? $posts_ids : $upsells;
}

Jadi saya tertanya-tanya sama ada boleh melakukan perkara yang sama dengan bahagian upsell.

P粉107991030
P粉107991030

membalas semua(1)
P粉806834059

Anda boleh mengeluarkan cangkuk tindakan lalai woocommerce_after_single_product_summary 并重新添加它,您可以自定义 upsells untuk produk. Semak kod di bawah. Kod tersebut akan masuk ke dalam fail functions.php tema aktif anda.

Tukar $taxonomy = 'pa_color'; kepada taksonomi tersuai anda.

function modify_woocommerce_upsell_display_based_on_attribute( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ){
    
    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );

    global $product;

    if ( ! $product ) {
        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,
        )
    );

    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 );

    // set your custom taxonomy here.
    $taxonomy   = 'pa_color';

    $term_slugs = wp_get_post_terms( $product->get_id(), $taxonomy, ['fields' => 'slugs'] );

    $posts_ids = get_posts( array(
        'post_type'            => 'product',
        'ignore_sticky_posts'  => 1,
        'posts_per_page'       => 6,
        'post__not_in'         => array( $product->get_id() ),
        'tax_query'            => array( array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug',
            'terms'    => $term_slugs,
        ) ),
        'fields'  => 'ids',
        'orderby' => 'rand',
    ) );

    if( !empty( $posts_ids ) ){

        // Get visible upsells then sort them at random, then limit result set.
        $upsells = wc_products_array_orderby( array_filter( array_map( 'wc_get_product', $posts_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 the previous version of up-sells.php.
                'posts_per_page' => $limit,
                'orderby'        => $orderby,
                'columns'        => $columns,
            )
        );
    }

}

add_action( 'woocommerce_after_single_product_summary', 'modify_woocommerce_upsell_display_based_on_attribute', 15 );
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan