Wie kann ich in Woocommerce einen benutzerdefinierten Inhalt für ein bestimmtes Produkt auf einzelnen Produktseiten hinzufügen?
Fügen Sie benutzerdefinierte Inhalte für ein bestimmtes Produkt auf WooCommerce-Einzelproduktseiten hinzu
Balaji
LoicTheAztec
Da Ihr Screenshot nicht so klar ist, wo Sie diesen benutzerdefinierten Inhalt haben möchten, haben Sie zwei Möglichkeiten:
1) Unter dem Produktpreis
Mit dieser benutzerdefinierten Funktion eingehakt woocommerce_before_single_product_summary
Aktionshaken können Sie einige benutzerdefinierte Inhalte hinzufügen zu einer bestimmten Produkt-ID (in der Funktion zu definieren) Hier entlang:
add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
global $product;
// Limit to a specific product ID only (Set your product ID below )
if( $product->get_id() != 37 ) return;
// The content start below (with translatables texts)
?>
<div class="custom-content product-id-<?php echo $product->get_id(); ?>">
<h3><?php _e("My custom content title", "woocommerce"); ?></h3>
<p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
</div>
<?php
// End of content
}
2) Unter dem Produktbild:
Mit dieser benutzerdefinierten Funktion eingehakt woocommerce_before_single_product_summary
Aktionshaken können Sie einige benutzerdefinierte Inhalte hinzufügen zu einer bestimmten Produkt-ID (in der Funktion zu definieren) Hier entlang:
add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_for_specific_product', 25 );
function add_custom_content_for_specific_product() {
global $product;
// Limit to a specific product ID only (Set your product ID below )
if( $product->get_id() != 37 ) return;
// The content start below (with translatables texts)
?>
<div class="custom-content product-id-<?php echo $product->get_id(); ?>">
<h3><?php _e("My custom content title", "woocommerce"); ?></h3>
<p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
</div>
<?php
// End of content
}
Wenn Sie die Produktkurzbeschreibung entfernen möchten, können Sie die Funktion direkt nach der if-Anweisung hinzufügen:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
Code geht in die functions.php-Datei Ihres aktiven Child-Themes (oder Themes) oder auch in jede Plugin-Datei.
Getestet und funktioniert…
-
nur ein kleiner Tippfehler – es sollte die Datei ‘functions.php’ sein, nicht ‘function.php’
– Nat
12. Oktober 2019 um 12:14 Uhr
Bearbeiten Sie einfach das Produkt, um den Inhalt hinzuzufügen. und dieser Inhalt wird auf der Detailseite angezeigt.