lundi 30 mai 2016

Add Checkboxes To Woocommerce Order Based On Post Name

I am currently working on a site where the user can select a number of checkboxes when on the woo commerce checkout page.

I need these checkout values to be post names from a custom loop so when they select it, the title of the post shows on the order in the dashboard.

So far I have managed to show the posts on the woo commerce page and add the checkboxes but I don't know how to show multiple values on the backend.

Please see code below.

// Add Fields to cart
add_action( 'woocommerce_checkout_after_customer_details', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

global $post;
echo '<div class="row">';
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'monthly-tie-subscription', 'orderby' => 'rand' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product;

echo '<div class="two">';

woocommerce_show_product_sale_flash( $post, $product );

if (has_post_thumbnail( $loop->post->ID )) 
    echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';

woocommerce_form_field( $post->post_name, array (
'type' => 'checkbox',
'value' => $post->post_name
));


echo'<input type="checkbox" value="' . $post->post_name . '" />' . the_title() . '<br />';

echo '<div class="overlay"></div>';

echo '</div>';
endwhile;
    echo '</div>';
}

// Show on backend
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST[$post->post_name]) update_post_meta( $order_id, $post->post_name, esc_attr($_POST[$post->post_name]));
}

Any help would be appreciated.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire