2016-05-12 98 views
0

我不知道是否有一個鉤子用於在點擊時更改「下單按鈕」行爲。我試圖在下單時更換/更換產品,因爲產品選擇(所有可用產品)也都位於結帳頁面中。針對自定義行爲的Woocommerce鉤子對於下訂單

到目前爲止,我一直試圖操縱woocommerce_checkout_order_review掛鉤&失敗。

add_action('woocommerce_checkout_order_review', 'remove_woocommerce_product'); 
function remove_woocommerce_product(){ 
    if (isset($_POST['woocommerce_checkout_place_order'])){ 

     global $woocommerce; 
     $woocommerce->cart->empty_cart(); // Empty the cart 

     $selectedproduct = $_POST['selectedproductid']; // Get the selected product 

     WC()->cart->add_to_cart($selectedproduct); // Insert the selected product in the the cart 
     return esc_url(wc_get_checkout_url()); // Redirect to Payment Gateway Page 
    } 
} 

上面的掛鉤不會在下訂單時觸發。也許我的代碼有問題,或者我懷疑是錯誤的鉤子應用。有任何想法嗎?

回答

0

沒關係......找到了答案......

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

有關詳細說明,請參見Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

相關問題