Woocommerce 在结帐页面下订单后替换购物车中的产品
Posted
技术标签:
【中文标题】Woocommerce 在结帐页面下订单后替换购物车中的产品【英文标题】:Woocommerce Replace Product in Cart Upon Place Order in Checkout Page 【发布时间】:2016-09-04 23:09:35 【问题描述】:我一直在网络上搜索、阅读文档和资料,但我无法弄清楚如何在结帐页面中更换产品。
为了您的信息,我的主要产品页面位于主页中,并且已选择的每个产品都将重定向到结帐页面。现在这里,有一个问题。让我解释一下……
您看,我在结帐页面中有一个轮播滑块,用户可以在付款前更改/更换他们的产品(已经添加到他们的购物车中)。
form-checkout.php
global $woocommerce;
global $product;
$items = $woocommerce->cart->get_cart();
foreach ($items as &$item)
$id = $item['product_id'];
echo $id;
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="carousel-inner" role="listbox">
<?php
// Querying of product information retrieval
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' =>'ASC');
$loop = new WP_Query( $args );
// Display each retrieved product
while ( $loop->have_posts() ) :
$loop->the_post();
// WooCommerce global product variable. Refer: https://docs.woothemes.com/document/class-reference/
global $product;
global $woocommerce;
?>
<div class="item <?php if ($product->id == $id) ?> active <?php ?>">
<div class="p-big" id="p-custom-color">
<strong><?php the_title(); ?></strong>
</div>
<div class="p-light-black">CANDIDATES</div>
<input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">
</div>
<?php
endwhile;
wp_reset_query(); // After the loop ended, quit the custom loop and reset back the main loop
?>
</div>
</div>
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order']))
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="" enctype="multipart/form-data">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<?php do_action( 'woocommerce_checkout_billing' ); ?>
<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
<?php endif; ?>
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>
<div id="order_review" class="woocommerce-checkout-review-order">
<!-- Checkout Review -->
<input type="hidden" id="selectedproduct" name="selectedproductid" value="">
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</div>
<?php do_action( 'woocommerce_checkout_after_order_review' ); ?>
</form>
如您所见,在轮播中,我已包含 <input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">
以获取每个产品 ID,并使用我的 jQuery(我未在此处显示),我获取了该产品当前在活动幻灯片上的任何产品 ID并在表格中填写<input type="hidden" id="selectedproduct" name="selectedproductid" value="">
。
这样,我可以使用以下代码(位于表单上方)将已添加到购物车的产品替换为基于活动幻灯片的选定/选择的产品:-
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order']))
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
这里的问题是,它未能用当前选择的产品替换旧产品,并且它只是用旧产品重定向到支付网关页面。 p>
我希望它在下订单时用新选择的产品替换产品。可能吗?我希望是这样,因为我已经在 WooCommerce 上玩了几个星期了,我不希望我的努力是徒劳的。帮帮我.....
【问题讨论】:
【参考方案1】:经过几天的研究,经过 30 多个 Chrome 标签页、50 多个购买测试和 10 加仑咖啡,我终于找到了答案……
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission()
if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) )
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
触发此函数所需的钩子位于 includes/class-wc-checkout.php 中的 WC_Checkout
process_checkout()
类中。这个woocommerce_checkout_process
不存在 WooCommerce 模板文件,我们会彻底的。因此,要在提交下订单时将数据发送到支付网关之前执行任何自定义操作,我们需要操作 woocommerce_checkout_process
钩子,因为 process_checkout()
函数在按下确认订单按钮后处理结帐。
希望这可以挽救某人的生命,因为我没有任何生命,因为我在熬夜熬了几天的油后需要睡觉,因为这可憎。
【讨论】:
以上是关于Woocommerce 在结帐页面下订单后替换购物车中的产品的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Woocommerce 购物车页面中添加订单备注字段?