對於WooCommerce,我正在尋找一種解決方案,以編程方式創建訂單(我的網站只有一個主頁,其中包含一些字段)以訂購。WooCommerce以編程方式創建訂單並重定向到付款
產品添加了複選框後,我想創建訂單並重定向到付款方式。
創建訂單幾乎是用這個答案完成的,但是如何開始付款? Wordpress (Woocommerce extension) - Create new order programatically
對於WooCommerce,我正在尋找一種解決方案,以編程方式創建訂單(我的網站只有一個主頁,其中包含一些字段)以訂購。WooCommerce以編程方式創建訂單並重定向到付款
產品添加了複選框後,我想創建訂單並重定向到付款方式。
創建訂單幾乎是用這個答案完成的,但是如何開始付款? Wordpress (Woocommerce extension) - Create new order programatically
這爲我做:
if (isset($_POST['isOrder']) && $_POST['isOrder'] == 1) {
$address = array(
'first_name' => $_POST['notes']['domain'],
'last_name' => '',
'company' => $_POST['customer']['company'],
'email' => $_POST['customer']['email'],
'phone' => $_POST['customer']['phone'],
'address_1' => $_POST['customer']['address'],
'address_2' => '',
'city' => $_POST['customer']['city'],
'state' => '',
'postcode' => $_POST['customer']['postalcode'],
'country' => 'NL'
);
$order = wc_create_order();
foreach ($_POST['product_order'] as $productId => $productOrdered) :
$order->add_product(get_product($productId), 1);
endforeach;
$order->set_address($address, 'billing');
$order->set_address($address, 'shipping');
$order->calculate_totals();
update_post_meta($order->id, '_payment_method', 'ideal');
update_post_meta($order->id, '_payment_method_title', 'iDeal');
// Store Order ID in session so it can be re-used after payment failure
WC()->session->order_awaiting_payment = $order->id;
// Process Payment
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'ideal' ]->process_payment($order->id);
// Redirect to success/confirmation/payment page
if ($result['result'] == 'success') {
$result = apply_filters('woocommerce_payment_successful_result', $result, $order->id);
wp_redirect($result['redirect']);
exit;
}
}
如果你還包含javascript,那將會很棒。 – Tomasz
您節省了我的2周,900美元,我找不到更多的詞來欣賞! –
絕對奇妙的,偉大的工作! – MrIndomitable
要跳過結帳頁面,您可以過濾添加到購物車的網址。
function so_31787244_redirect_to_checkout($url) {
// Remove default cart message
WC()->clear_messages();
// Redirect to checkout
$url = WC()->cart->get_checkout_url();
return $url;
}
add_filter('add_to_cart_redirect', 'so_31787244_redirect_to_checkout');
您也可以追求一個插件如One Page Checkout
我想跳過購物車和結帳頁面。例如,在我的產品頁面上,人們可以填寫他們的聯繫信息並按下「立即付款」按鈕。之後,需要創建訂單並將其重定向到付款頁面(例如paypal.com)。 – Ronn0
我不確定爲什麼你甚至使用WooCommerce。但是,這就是說,這聽起來非常自定義,也許你可以編寫一個函數,在PayPal驗證訂單時創建訂單。但看起來很多工作沒有多大好處。我認爲你最好在SSL網站上投資SSL證書和優質PayPal插件並接受付款權利。 – helgatheviking
大問題,事實上,我也一直在尋找解決方案。 – MrIndomitable