WordPress购物车短代码失效:页面无内容显示问题求助
Let's break down the issues in your code step by step—since you're new to PHP and WordPress, these are common pitfalls to watch out for:
1. Variable Scope Problem (Critical!)
Your $xml variable is defined outside the print_shopping_cart() function, so the function has no access to it. This will throw a PHP error (like "Undefined variable: xml"), and since WordPress often suppresses errors on live sites, it results in a blank page.
Plus, the $xml you're loading is tied to a single $product_id (from the add-to-cart button), which isn't relevant for the shopping cart—you need to load the user's current cart XML instead, not a single product's data.
2. Shortcode Output Rules Violation
WordPress shortcodes must return content instead of echoing directly (unless you use output buffering). Your original code uses echo everywhere, which can break the page rendering order. Even when you tried switching to $output, you might have missed some edge cases or still had leftover echoes.
3. Undefined Variables & Syntax Errors
- You're echoing
$empty_cart_textoutside theif (!cart_not_empty())block—when the cart is not empty, this variable is undefined, causing an error. - There's a typo in your table header:
<th class"cart_qty_th"is missing an equals sign (class="cart_qty_th"). - You're accessing
$_SESSIONwithout ensuring the session is started. WordPress doesn't start sessions automatically, so this can lead to undefined index errors.
4. Invalid XML Loading Logic
The URL you're using to load XML has incorrect concatenation:
"https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=' . $product_id . '"
This would result in a malformed URL (the PHP concatenation is inside a quoted string, so it won't execute correctly). For the shopping cart, you should load the cart-specific XML endpoint instead of a product-specific one.
Fixed Code Example
Here's a revised version of your print_shopping_cart() function and related code that addresses these issues:
1. Update the Shortcode Handler (plugin.php)
Ensure it returns the output instead of relying on echo:
function show_shopping_cart_handler($atts) { // Start session if not already started if (!session_id()) { session_start(); } return print_shopping_cart($atts); }
2. Revised print_shopping_cart() Function (functions.php)
function print_shopping_cart($atts = []) { $output = ''; // Handle empty cart text if (!cart_not_empty()) { $empty_cart_text = get_option('cart_empty_text', 'Your cart is empty.'); $output .= '<div class="bmt_cart_empty_cart_section">'; if (preg_match("/http/", $empty_cart_text)) { $output .= '<img src="' . esc_url($empty_cart_text) . '" alt="Empty cart" class="empty_cart_image" />'; } else { $output .= esc_html__($empty_cart_text, 'your-text-domain'); } $output .= '</div>'; } // Cart title and icon $title = get_option('cart_title', 'Shopping Cart'); $output .= '<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); $output .= '<img src="' . esc_url($cart_icon_img_src) . '" class="cart_header_image" alt="' . esc_attr__("Cart", "your-text-domain") . '" />'; } if (!empty($title)) { $output .= '<h2>' . esc_html($title) . '</h2>'; } // Cart items table $output .= '<table style="width: 100%;">'; // Check if session exists and has cart data if (isset($_SESSION) && is_array($_SESSION)) { // Load the correct cart XML (adjust the URL to match BMT Micro's cart endpoint) $cart_xml_url = "https://secure.bmtmicro.com/cart?CID=2/WP"; // Add necessary parameters for user's cart $xml = simplexml_load_file($cart_xml_url); if ($xml) { $output .= ' <tr class="cart_item_row"> <th class="cart_item_name_th">' . esc_html__("Product Name", "your-text-domain") . '</th> <th class="cart_qty_th">' . esc_html__("Quantity", "your-text-domain") . '</th> <th class="cart_price_th">' . esc_html__("Price", "your-text-domain") . '</th> <th></th> </tr>'; // Loop through cart products (adjust the XML path to match BMT Micro's structure) foreach ($xml->producttable->row as $product) { $product_name = (string) $product->productname; $output .= ' <tr class="cart_item_thumb"> <td class="cart_item_name_td" style="overflow: hidden;"> <div class="cart_item_info"> <span class="cart_item_name">' . esc_html($product_name) . '</span> <span class="cart_clear_float"></span> </div> </td> </tr>'; } } else { $output .= '<tr><td colspan="4">' . esc_html__("Failed to load cart data.", "your-text-domain") . '</td></tr>'; } } else { $output .= '<tr><td colspan="4">' . esc_html__("No items in cart.", "your-text-domain") . '</td></tr>'; } $output .= '</table></div>'; return $output; }
Additional Tips
- Enable WP_DEBUG: Add
define('WP_DEBUG', true);to yourwp-config.phpfile to see PHP errors directly—this will help you spot issues faster instead of getting blank pages. - Escape Output: Always use
esc_html(),esc_url(),esc_attr()to sanitize content and prevent XSS vulnerabilities (this is a WordPress best practice). - Check BMT Micro's API Docs: Make sure you're using the correct XML endpoint for retrieving the user's cart—your current URL is for a single product, not the cart summary.
内容的提问来源于stack exchange,提问作者Jeff Berlin




