如何在WordPress的WooCommerce购物车添加非实际Printful商品?
无需创建WooCommerce实际商品,添加Printful定制商品到购物车的实现方案
1. 处理「加入购物车」提交,添加临时商品到购物车
点击按钮时,通过POST请求传递定制商品的核心数据(标题、价格、缩略图URL、Printful生成的商品ID等)到后端,利用WooCommerce的WC_Cart类直接添加自定义临时商品,无需预先在WooCommerce后台创建实体商品。
后端处理代码(放入主题functions.php或自定义插件)
// 接收并处理添加定制商品的请求 add_action('init', 'add_printful_custom_product_to_cart'); function add_printful_custom_product_to_cart() { if (isset($_POST['add_printful_custom_product']) && $_POST['add_printful_custom_product'] === '1') { // 清洗并获取提交的商品数据 $product_title = sanitize_text_field($_POST['product_title']); $product_price = floatval($_POST['product_price']); $product_thumbnail = esc_url($_POST['product_thumbnail']); $printful_item_id = sanitize_text_field($_POST['printful_item_id']); if (!empty($product_title) && $product_price > 0) { global $woocommerce; // 把定制商品的所有关键数据存入购物车项元数据 $cart_item_data = array( 'printful_custom_product' => true, // 标记为Printful定制商品 'product_title' => $product_title, 'product_price' => $product_price, 'thumbnail' => $product_thumbnail, 'printful_item_id' => $printful_item_id ); // 用虚拟ID(如0)添加到购物车,数量设为1 $woocommerce->cart->add_to_cart(0, 1, 0, array(), $cart_item_data); // 重定向到购物车页面 wp_redirect(wc_get_cart_url()); exit; } } }
前端「加入购物车」按钮表单示例
<form method="post" action=""> <input type="hidden" name="add_printful_custom_product" value="1"> <input type="hidden" name="product_title" value="我的定制纯棉T恤"> <input type="hidden" name="product_price" value="99.9"> <input type="hidden" name="product_thumbnail" value="https://example.com/custom-tshirt-thumb.jpg"> <input type="hidden" name="printful_item_id" value="pf_123456"> <button type="submit">加入购物车</button> </form>
2. 覆盖购物车显示逻辑,渲染定制商品信息
由于使用虚拟ID,WooCommerce默认会显示错误的商品信息,需要通过钩子覆盖购物车项的标题、缩略图、价格渲染逻辑:
// 修改购物车项的标题显示 add_filter('woocommerce_cart_item_name', 'render_printful_custom_product_name', 10, 3); function render_printful_custom_product_name($name, $cart_item, $cart_item_key) { if (isset($cart_item['printful_custom_product']) && $cart_item['printful_custom_product']) { return $cart_item['product_title']; } return $name; } // 修改购物车项的缩略图显示 add_filter('woocommerce_cart_item_thumbnail', 'render_printful_custom_product_thumbnail', 10, 3); function render_printful_custom_product_thumbnail($thumbnail, $cart_item, $cart_item_key) { if (isset($cart_item['printful_custom_product']) && $cart_item['printful_custom_product']) { $thumb_url = $cart_item['thumbnail']; return '<img src="' . esc_url($thumb_url) . '" alt="定制商品缩略图" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">'; } return $thumbnail; } // 修改购物车项的价格显示 add_filter('woocommerce_cart_item_price', 'render_printful_custom_product_price', 10, 3); function render_printful_custom_product_price($price, $cart_item, $cart_item_key) { if (isset($cart_item['printful_custom_product']) && $cart_item['printful_custom_product']) { return wc_price($cart_item['product_price']); } return $price; }
3. 处理结账流程,调用Printful API完成下单
用户结账完成后,触发Printful API下单请求,利用购物车项中存储的printful_item_id关联定制商品:
// 订单完成后调用Printful API创建正式订单 add_action('woocommerce_order_status_completed', 'place_printful_order_after_checkout', 10, 1); function place_printful_order_after_checkout($order_id) { $order = wc_get_order($order_id); foreach ($order->get_items() as $item) { $cart_item_data = $item->get_meta('_cart_item_data'); if (isset($cart_item_data['printful_custom_product']) && $cart_item_data['printful_custom_product']) { $printful_item_id = $cart_item_data['printful_item_id']; // 这里替换为你的Printful API调用逻辑 // 示例(需引入Printful PHP SDK): // $printful = new PrintfulClient('你的Printful API密钥'); // $printful->post('orders', array( // 'recipient' => array( // 'name' => $order->get_billing_full_name(), // 'address1' => $order->get_billing_address_1(), // 'city' => $order->get_billing_city(), // 'state_code' => $order->get_billing_state(), // 'country_code' => $order->get_billing_country(), // 'zip' => $order->get_billing_postcode() // ), // 'items' => array(array( // 'id' => $printful_item_id, // 'quantity' => $item->get_quantity() // )) // )); } } }
关键注意事项
- 所有定制商品数据必须存入
cart_item_data,避免依赖POST数据导致页面刷新后信息丢失。 - 后端要对提交的价格、商品ID等数据做二次校验(比如和Printful API返回的商品信息比对),防止恶意篡改。
- 测试购物车的更新、删除操作,确保临时商品的行为和普通商品一致。
内容的提问来源于stack exchange,提问作者sachin khansali
相关产品推荐
相关产品推荐

