2017-01-24 4 views
1

EDIT: Ich habe meinen Code erheblich vereinfacht, um zu sehen, ob ich HTML tatsächlich erhalten kann, aber kein Glück.Ausgabe Produkt HTML in benutzerdefinierten Shortcode für WooCommerce

Hier ist die vereinfachte Version, entfernen Sie alle meine benutzerdefinierten HTML und nur <div class="test"></div> hinzufügen, um zu sehen, ob ich meinen Shortcode HTML ausgeben kann, was es nicht tut.

//custom shortcodes 
function product_snippet($atts) { 

// Attributes 
$atts = shortcode_atts(
    array(
     'id' => '', 
     'snippet' => '', 
    ), 
    $atts, 
    'product_snippet' 
); 

return $product = wc_get_product($atts['id']); 
return $product_img = $product->get_image(); 
return $product_title = $product->get_title(); 
return $product_link = get_permalink($atts['id']); 
return $children = $product->get_children(); 
return $children_array = array(); 
echo "<div class='test'></div>"; 


} 
add_shortcode('product_snippet', 'product_snippet'); 

Dies stoppt die Website vor dem Absturz, aber HTML wird nicht ausgegeben.

ORIGINAL POST: Ich versuche, einen benutzerdefinierten Shortcode zu schreiben, damit mein Client leichter ihre Website bearbeiten kann. Ich habe den folgenden Code zu meiner WordPress functions.php Datei hinzugefügt und ich kann nicht feststellen, warum es nicht funktioniert.

Die Idee besteht darin, die ID eines gruppierten Produkts in den Funktionscode einzugeben, und es wird den Titel, das Bild und alle untergeordneten Produkte des Produkts aufrufen.

function product_snippet($atts) { 
$a = shortcode_atts(array (
     'id', 
     'snippet', 
    ), $atts); 

$product = wc_get_product($a['id']); 
$product_img = $product->get_image(); 
$product_title = $product->get_title(); 
$product_link = get_permalink($a['id']); 
$children = $product->get_children(); 

//initialize array so that child products can be sorted by price. 
$children_array = array(); 

//Assign each child product to its price in array so that child products can be sorted by price in HTML. 
foreach ($children as $key => $value) { 
     $children_array[$value] = wc_get_product($value)->get_price(); 

    }; 
    asort($children_array); 

return '<div class="main_pro"> 

      <div class="left-sd">' . do_shortcode('[product id="' . $a['id'] . '"]') . '</div> 
      <div class="right-sd"> 
      <div class="info"> 
       <h2>'. $product_title . '</h2> 
       <p>' . $a['snippet'] . '&nbsp;<a href="' . $product_link . '">More Info &gt;&gt;</a></p> 
      </div> 
       <ul> 
         ' . foreach ($children_array as $key => $value) { 
          $option = wc_get_product($key); 
           $option_title = $option->post->post_title; 
         . ' 
         <li> 
           <div id="outer-bar"> 
            <div class="bar-l"> 
              ' . $option_title; 
              if ($option->get_attribute('pa_pack-size')) { 
               strval(wc_get_product($key)->get_attribute('pa_pack-size')); 
               .'<br/> 
               <span class="small_pz"> 

                 ' . "$" . strval(round(($option->get_price()/intval($option->get_attribute('pa_pack-size'))), 2)) . " per tray - SAVE $" . strval(round(($product->get_price() * $option->get_price() - $option->get_price()), 2)); . ' 
               </span> 
              ' . } 
            . '</div> 

            <div class="bar-r">' . do_shortcode('[add_to_cart id="' . $key . '"]'); . '</div> 
          </div> 
         </li> 
         ' . } 
       . ' </ul> 
      </div> 
    </div>'; 
}; 
add_shortcode('product_snippet', 'product_snippet'); 

Angenommen, ich meine Öffnung haben und <?php ?> Tags schließen. Ich schreibe nicht viel in PHP, also entschuldige ich mich, wenn mein Code janky ist. Wenn ich dies den Best Practices näher bringen kann (ich glaube nicht, dass meine Verkettung richtig aussieht), bin ich offen für Feedback.

+0

Was wird in dem Moment ausgegeben, wenn Sie den Shortcode in einen Post schreiben? – AceWebDesign

+0

Der Code wie geschrieben gibt einen 500 Fehler zurück. Wenn ich 'return 'div class =" main_pro "...' und den gesamten nachfolgenden HTML-Code entferne, wird beim Eingeben des Kurzwahlcodes in eine Seite nur der nav der Seite geladen. – user3183717

Antwort

1

Es gab eine Reihe von Fehlern im Code. Versuche dies.

function product_snippet($atts) 
{ 
    $a = shortcode_atts(array(
     'id', 
     'snippet', 
    ), $atts); 

    $product = wc_get_product($a['id']); 
    $product_img = $product->get_image(); 
    $product_title = $product->get_title(); 
    $product_link = get_permalink($a['id']); 
    $children = $product->get_children(); 

//initialize array so that child products can be sorted by price. 
    $children_array = array(); 

//Assign each child product to its price in array so that child products can be sorted by price in HTML. 
    foreach ($children as $key => $value) { 
     $children_array[$value] = wc_get_product($value)->get_price(); 

    }; 
    asort($children_array); 


    $string = '<div class="main_pro"> 

      <div class="left-sd">' . do_shortcode('[product id="' . $a['id'] . '"]') . '</div> 
      <div class="right-sd"> 
      <div class="info"> 
       <h2>' . $product_title . '</h2> 
       <p>' . $a['snippet'] . '&nbsp;<a href="' . $product_link . '">More Info &gt;&gt;</a></p> 
      </div> 
       <ul>'; 
    foreach ($children_array as $key => $value) { 
     $option = wc_get_product($key); 
     $option_title = $option->post->post_title; 
     $string .= ' 
         <li> 
           <div id="outer-bar"> 
            <div class="bar-l"> 
              ' . $option_title; 
     if ($option->get_attribute('pa_pack-size')) { 
      strval(wc_get_product($key)->get_attribute('pa_pack-size')); 
      $string .= '<br/> 
               <span class="small_pz"> 

                 ' . "$" . strval(round(($option->get_price()/intval($option->get_attribute('pa_pack-size'))), 2)) . " per tray - SAVE $" . strval(round(($product->get_price() * $option->get_price() - $option->get_price()), 2)) . 
       '</span>'; 
     } 

     $string .= '</div> 

            <div class="bar-r">' . do_shortcode('[add_to_cart id="' . $key . '"]') . '</div> 
          </div> 
         </li> 
         '; 
    } 
    $string .= ' </ul> 
      </div> 
    </div>'; 
    return $string; 
} 


add_shortcode('product_snippet', 'product_snippet'); 
+0

Ich habe nicht überprüft, dass die Funktionen, die Sie verwenden, funktionieren, aber der PHP sollte jetzt ordnungsgemäß verschachteln. – AceWebDesign

+0

Ein schnelles Kopieren/Einfügen gibt mir das Problem, wo nur der Header/NAV meiner WP-Site auftaucht. Ich denke, ich isoliert das Problem: aus welchem ​​Grund auch immer '$ product_img = $ product-> get_image(); $ product_title = $ product-> get_title(); $ product_link = get_permalink ($ a ['id']); $ children = $ product-> get_children(); ' Entweder einige oder alle dieser Variablen verursachen den Absturz. Aber mit deinen Codeänderungen sollte ich in der Lage sein, auf dem richtigen Weg zu sein. Ich werde das aktualisieren, wenn ich die Antwort herausgefunden habe. – user3183717

+0

kann auch $ product_title = $ product-> post-> post_title verwenden; um den Namen – AceWebDesign

1

Ich habe ein wenig Ihren Code geändert, damit es funktioniert. Dies ist nur ein Beispiel, das Sie vervollständigen können. Probieren Sie es aus und lassen Sie mich wissen:

//custom shortcodes 
if(!function_exists('product_snippet')) {  

    function product_snippet($atts) { 

     // Attributes 
     extract(shortcode_atts( 
      array(
       'id' => '', // You will use $id to get the value of this attribute 
       'snippet' => '' // You will use $snippet to get the value of this attribute 
      ), 
      $atts 
     )); 

     // Get an instance of the product object 
     $product = wc_get_product($id); 

     // Displays go here 
     return '<div class="test">'.$product->get_image().'<a href="'.get_permalink($id).'">'.$product->get_title().'</a></div>'; 

    } 

    add_shortcode('product_snippet', 'product_snippet'); 
} 

Sie sollten eine HTML-Ausgabe diesmal bekommen ...

-Code geht in function.php Datei Ihres aktiven Kind Thema (oder Thema) oder auch in jeder Plugin-Datei .

Verwandte Themen