【发布时间】:2018-05-24 15:56:29
【问题描述】:
我正在构建一个“添加到购物车”按钮,它将项目(从 XML 代码中提取)放入购物车。按钮的 shortcodes 正在工作并显示按钮,但是当我尝试实现 shortcode function 来显示购物车本身时,页面根本不会呈现按钮或购物车。这是我的一些代码(PHP 和 Wordpress 的新手):
在shortcodes.php 文件中:
add_shortcode('show_cart', 'show_shopping_cart_handler');
在plugin.php主文件中:
function show_shopping_cart_handler($atts) {
return print_shopping_cart($atts);
}
在functions.php 文件中:
// code for pulling the XML, $product_id is attached to each button
$xml = simplexml_load_file("https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=' . $product_id . '") or wp_die("Error.");
function print_shopping_cart() {
// handles the cart name/text
if (!cart_not_empty()) {
$empty_cart_text = get_option('cart_empty_text');
if (!empty($empty_cart_text)) {
echo '<div class="bmt_cart_empty_cart_section">';
if (preg_match("/http/", $empty_cart_text)) {
echo '<img src="' . $empty_cart_text . '" alt="' . $empty_cart_text . '" class="empty_cart_image" />';
} else {
echo __($empty_cart_text);
}
echo '</div>';
}
}
echo $empty_cart_text;
$title = get_option('cart_title');
echo '<div class="shopping_cart">';
if (!get_option('shopping_cart_image_hide')) {
$cart_icon_img_src = CART_URL . "/images/shopping_cart_icon.png";
$cart_icon_img_src = apply_filters('cart_icon_image_src', $cart_icon_img_src);
echo "<img src='" . $cart_icon_img_src . "' class='cart_header_image' value='" . (__("Cart")) . "' alt='" . (__("cart")) . "' />";
}
if (!empty($title)) {
echo '<h2>';
echo $title;
echo '</h2>';
}
echo '<table style="width: 100%;">';
if ($_SESSION && is_array($_SESSION)) {
echo '
<tr class="cart_item_row">
<th class="cart_item_name_th">' . (__("Product Name")) . '</th><th class"cart_qty_th">' . (__("quantity")) . '</th><th class="cart_price_th">' . (__("Price")) . '</th><th></th>
</tr>';
foreach ($xml->producttable->row() as $product) {
// I will pull more product info, but just using name for testing
$product_name = $product->productname.PHP_EOL;
echo '<tr class="cart_item_thumb"><td class="cart_item_name_td" style="overflow: hidden;">';
echo '<div class="cart_item_info">';
echo '<span class="cart_item_name">' . $product_name . '</span>';
echo '<span class="cart_clear_float"></span>';
echo '</div>';
echo '</td>';
}
}
echo "</table></div>";
}
真的是我要问的问题,任何人都可以看到我可能做错或丢失的东西导致什么都没有显示吗?我还尝试将echo 替换为$output 并执行$output .= 'some HTML code'; 之类的操作,然后运行return $output;,但我仍然得到相同的空白页结果。
如果需要,我可以提供更多代码,提前致谢!
【问题讨论】:
-
短代码不应该
echo任何东西,只返回一个字符串。无论如何,你需要调试正在发生的事情,你当前的代码是不可能重现的......见How do I debug a WordPress plugin?