亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在存檔頁面上顯示 Woocommerce 產品屬性

在存檔頁面上顯示 Woocommerce 產品屬性

PHP
翻閱古今 2023-10-22 21:09:13
我已為我的產品設置了交貨時間屬性。我使用以下功能將其顯示在產品檔案、單個產品頁面、訂單和電子郵件通知上:add_action( 'woocommerce_single_product_summary', 'product_attribute_delivery', 27 );function product_attribute_delivery(){    global $product;    $taxonomy = 'pa_delivery';    $value = $product->get_attribute( $taxonomy );    if ( $value && $product->is_in_stock() ) {        $label = get_taxonomy( $taxonomy )->labels->singular_name;        echo '<small>' . $label . ': ' . $value . '</small>';    }}add_action('woocommerce_order_item_meta_end', 'custom_item_meta', 10, 4 );function custom_item_meta($item_id, $item, $order, $plain_text)    {   $productId = $item->get_product_id();    $product = wc_get_product($productId);    $taxonomy = 'pa_delivery';    $value = $product->get_attribute($taxonomy);    if ($value) {        $label = get_taxonomy($taxonomy)->labels->singular_name;        echo  '<small>' . $label . ': ' . $value . '</small>';    }}add_action( 'woocommerce_after_shop_loop_item', 'product_attribute_delivery_shop', 1 );function product_attribute_delivery_shop(){    global $product;    $taxonomy = 'pa_delivery';    $value = $product->get_attribute( $taxonomy );    if ( $value && $product->is_in_stock() ) {        $label = get_taxonomy( $taxonomy )->labels->singular_name;        echo '<small>' . $label . ': ' . $value . '</small>';    }}我有兩個問題:有沒有辦法結合這些功能來優化和清理代碼?對于存檔頁面(但不是單個產品頁面?。?,我希望當產品沒有庫存時更改文本。我希望它“已售完”,而不是根本不顯示。
查看完整描述

1 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

您可以使用將在每個掛鉤函數上調用的自定義函數,例如:


// Custom function that handle the code to display a product attribute?

function custom_display_attribute( $product, $taxonomy = 'pa_delivery') {

? ? $value = $product->get_attribute( $taxonomy );

? ? if ( ! empty($value) && $product->is_in_stock() ) {

? ? ? ? $label = wc_attribute_label( $taxonomy );

? ? ? ? echo '<small>' . $label . ': ' . $value . '</small>';

? ? }

}


// On product archive pages

add_action( 'woocommerce_after_shop_loop_item', 'product_attribute_delivery_archives', 1 );

function product_attribute_delivery_archives() {

? ? global $product;


? ? custom_display_attribute( $product );


? ? // When product is out of stock displays "Sold Out"

? ? if ( ! $product->is_in_stock() ) {

? ? ? ? echo __("Sold Out", "woocommerce");

? ? }


}


// On product single pages

add_action( 'woocommerce_single_product_summary', 'product_attribute_delivery_single', 27 );

function product_attribute_delivery_single() {

? ? global $product;


? ? custom_display_attribute( $product );

}


// On orders and email notifications

add_action('woocommerce_order_item_meta_end', 'custom_item_meta', 10, 4 );

function custom_item_meta( $item_id, $item, $order, $plain_text ) {? ?

? ? custom_display_attribute( wc_get_product( $item->get_product_id() ) );

}

它應該有效。


只有當產品沒有庫存時,存檔頁面才會顯示“已售完”。


查看完整回答
反對 回復 2023-10-22
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號