Linkwise追踪与重定向集成指导:WooCommerce代码配置及变量补全求助
Hey Mike, let's get that Linkwise addItem tracking code sorted for your WooCommerce product listings. I’ve mapped the WooCommerce product variables to match what Linkwise expects in the addItem call, here’s the complete implementation:
WooCommerce Product List Linkwise
addItem Tracking Code First, we'll hook this code into a WooCommerce action so it runs for every product in your shop/category listings. The woocommerce_after_shop_loop_item action is perfect for this—it triggers right after each product is rendered on archive pages.
Complete Snippet with Valid WooCommerce Variables
add_action('woocommerce_after_shop_loop_item', 'linkwise_product_list_tracking', 10); function linkwise_product_list_tracking() { global $product; // Skip if we don't have a valid product object if (!is_a($product, 'WC_Product')) return; // Pull critical product data from WooCommerce $product_identifier = $product->get_sku() ?: $product->get_id(); // Use SKU first (matches most Linkwise XML feeds), fall back to product ID $product_name = esc_js($product->get_name()); // Sanitize name for safe JS output $product_price = $product->get_price(); // Regular price; use get_sale_price() for sale items $default_quantity = 1; // Default quantity for listing views (users haven't selected quantity yet) // Output the Linkwise addItem script ?> <script> lw("addItem", { id: "<?php echo $product_identifier; ?>", name: "<?php echo $product_name; ?>", price: <?php echo $product_price; ?>, quantity: <?php echo $default_quantity; ?> }); </script> <?php }
Variable Breakdown (Matching Linkwise Requirements)
id: This should align with the product ID/SKU specified in your Linkwise XML feed. I set it to prioritize SKUs (since feeds typically use SKUs for product matching) and fall back to the WooCommerce internal product ID if no SKU exists. Adjust this based on what’s in your Linkwise feed.name: Usesesc_js()to sanitize the product name, preventing JavaScript errors from special characters or quotes in product titles.price: Pulls the product’s regular price. Swapget_price()withget_sale_price()if you want to track the discounted price for sale items instead.quantity: Set to 1 by default for product listings—since users haven’t selected a quantity at this stage, this is the standard default for Linkwise tracking.
Quick Tips
- Ensure you’ve already loaded the base Linkwise tracking script on your site (usually in the header or footer) before adding this snippet—this
addItemcall depends on the core Linkwise script being present. - Test this on a staging site first! Check your browser’s developer console for JS errors, and verify Linkwise is receiving the product data correctly via their debugging tools.
- To target specific categories or exclude products, add conditional checks inside the function (e.g.,
if (has_term('books', 'product_cat', $product->get_id())) { ... }).
内容的提问来源于stack exchange,提问作者mike
相关产品推荐
相关产品推荐

