I am creating products using a PHP/HTML form. Here is how it works:
- Specify dimensions, quantity, and attributes in the form
- Send that info to create a new product on the fly when I hit "Calculate"
- It then creates an add to cart button with the new product.
What I can't figure out is how to update the shipping I want to use. I want it to have a certain shipping plugin for certain dimensions (i.e. shipping FedEx when the dimensions are smaller than 24"x30", and Table Rate Shipping plugin when the product is larger than that).
I am currently using WooCommerce Force Product Shippings to have it toggle between my two shipping plugins (FedEx and Table Rate Shipping). I just need to figure out how to get the box checked for "FedEx" when it is 24" x 30" or less, and check the box for Table Rate Shipping when the size is too big.
Here is a link to my form, and here is a link to what my product page looks like in WooCommerce. *Notice how the top right shows "Shipping Methods": FedEx and Shipping. Those are what I need to update in the form when someone creates the product and adds it to the cart.
What "meta key" would I use to update that info? Here is my code for the form so far that creates and updates the product:
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => $Description,
'post_parent' => '',
'post_type' => "product",
);
//Create post
$post_id = wp_insert_post( $post, $wp_error );
if ( $post_id ) {
$attach_id = get_post_meta( $product->parent_id, "_thumbnail_id", true );
add_post_meta( $post_id, '_thumbnail_id', $attach_id );
}
wp_set_object_terms( $post_id, 'customer-added', 'product_cat' );
wp_set_object_terms( $post_id, 'simple', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock' );
update_post_meta( $post_id, 'total_sales', '0' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_virtual', 'no' );
update_post_meta( $post_id, '_regular_price', $Price );
update_post_meta( $post_id, '_sale_price', $Price );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', $Weight );
update_post_meta( $post_id, '_length', $Height );
update_post_meta( $post_id, '_width', $Width );
update_post_meta( $post_id, '_height', $Thick );
update_post_meta( $post_id, '_sku', "" );
update_post_meta( $post_id, '_product_attributes', array() );
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', $Price );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
// file paths will be stored in an array keyed off md5(file path)
$downdloadArray = array(
'name' => "Test",
'file' => $uploadDIR['baseurl'] . "/video/" . $video
);
$file_path = md5( $uploadDIR['baseurl'] . "/video/" . $video );
$_file_paths[ $file_path ] = $downdloadArray;
// grant permission to any newly added files on any existing orders for this product
//do_action( 'woocommerce_process_product_file_download_paths', $post_id, 0, $downdloadArray );
update_post_meta( $post_id, '_downloadable_files ', $_file_paths );
update_post_meta( $post_id, '_download_limit', '' );
update_post_meta( $post_id, '_download_expiry', '' );
update_post_meta( $post_id, '_download_type', '' );
update_post_meta( $post_id, '_product_image_gallery', '' );
Update 12/10/2015: Here is the code to the Force Shipping Plugin that uses the "update_post_meta" function wps_ship_meta_box_save($post_id, $post){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if (isset($post->post_type) && $post->post_type == 'revision')
return $post_id;
if(isset($_POST['post_type']))
if ($_POST['post_type'] == 'product'){
$productIds = get_option('woocommerce_product_apply_wfpship');
if(is_array($productIds)){
if (!in_array($post_id, $productIds)){
$productIds[] = $post_id;
update_option('woocommerce_product_apply_wfpship', $productIds);
}
}
$wfp_ship = array();
if(isset($_POST['ship']))
if ($_POST['ship']){
foreach ($_POST['ship'] as $ship)
$wfp_ship[] = $ship;
}
if (count($wfp_ship)){
update_post_meta($post_id, 'wfp_ship', $wfp_ship);
} else {
delete_post_meta($post_id, 'wfp_ship');
}
}
}
What I want to do is have a line of code in my PHP/Html Form that says:
update_post_meta( $post_id, 'wfp_ship', $insert_shipping_method_here? );
I am just not sure what the syntax is to update that. I already have a plugin that can select the shipping method, I just don't know how to write the code to set it when creating the new product.
The link to the custom calculator (my form) now has the complete plugin code for the Forced Shipping underneath it for people here to see.
In the future how would I figure this out? I've heard about Firebug as a debug, but how can I use these things to figure out the syntax to update?
Update 12/10/2015, 4:07pm: As I read more about meta boxes, which is what the Forced shipping plugin has created for my products... I just need to know now how to check the checkbox using the PHP code through my custom calculator form on my experiment-page so that it forces the "fedex" or "shipping" upon product creation.