Display Woocommerce product attribute on archive page

白昼怎懂夜的黑 提交于 2021-01-28 07:31:05

问题


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".

回答1:


Note that the rule on StackOverFlow is one question at the time. You can use a custom function that you will call on each hooked function like:

// 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".



来源:https://stackoverflow.com/questions/63527081/display-woocommerce-product-attribute-on-archive-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!