简体   繁体   English

在存档页面上显示 Woocommerce 产品属性

[英]Display Woocommerce product attribute on archive page

I have set up an attribute for my products for a delivery time.我已经为我的产品设置了交货时间属性。 And I am using the following functions to display it on product archives, on single product pages, on Orders and emails notifications:我正在使用以下功能在产品档案、单个产品页面、订单和电子邮件通知上显示它:

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>';
    }
}

I have two questions:我有两个问题:

  1. Is there a way o combine these functions to optimize and clean up the code?有没有办法结合这些功能来优化和清理代码?
  2. For the archive page (but not the single product page.) I want the text to change when the product is not on stock, Instead of not being displayed at all.对于存档页面(但不是单个产品页面。)我希望在产品没有库存时更改文本,而不是根本不显示。 I would like it to be "Sold Out".我希望它是“售罄”。

Note that the rule on StackOverFlow is one question at the time.请注意,StackOverFlow 上的规则是当时的一个问题。 You can use a custom function that you will call on each hooked function like:您可以使用自定义 function,您将在每个挂钩的 function 上调用它,例如:

// 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() ) );
}

It should works.它应该有效。

On archive pages only when product is not in stock, it will displays "Sold Out".只有当产品没有库存时,存档页面才会显示“已售罄”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce存档页面中显示特定产品属性 - Display specific product attribute in Woocommerce archive pages 在 woocommerce 产品页面中显示产品属性和分类 - display product attribute and taxonomy in woocommerce product page Woocommerce - 在商店/存档页面的产品标题下显示产品类别 - Woocommerce - Display the product category under product title in shop/archive page 显示Woocommerce存档页面中变体的大小产品属性值 - Display size product attribute values for variations in Woocommerce archive pages 在商店页面显示 WooCommerce 尺寸产品属性 - Display WooCommerce size product attribute on shop page wooCommerce在单个产品页面上显示属性描述 - wooCommerce display Attribute description on single product page 在 Woocommerce 购物车页面上显示特定的产品属性 - Display specific product attribute on Woocommerce cart page Woocommerce - 如何在产品存档中显示父类别页面上的文本? - Woocommerce - How to display the text on the parent category page in the product archive? 在woocommerce存档页面和加售/相关产品中显示自定义产品价格 - Display a custom product price in woocommerce archive page and for upsells/related products 需要在产品页面woocommerce中显示当前产品属性 - need to display the current product attribute in product page woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM