简体   繁体   中英

Woocommerce add Local pickup information to order programmatically

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:

  1. Shipping method: Local pickup (already added to woocommerce)
  2. Pickup location: North Miami Beach (already added to woocommerce)
  3. Pickup Date (user selected)
  4. 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'      => 'my.email@mail.com',
    '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);

?>

OK no help sp far. I didn't find and wordpress hook anywhere to do this safely. So I went the bad way this time. Added line items in the database directly.

INSERT INTO `wp_woocommerce_order_items` (`order_item_id`, `order_item_name`, `order_item_type`, `order_id`) values (null, 'Local Pickup', 'shipping', '$OrderID')

INSERT INTO `wp_woocommerce_order_itemmeta` (`meta_id`, `order_item_id`, `meta_key`, `meta_value`) VALUES
(null, $OrdeItemID, 'method_id', 'local_pickup_plus'),
(null, $OrdeItemID, 'instance_id', '0'),
(null, $OrdeItemID, 'cost', '0.00'),
(null, $OrdeItemID, 'total_tax', '0'),
(null, $OrdeItemID, 'taxes', 'a:1:{s:5:\"total\";a:0:{}}'),
(null, $OrdeItemID, '_pickup_location_id', '$PickupPostal'),
(null, $OrdeItemID, '_pickup_location_name', 'PickupName'),
(null, $OrdeItemID, '_pickup_location_address', '$PickupLocationArray'),
(null, $OrdeItemID, '_pickup_location_phone', ''),
(null, $OrdeItemID, '_pickup_date', '$DateOfPickup'),
(null, $OrdeItemID, '_pickup_minimum_hours', '$PickupTimeMin'),
(null, $OrdeItemID, '_pickup_items', '$Pickupitem')

I know this is not good, but I don't have a better choice yet. If someone has a solution I will be happy to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM