2016-05-15 3 views
0

Ich brauche diesen Filter, nur für eine bestimmte Beitrags-ID hinzugefügt/ausgelöst werden.Woocommerce-Buchungen ändern Datumsausgabe, um benutzerdefinierten Text pro Produkt oder Kategorie einzuschließen

add_filter('woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 3); 

function woocommerce_cart_item_product($item_data, $cart_item){ 

if (! empty($cart_item['booking'])) { 
    $date_format = apply_filters('woocommerce_bookings_date_format', wc_date_format()); 
    $time_format = apply_filters('woocommerce_bookings_time_format', ', ' . wc_time_format()); 
    $end_date = apply_filters('woocommerce_bookings_get_end_date_with_time', date_i18n($date_format, $cart_item['booking']['_end_date'])); 
    $item_data[] = array(
     'key' => __('for the week of', 'your-textdomain'), 
     'value' => $cart_item['booking']['_end_date'], 
     'display' => $end_date, 
    ); 
} 

return $item_data; 

}

Antwort

0

Je nachdem, wo dieser Code ausgeführt wird, dies sollte funktionieren:

add_filter('woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 3); 

function woocommerce_cart_item_product($item_data, $cart_item) { 

    global $post; 

    if (! empty($cart_item['booking']) && $post->ID == YOURUNIQUEIDHERE) { 
     $date_format = apply_filters('woocommerce_bookings_date_format', wc_date_format()); 
     $time_format = apply_filters('woocommerce_bookings_time_format', ', ' . wc_time_format()); 
     $end_date = apply_filters('woocommerce_bookings_get_end_date_with_time', date_i18n($date_format, $cart_item['booking']['_end_date'])); 
     $item_data[] = array(
      'key' => __('for the week of', 'your-textdomain'), 
      'value' => $cart_item['booking']['_end_date'], 
      'display' => $end_date, 
     ); 
    } 

    return $item_data; 

} 

Ich habe eine zusätzliche Kontrolle für die Post-ID in Ihrer if-Anweisung hinzugefügt. Hier können Sie IHREUNIQUEIDHERE durch die ID eines beliebigen Posts ersetzen. Hoffe das hilft dir aus!

+0

'globales $ post;' -Objekt wird zum Zeitpunkt des 'add to cart' -Ereignisses nicht verfügbar sein, da es zu dieser Zeit keine WP-Schleife gibt. – Sark

1

$cart_item Argument haben alle Details über das Produkt, das in den Warenkorb hinzugefügt wird.

$cart_item[ 'product_id' ] // Product Id (the product that is being added to cart) 
$cart_item[ 'variation_id' ] // Variable product's id (if that product is a variable) 
$cart_item[ 'variation' ] // Variation array (if that product is a variable) 
$cart_item[ 'quantity' ] // Line Item quantity 
$cart_item[ 'data' ] // Post object of the product that is being added 

So sollte Ihr Code so aussehen.

add_filter('woocommerce_get_item_data', 'woocommerce_cart_item_product', 10, 2); 

function woocommerce_cart_item_product($item_data, $cart_item) { 
    if (!empty($cart_item['booking']) && $cart_item["product_id"] == "Your Product Id") { 
     $date_format = apply_filters('woocommerce_bookings_date_format', wc_date_format()); 
     $time_format = apply_filters('woocommerce_bookings_time_format', ', ' . wc_time_format()); 
     $end_date = apply_filters('woocommerce_bookings_get_end_date_with_time', date_i18n($date_format, $cart_item['booking']['_end_date'])); 
     $item_data[] = array(
      'key' => __('for the week of', 'your-textdomain'), 
      'value' => $cart_item['booking']['_end_date'], 
      'display' => $end_date, 
     ); 
    } 
    return $item_data; 
} 

Ersetzen Sie diese "Your Product Id" mit Ihrer spezifischen Post-ID.

Verwandte Themen