Woocommerce 在结帐页面下订单时替换购物车中的产品 [英] Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

查看:33
本文介绍了Woocommerce 在结帐页面下订单时替换购物车中的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索,阅读文档和东西,但我不知道如何更换结帐页面中的产品.

供您参考,我的主要产品页面位于主页中,每个已选择的产品都将重定向到结帐页面.现在这里有一个问题.让我解释一下....

您看,我在结帐页面中有一个轮播滑块,用户可以在付款前更改/替换他们的产品(已添加到购物车中).

form-checkout.php

全球$woocommerce;全球$产品;$items = $woocommerce->cart->get_cart();foreach ($items as &$ite​​m){$id = $item['product_id'];}回声 $id;<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false"><div class="carousel-inner" role="listbox"><?php//查询商品信息检索$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' => 'ASC');$loop = new WP_Query( $args );//显示每个检索到的产品while ( $loop->have_posts() ) :$loop->the_post();//WooCommerce 全局产品变量.参考:https://docs.woothemes.com/document/class-reference/全球$产品;全球 $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 class="p-light-black">候选人</div><input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">

<?php终了;wp_reset_query();//循环结束后,退出自定义循环并重置主循环?>

<!-- 表单提交时-->如果 (isset($_POST['woocommerce_checkout_place_order'])){全球 $woocommerce;$woocommerce->cart->empty_cart();//清空购物车$selectedproduct = $_POST['selectedproductid'];//获取选中的商品do_shortcode('[add_to_cart id="' . $selectedproduct . '"]');//将所选产品插入购物车返回 esc_url( wc_get_checkout_url() );//重定向到支付网关页面}<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('您的订单', 'woocommerce');?></h3><?php do_action('woocommerce_checkout_before_order_review');?><div id="order_review" class="woocommerce-checkout-review-order"><!-- 结帐评论--><input type="hidden" id="selectedproduct" name="selectedproductid" value=""><?php do_action('woocommerce_checkout_order_review');?>

<?php do_action('woocommerce_checkout_after_order_review');?></表单>

如您所见,在轮播中,我包含了 <input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>"> 获取每个产品 ID 并使用我的 jQuery(我没有在此处显示),我获取了该产品当前在活动幻灯片上的任何产品 ID,并将其填入 表单中.

这样,我可以使用这些代码(位于表单上方)根据活动幻灯片将已添加到购物车的产品替换为选定/选择的产品:-

如果 (isset($_POST['woocommerce_checkout_place_order'])){全球 $woocommerce;$woocommerce->cart->empty_cart();//清空购物车$selectedproduct = $_POST['selectedproductid'];//获取选中的商品do_shortcode('[add_to_cart id="' . $selectedproduct . '"]');//将所选产品插入购物车返回 esc_url( wc_get_checkout_url() );//重定向到支付网关页面}

这里的问题是,它无法用当前选择的产品替换旧产品,并且它只是重定向到带有旧产品的支付网关页面.>

我希望它在下订单时用新选择的产品替换产品.是否可以?我希望是这样,因为我已经使用 WooCommerce 几个星期了,我不希望我的努力是徒劳的.帮帮我吧……

解决方案

经过几天的摸索,有了 30 多个 Chrome 标签,50 多个购买测试 &10加仑咖啡,终于找到答案了...

add_action('woocommerce_checkout_process', 'change_product_upon_submission');函数 change_product_upon_submission() {if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {$selectedproduct = $_POST['selectedproductid'];//获取选中的商品WC()->购物车->empty_cart();//清空购物车WC()->cart->add_to_cart($selectedproduct);//将选中的产品插入购物车}}

触发此函数所需的钩子位于includes/class-wc-checkout.php 中的WC_Checkout process_checkout() 类中.这个 woocommerce_checkout_process 不存在 WooCommerce 模板文件,我们会彻底.因此,要在提交订单时将数据发送到支付网关之前执行任何自定义操作,我们将需要操作 woocommerce_checkout_process 钩子,因为 process_checkout() 函数处理结帐按下确认订单按钮后.

希望这能挽救某人的生命,因为我没有任何生命,因为我需要在几天的夜宵后找到这个可憎的东西睡觉.

I have been searching on the web, reading docs & stuff but I can't figure out on replacing product in Checkout Page.

For your information, my main product page is in the Home Page and each product that have been selected, will redirected to Checkout Page. Now here, there is a problem. Let me explain....

You see, I have a carousel slider in Checkout Page which user can change/replace their product (which already been added into their cart) before they pay.

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>

As you can see, in the carousel, I have included <input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>"> to get each product ID and with my jQuery (I didn't show here), I took any product ID that the product is currently on the active slide and fill it in the <input type="hidden" id="selectedproduct" name="selectedproductid" value=""> in the form.

By that, I can replace the product that have been added to cart with the selected/chosen product based on the active slide with these code (Located above the form):-

<!-- 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
}

The problem here is, it failed to replace the old product with the current chosen product and it just redirect to the payment gateway page with the old product.

I want it to replace the product with the new selected ones upon placing order. Is it possible? I hope it is, because I have been playing with WooCommerce for weeks now and I don't want my effort to be futile. Help me guys.....

解决方案

After few days of figuring this out, with 30+ Chrome tabs, 50+ of purchases test & 10 gallons of coffee, finally I found the answer...

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
     }
}

The hook required to trigger this function is within the WC_Checkout process_checkout() class in includes/class-wc-checkout.php. This woocommerce_checkout_process doesn't exist WooCommerce template files, we're gonna be thorough. So to do whatever custom stuff before sending data to the payment gateway upon place order submission, we're gonna need to manipulate the woocommerce_checkout_process hook as process_checkout() function processes the checkout after the confirm order button is pressed.

Hope this save someone's lives as I don't have any because I need to sleep after few days of burning midnight oil figuring this abomination.

这篇关于Woocommerce 在结帐页面下订单时替换购物车中的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆