2017-05-16 7 views
2

Ich versuche, in WoCommerce 3.0 in WordPress einen benutzerdefinierten Stock_Status hinzuzufügen.Hinzufügen einer Aktienoption zu Woocommerce 3.0

Das Endziel ist das Hinzufügen einer 3. Inventaroption auf der Produktbearbeitungsseite, "On Hold", und Anzeigen des Lagerstatus auf der Produktseite.

Früher war ich in der Lage, die Methode verwenden hier: Add stock option in woocommerce, die zusätzliche Aktienoptionen zum Produkt Admin hinzuzufügen funktioniert, aber es sieht aus wie mit WooCommerce 3.0 gibt es etwas, meine Einstellungen auf der tatsächlichen Produktseite über Reiten . Es wurde auch auf der Produktseite gearbeitet, bis ich auf 3.0 aktualisiert habe.

Mein functions.php:

function add_custom_stock_type() { 
?> 
<script type="text/javascript"> 
jQuery(function(){ 
    jQuery('._stock_status_field').not('.custom-stock-status').remove(); 
}); 
</script> 
<?php 

woocommerce_wp_select(array('id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __('Stock status', 'woocommerce'), 'options' => array(
    'instock' => __('In stock', 'woocommerce'), 
    'outofstock' => __('Out of stock', 'woocommerce'), 
    'onhold' => __('On Hold', 'woocommerce'), // The new option !!! 
), 'desc_tip' => true, 'description' => __('Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce')));} 
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type'); 

function save_custom_stock_status($product_id) { 
update_post_meta($product_id, '_stock_status', wc_clean($_POST['_stock_status'])); 
} 
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1); 

function woocommerce_get_custom_availability($data, $product) { 
switch($product->get_stock_status) { 
    case 'onhold': 
     $data = array('availability' => __('On Hold', 'woocommerce'), 'class' => 'on-hold'); 
    break; 
    case 'instock': 
     $data = array('availability' => __('In stock', 'woocommerce'), 'class' => 'in-stock'); 
    break; 
    case 'outofstock': 
     $data = array('availability' => __('Out of stock', 'woocommerce'), 'class' => 'out-of-stock'); 
    break; 
} 
return $data; 
} 
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 3, 2); 

https://pastebin.com/EFtBVY9h

ich in der DB sehen, dass der stock_status richtig setzt "On Hold", mein benutzerdefinierten Status, wenn ich es auf den Server-Betreiber wählen , aber dies wird nicht auf der aktuellen Produktseite angewendet.

Um zu überprüfen, modifizierte ich meine price.php einfach Ausgang den Lagerstatus:

<p class="price"> 

<?php 
    $stockamount = $product->get_stock_quantity(); 
    $price = $product->get_price_html(); 
    $stockstatus = $product->get_stock_status(); 
    $pricelabelone = "Out of Stock"; 
    $pricelabeltwo = "On Hold"; 

    echo $stockstatus;    
?> 
</p> 

Doch obwohl ich Produkte auf „Halten“ (und dies ist das Speichern) ist die Produktseite immer "Auf Lager" ausgeben (Testprodukt: http://aegis-staging.byethost7.com/dgh/product/santa-cruz-h13-custom-42-cocobolo-and-moon-spruce/).

Was fehlt mir?

Antwort

0

Diese Lösung kann Ihnen helfen, Ihr Problem zu lösen. Verwenden Sie "get_post_meta()" anstelle von get_stock_status(), um Lagerstatus zu erhalten.

Gehen durch den "woocommerce_get_availability" Haken Code wie der Code unten

function woocommerce_get_custom_availability($data, $product) { 
$stock_status = get_post_meta($product->id , '_stock_status' , true); 
switch($stock_status ) { 
    case 'onhold': 
     $data = array('availability' => __('On Hold', 'woocommerce'), 'class' => 'on-hold'); 
    break; 
    case 'instock': 
     $data = array('availability' => __('In stock', 'woocommerce'), 'class' => 'in-stock'); 
    break; 
    case 'outofstock': 
     $data = array('availability' => __('Out of stock', 'woocommerce'), 'class' => 'out-of-stock'); 
    break; 
} 
return $data; 
} 
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 3, 2); 
+0

wie ein Charme, danke! – Kevin

Verwandte Themen