-1

I am creating products using a PHP/HTML form. Here is how it works:

  1. Specify dimensions, quantity, and attributes in the form
  2. Send that info to create a new product on the fly when I hit "Calculate"
  3. 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.

1 Answer 1

0

Based on the code found here you could do calculations on the cart object with WC()->cart->get_cart by doing whatever math you need to do for each product, in your case the custom glass. Get the contents of the cart, calculate your dimensions, and then remove the shipping methods based on your results.

Just add a custom plugin or some lines in your functions.php e.g.

add_filter( 'woocommerce_available_shipping_methods', 'calc_glass_items_and_filter_shipping_methods', 10, 1 );

function calc_glass_items_and_filter_shipping_methods($available_methods){

  // get the items in cart
  $items = WC()->cart->get_cart;

//cycle through items using the product id's to get the dimensions you must have already set in the meta

foreach($items as $cart_key => $item){

  $procuct_id = $item->id;
  $length = get_post_meta($product_id, '_width', true;

  // and on and on ... math math math
  $item_dimensions[] = $your_cart_item_dimensions; // e.g. 'large' or 'small'

}
// Then do some work on the $item_dimensions array
//  math math math

$whatever_you_figure_out = $math_math_math;

if($whatever_you_figure_out == 'large'){
  $carrier = 'fedex'; // this is the one you'd like to remove
    }else{
  $carrier = 'usps'; // or remove this one
    }

$available_methods = wp_filter_object_list( $available_methods, array( 'method_id' => $carrier ), 'NOT' );

return $available_methods;

}

Its especially helpful to debug with xdebug locally, so you can see what the $available_methods array or object actually holds.

Hope that helps.

3
  • Thank you for taking the time to reply. I don't really want to edit the functions.php area. I just need to somehow update the product with the shipping info using the Force Shipping plugin I have already. So it needs to be in the custom calculator form I have on my website in php- I think it has to be something like: update_post_meta( $post_id, 'wfp_ship', $insert_shipping_method_here? ); The syntax is just throwing me... I don't know what to put there. I posted the full plugin code on twowaymirrors.com/experiment-page under my calculator. Commented Dec 10, 2015 at 17:25
  • So I am re-evaluating my plan on how to get this to work... where do I find this functions.php? Is it inside the WooCommerce plugin or is it part of the theme? I'm not sure which one to edit. Commented Dec 11, 2015 at 15:45
  • Modifying the shipping options is done on the cart object. You may be able to copy the template and the checkout template, but it would be easier to add the functions in a custom plugin or your functions.php file. Functions.php is located in your theme folder. You should probably investigate how to make a child theme, that way your custom code does not get overwritten when your theme updates.
    – rex a.k.
    Commented Dec 11, 2015 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.