Woocommerce add Local pickup information to order programmatically - php

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' => '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.

Related

PDO is inserting "null" for empty values

I am using PDO for writing to sqlite database.
// $dbo is an instance of PDO
$query = "INSERT INTO items (id, name, rank) VALUES (:id, :name, :rank)";
$statement = $dbo->prepare($query);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => '');
$statement->execute($values);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);
$statement->execute($values);
After executing any of these, I expect "rank" to be an empty string or even a NULL is acceptable.
But what I get in the database is 'null', yes a string 'null' value, not the real NULL.
Could not find any solution after several attempts.
Try sending NULL directly in this line
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);

Store decimal number into MySQL database

Here is the return of AJAX, which display what value I am trying to store into database:
Here is what is really stored into database (58.00 is what I stored):
Here is the structure of table:
Here is the PHP:
$req = $bdd->prepare("INSERT INTO salon_histo(reference, designation, colour, size, type, price, qty, payment, date) VALUES(:reference, :designation, :colour, :size, :type, :price, :qty, :payment, NOW())");
$req->execute(array(
'reference' => $_POST['reference'],
'designation' => $_POST['designation'],
'colour' => $_POST['colour'],
'size' => $_POST['size'],
'type' => $_POST['type'],
'price' => floatval($price[0]),
'qty' => $_POST['soldQty'],
'payment' => $_POST['payment']
));
$req->closeCursor();
echo json_encode($price[0]);
How can MySQL store a data with 2 decimals at 0 when I am trying to store 58,33? I tried in PHP to use floatval and indeed the number becomes 58.
Price appears to be an array:
'price' => floatval($price[0]),
Possibly the decimal part has key 1?
'price' => (float) ($price[0] . '.' . $price[1]),
If that doesn't work, please var_dump($price) and paste me the result.
The other thing to check is wether PHP is formatting those numbers with a comma or not.

How to insert 100000 records in a schema

I'm using Php 5.4, mySql 5.6
I have a schema that I need to fill with 100000s entries in a single query (for testing purpose). I have some problems because the server takes too long to elaborate, so it reaches the time limit for the elaboration. Or it directly fails the call to the database, sending a message of general fatal error without any further explanation.
My question is simple: why? Are there some php parameters that have to be changed?
Moreover, is better to run a single insert more that 1000000 times or a single multi-insert query with 1000000 records?
P.S.: if I cycle 13 times a single instance which inserts 8000 records everything it's ok and it doesn't take so long...
You could do this, for example:
<?php
$x=1;
$col1 = 1;
$col2 = 1;
$col3 = 1;
$quantity = 1000000-1;
$query = "INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES ('$col1', '$col2', '$col3') ";
while ($x <= $quantity)
{
$col1++;
$col2++;
$col3++;
$x++;
$query .= ", ('$col1', '$col2', '$col3') ";
}
echo $query;
?>
Changing quantity to 100-1 generates this:
INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES ('1', '1', '1') ,
('2', '2', '2') , ('3', '3', '3') , ('4', '4', '4') , ('5', '5', '5')
, ('6', '6', '6') , ('7', '7', '7') , ('8', '8', '8') , ('9', '9',
'9') , ('10', '10', '10') , ('11', '11', '11') , ('12', '12', '12') ,
('13', '13', '13') , ('14', '14', '14') , ('15', '15', '15') , ('16',
'16', '16') , ('17', '17', '17') , ('18', '18', '18') , ('19', '19',
'19') , ('20', '20', '20') , ('21', '21', '21') , ('22', '22', '22') ,
('23', '23', '23') , ('24', '24', '24') , ('25', '25', '25') , ('26',
'26', '26') , ('27', '27', '27') , ('28', '28', '28') , ('29', '29',
'29') , ('30', '30', '30') , ('31', '31', '31') , ('32', '32', '32') ,
('33', '33', '33') , ('34', '34', '34') , ('35', '35', '35') , ('36',
'36', '36') , ('37', '37', '37') , ('38', '38', '38') , ('39', '39',
'39') , ('40', '40', '40') , ('41', '41', '41') , ('42', '42', '42') ,
('43', '43', '43') , ('44', '44', '44') , ('45', '45', '45') , ('46',
'46', '46') , ('47', '47', '47') , ('48', '48', '48') , ('49', '49',
'49') , ('50', '50', '50') , ('51', '51', '51') , ('52', '52', '52') ,
('53', '53', '53') , ('54', '54', '54') , ('55', '55', '55') , ('56',
'56', '56') , ('57', '57', '57') , ('58', '58', '58') , ('59', '59',
'59') , ('60', '60', '60') , ('61', '61', '61') , ('62', '62', '62') ,
('63', '63', '63') , ('64', '64', '64') , ('65', '65', '65') , ('66',
'66', '66') , ('67', '67', '67') , ('68', '68', '68') , ('69', '69',
'69') , ('70', '70', '70') , ('71', '71', '71') , ('72', '72', '72') ,
('73', '73', '73') , ('74', '74', '74') , ('75', '75', '75') , ('76',
'76', '76') , ('77', '77', '77') , ('78', '78', '78') , ('79', '79',
'79') , ('80', '80', '80') , ('81', '81', '81') , ('82', '82', '82') ,
('83', '83', '83') , ('84', '84', '84') , ('85', '85', '85') , ('86',
'86', '86') , ('87', '87', '87') , ('88', '88', '88') , ('89', '89',
'89') , ('90', '90', '90') , ('91', '91', '91') , ('92', '92', '92') ,
('93', '93', '93') , ('94', '94', '94') , ('95', '95', '95') , ('96',
'96', '96') , ('97', '97', '97') , ('98', '98', '98') , ('99', '99',
'99') , ('100', '100', '100')
It's not adviced to do so. It's going to take a while to run and I'm not sure if it will work, you can increase the timeout for the page to make sure it runs.
But since it's for testing purposes..
You can change the values of the col variables if you want, but you can't use $var++ on strings.
Is the problem that the test takes longer than the 30 seconds or so that PHP allows before killing the process?
If so, you can set max_execution_time in php.ini.
See
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php.net/manual/en/function.set-time-limit.php
You could try to minimise the traffic between the application server and the SQL engine:
Start by inserting just one record, and then in a PHP loop, double the number of records in each iteration:
First record:
INSERT INTO TABLE mytable (txt) VALUES ('Test');
In a loop:
INSERT INTO TABLE mytable (txt)
SELECT txt FROM mytable;
If you execute this loop 20 times, you'll end up with 220 records, i.e. ~ 1000000
If you have an AUTO_INCREMENT id field, it will of course be filled with unique values. You can also increment another numerical field explicitly, like this:
INSERT INTO TABLE mytable (num) VALUES (1);
In a loop (PHP):
$count = 1;
foreach (range(1, 20) as $i) {
$sql = "INSERT INTO TABLE mytable (num)
SELECT num + $count FROM mytable";
$mysqli->query($sql);
$count += $count;
}
You should insert this one by one for best perfomance and for more clear statement. You should also remove any index or constrains as well as use MyISAM instead of innodb for fastest sql insert. Best is to use .sql file and console input.

How to resolve php eden error

I have just used the simple query to insert record
$q = "INSERT INTO jobform
(`full_name`,
`father_name`,
`date_of_birth`,
`gender`,
`cnic`,
`city`,
`country`,
`region`,
`contact_number`,
`email_address`,
`degree_title`,
`university`,
`gpa`,
`division`,
`banking_experience`,
`current_organization`,
`job_title`,
`non_banking_experience`,
`non_current_organization`,
`non_job_title`)
VALUES (:full_name,
:father_name,
:date_of_birth,
:gender,
:cnic,
:city,
:country,
:region,
:contact_number,
:email_address,
:degree_title,
:university,
:gpa,
:division,
:banking_experience,
:current_organization,
:job_title,
:non_banking_experience,
:non_current_organization,
:non_job_title)";
$bind = array(
":full_name"=>$full_name,
":father_name"=>$father_name,
":date_of_birth"=>$date_of_birth,
":gender"=>$gender,
":cnic"=>$cnic,
":city"=>$city,
":country"=>$country,
":region"=>$region,
":contact_number"=>$contact_number,
":email_address"=>$email_address,
":degree_title"=>$degree_title,
":university"=>$university,
":gpa"=>$gpa,
":division"=>$division,
":banking_experience"=>$banking_experience,
":current_organization"=>$current_organization,
":job_title"=>$job_title,
":non_banking_experience"=>$non_banking_experience,
":non_current_organization"=>$non_current_organization,
":non_job_title"=>$non_job_title
);
$r = $database->query($q,$bind);
and getting this error, there is no documentation for inserting rows too
Fatal error: Uncaught exception 'Eden_Sql_Error' with message 'INSERT INTO jobform (`full_name`, `father_name`, `date_of_birth`, `gender`, `cnic`, `city`, `country`, `region`, `contact_number`, `email_address`, `degree_title`, `university`, `gpa`, `division`, `banking_experience`, `current_organization`, `job_title`, `non_banking_experience`, `non_current_organization`, `non_job_title`) VALUES ('najm', 'zain', '01-01-2000', 'male', '923092032094209', 'karachi', 'Pakistan', 'sindh', '029320329', 'najm#najm.com', 'Bachelor', 'College Of digital science', '16', 'First', '3 Year', 'BAS', 'Account Manager', '2 Year', in /save.php on line 115
You should use $database->insertRow instead.
Try this.
$data = array(
'full_name' => $full_name,
'date_of_birth' => $date_of_birth ,
'gender' => $gender ,
'cnic' => $cnic ,
'city' => $city ,
'country' => $country ,
'region' => $region ,
'contact_number' => $contact_number ,
'email_address' => $email_address ,
'degree_title' => $degree_title ,
'university' => $university ,
'gpa' => $gpa ,
'division' => $division ,
'banking_experience' => $banking_experience ,
'current_organization' => $current_organization ,
'job_title' => $job_title ,
'non_banking_experience' => $non_banking_experience ,
'non_current_organization' => $non_current_organization ,
'non_job_title' => $non_job_title
);
$result = $database->insertRow('jobform', $data);
Please see official documentation

wpdb->query prepare doesn't work

I followed the introduction to use prepare to insert some data to the table.
The code is like this:
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}awpcp_adphotos VALUES
(ad_id, image_name, disabled, isprimary)
(%d, %s, %d,%d)",
$ad_id,
$ad_img,
0,
0));
Also, disabled and is_primary should be Boolean. But I am not sure what the data type in here.
I also tried
$wpdb->insert(
'{$wpdb->prefix}awpcp_adphotos', // Table name
array(
'ad_id' => $ad_id,
'image_name' => $ad_img,
'disabled' => 0,
'is_primary' => 0,
), // Columns
array(
'%s',
'%d',
'%d',
'%d'
) // Explicit formatting
);
But haven't got any luck yet.
Shouldn't it be (?) :
$wpdb->query(
$wpdb->prepare(
"INSERT INTO {$wpdb->prefix}awpcp_adphotos(`ad_id`, `image_name`, `disabled`, `isprimary`)
VALUES(%d, %s, %d,%d)",
$ad_id, $ad_img, 0, 0)
);
I think you wrote the entire thing wrong, maybe by mistake :)
For your edit, if using insert() should be like:
$wpdb->insert($wpdb->prefix.'awpcp_adphotos', array(
'ad_id' => $ad_id,
'image_name' => $image_name,
'disabled' => $disabled,
'isprimary' => $isprimary,
));

Categories