Rumah  >  Soal Jawab  >  teks badan

"Tunjukkan lencana jualan tanpa menunjukkan harga jualan di Woocommerce"

Saya sedang membangunkan woocommerce di WordPress dan saya ingin membuat beberapa lencana jualan tetapi mengabaikan harga jualan (hanya harga biasa).

Saya telah mencuba ini beberapa kali tetapi "Lencana Promosi" hanya muncul apabila saya meletakkan nombor pada harga jualan produk

Saya guna kod di bawah

add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
{
    global $post,$product;
    if ( ! $product->is_in_stock() ) return;
    $sale_price = get_post_meta( $product->id, '_price', true);
    $regular_price = get_post_meta( $product->id, '_regular_price', true);
    if (has_term('one', 'product_cat', $product->ID)) {
        return '<span class="onsale">one</span>';
    } elseif (has_term('two', 'product_cat', $product->ID)) {
        return '<span class="onsale">two</span>';
    } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) {
        return '<span class="onsale">three</span>';
    }
    return '<span class="onsale">Sale</span>';
}

P粉590929392P粉590929392260 hari yang lalu539

membalas semua(1)saya akan balas

  • P粉713866425

    P粉7138664252024-01-08 13:15:16

    Penapis itu sendiri hanya digunakan apabila produk dipromosikan.

    Anda perlu menutup tindakan jualan kilat yang berlaku sebelum menyemak sama ada produk itu dijual.

    Pertama, tanggalkan cangkuk jualan kilat teras.

    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
    remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );

    Kemudian, tambahkan fungsi jualan tersuai anda.

    add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_custom_sale_text', 10 );
    add_action( 'woocommerce_before_single_product_summary', 'woocommerce_custom_sale_text', 10 );

    Kemudian gunakan echo而不是return

    function woocommerce_custom_sale_text()
    {
        global $post,$product;
        if ( ! $product->is_in_stock() ) return;
        $sale_price = get_post_meta( $product->id, '_price', true);
        $regular_price = get_post_meta( $product->id, '_regular_price', true);
        if (has_term('one', 'product_cat', $product->ID)) {
            echo 'one';
        } elseif (has_term('two', 'product_cat', $product->ID)) {
            echo 'two';
        } elseif (has_term('three', 'product_cat', $product->ID) || empty($sale_price)) {
            echo 'three';
        }
        echo 'Sale';
    }

    balas
    0
  • Batalbalas