2016-05-10 8 views
3

私はウェブで検索していましたが、ドキュメント&は読んでいますが、Checkoutページで商品を交換することはできません。Woocommerce Checkoutに注文時にカートで商品を交換するページ

私の主な製品ページはホームページにあり、選択された各製品はチェックアウトページにリダイレクトされます。今ここに問題があります。私に説明させてください....

あなたが確認するページにはカルーセルスライダーがあります。ユーザーは支払前に商品を交換/交換することができます。

フォーム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> 

あなたが見ることができるように、カルーセルに、私は、各製品のIDを取得するために<input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">を含め、私のjQueryを使っている(私はここでは示さなかった)、I現在製品がアクティブなスライドにある製品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 
} 

問題: - それにより

、私は(フォーム上にある)これらのコードを使用してアクティブなスライドに基づいて選択/選択された製品にカートに追加された製品を置き換えることができ古い製品を現在選択されている製品およびに置き換えることができませんでした。これは、古い製品の支払いゲートウェイページにリダイレクトされます。

私はそれをに置き換えたいと思っています。を注文すると、新しいものと交換します。出来ますか? WooCommerceで何週間も遊んでいて、無駄な努力をしたくないからです。 30+クロームタブで、これを考え出すの数日後に

答えて

1

.....私の男を助け、購入コーヒーのテスト& 10ガロンの50+は、最終的に私は...

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 
    } 
} 
を答えを見つけました

この機能をトリガするために必要なフックは、 process_checkout()クラス内にあり、には/ class-wc-checkout.phpが含まれています。このwoocommerce_checkout_processは存在しませんWooCommerceのテンプレートファイル、私達は徹底的になるつもりです。したがって、注文発注時に決済ゲートウェイにデータを送信する前に、カスタム注文を行うには、woocommerce_checkout_processフックを操作する必要があります。process_checkout()ファンクションは注文確認ボタンを押した後にチェックアウトを処理します。

この嫌なことを考えて、真夜中のオイルを燃やして数日後に寝る必要があるので、これは誰も救い出せません。

関連する問題