Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakirsajib/7cc3a2463de4f999efa52c82e97092f8 to your computer and use it in GitHub Desktop.
Save zakirsajib/7cc3a2463de4f999efa52c82e97092f8 to your computer and use it in GitHub Desktop.
Shipstation
add_action('gform_after_submission', 'add_product_to_cart_and_checkout', 10, 2);
function add_product_to_cart_and_checkout($entry, $form) {
// Product mapping array
$product_mapping = array(
'Mycotoxin Panel' => 373340
);
// Replace '4' with the ID of your Gravity Form
if ($form['id'] == 30) {
// Get selected product name from dropdown field ID 12
$selected_option = rgar($entry, '12'); // Assuming field ID 12 is the dropdown field
// Extract product name from the selected option (before the pipe character)
$pipe_position = strpos($selected_option, '|');
$product_name = $pipe_position !== false ? substr($selected_option, 0, $pipe_position) : $selected_option;
// Get product ID from product mapping array
$product_id = isset($product_mapping[$product_name]) ? $product_mapping[$product_name] : null;
// Check if product ID is valid
if ($product_id) {
$quantity = rgar($entry, '16');
// Gather customer details
$customer_data = array(
'billing_first_name' => rgar($entry, '4'),
'billing_company' => rgar($entry, '3'),
'billing_email' => rgar($entry, '7'),
'billing_phone' => rgar($entry, '6'),
'billing_address_1' => rgar($entry, '5.1'),
'billing_address_2' => rgar($entry, '5.2'),
'billing_city' => rgar($entry, '5.3'),
'billing_state' => rgar($entry, '5.4'),
'billing_postcode' => rgar($entry, '5.5'),
'billing_country' => rgar($entry, '5.6'),
);
// Create a new order object
$order = wc_create_order();
// Set order properties
$order->set_customer_id(get_current_user_id());
$order->set_billing_email($customer_data['billing_email']);
$order->set_billing_first_name($customer_data['billing_first_name']);
$order->set_billing_last_name(''); // Assuming you don't have a last name field
$order->set_billing_company($customer_data['billing_company']);
$order->set_billing_address_1($customer_data['billing_address_1']);
$order->set_billing_address_2($customer_data['billing_address_2']);
$order->set_billing_city($customer_data['billing_city']);
$order->set_billing_state($customer_data['billing_state']);
$order->set_billing_postcode($customer_data['billing_postcode']);
$order->set_billing_country($customer_data['billing_country']);
$order->set_billing_phone($customer_data['billing_phone']);
$order->update_meta_data('_shipping_first_name', $customer_data['billing_first_name']);
$order->update_meta_data('_shipping_last_name', ''); // Assuming you don't have a last name field
$order->update_meta_data('_shipping_company', $customer_data['billing_company']);
$order->update_meta_data('_shipping_address_1', $customer_data['billing_address_1']);
$order->update_meta_data('_shipping_address_2', $customer_data['billing_address_2']);
$order->update_meta_data('_shipping_city', $customer_data['billing_city']);
$order->update_meta_data('_shipping_state', $customer_data['billing_state']);
$order->update_meta_data('_shipping_postcode', $customer_data['billing_postcode']);
$order->update_meta_data('_shipping_country', $customer_data['billing_country']);
$order->update_meta_data('_shipping_phone', $customer_data['billing_phone']);
// Add products to the order
$order->add_product(wc_get_product($product_id), $quantity);
// Calculate totals
$order->calculate_totals();
// Save the order
$order->save();
// Set the order status to a value that the ShipStation plugin recognizes as needing to be synced
$order->update_status('processing'); // Replace 'processing' with the appropriate status
// Trigger order status change action
do_action('woocommerce_order_status_changed', $order->get_id(), 'pending', 'processing');
// Trigger order creation action
do_action('woocommerce_new_order', $order->get_id(), $order);
// Optionally, you can send a confirmation email to the customer
//$order->send_order_confirmation_email();
} else {
// Debug: Output error message
error_log('Invalid product name.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment