I am creating an order with php. I have successfully created the order, however, cannot add local picup information.
I am using WooCommerce Local Pickup Plus plugin.
I want to add below fields to the order:
- Shipping method: Local pickup (already added to woocommerce)
- Pickup location: North Miami Beach (already added to woocommerce)
- Pickup Date (user selected)
- Items to Pickup (all items in cart)
My code till now:
<?php
$woocommerce->cart->empty_cart();
$product_id=358;
$quantity=3;
$variationID=766;
$attr_array=array(
'choose-a-flavor' => 'Red Velvet'
);
$address = array(
'first_name' => 'Arka',
'last_name' => 'Debnath',
'company' => 'Arkas',
'email' => '[email protected]',
'phone' => '123-456-780',
'address_1' => '123 Main st.',
'address_2' => '104',
'city' => 'San Diego',
'state' => 'Ca',
'postcode' => '92121',
'country' => 'US'
);
$woocommerce->cart->add_to_cart($product_id, $quantity, $variationID, $attr_array);
$order_data = array(
'status' => apply_filters('woocommerce_default_order_status', 'processing')
);
$new_order = wc_create_order($order_data);
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$item_id = $new_order->add_product(
$values['data'], $values['quantity'], array(
'variation' => $values['variation'],
'totals' => array(
'subtotal' => $values['line_subtotal'],
'subtotal_tax' => $values['line_subtotal_tax'],
'total' => $values['line_total'],
'tax' => $values['line_tax'],
'tax_data' => $values['line_tax_data']
)
)
);
}
$new_order->set_address($address, 'billing');
$new_order->set_address($address, 'shipping');
$new_order->calculate_totals();
// The below part does not work
$woocommerce->shipping->load_shipping_methods();
$shipping_methods = $woocommerce->shipping->get_shipping_methods();
$selected_shipping_method = $shipping_methods['local_pickup'];
$shippingMethod = new \WC_Shipping_Rate($selected_shipping_method->id,$selected_shipping_method->title, 0);
$new_order->add_shipping($shippingMethod);
?>