Visualizza l'attributo del prodotto Woocommerce nella pagina di archivio

Aug 22 2020

Ho impostato un attributo per i miei prodotti per un tempo di consegna. E sto utilizzando le seguenti funzioni per visualizzarlo negli archivi dei prodotti, nelle pagine dei singoli prodotti, negli Ordini e nelle notifiche via email:

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

Ho due domande:

  1. C'è un modo per combinare queste funzioni per ottimizzare e pulire il codice?
  2. Per la pagina dell'archivio (ma non la pagina del singolo prodotto!) Voglio che il testo cambi quando il prodotto non è disponibile. Invece di non essere visualizzato affatto, vorrei che fosse "Esaurito".

Risposte

1 LoicTheAztec Aug 22 2020 at 00:20

Nota che la regola su StackOverFlow è una domanda alla volta. Puoi usare una funzione personalizzata che chiamerai su ogni funzione agganciata come:

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

Dovrebbe funzionare.

Nelle pagine di archivio solo quando il prodotto non è in stock, verrà visualizzato "Esaurito".