Skip to content

Instantly share code, notes, and snippets.

@mcfarhat
Created October 5, 2016 20:38
Show Gist options
  • Save mcfarhat/457823570afe52d0a5f8061b5979cb22 to your computer and use it in GitHub Desktop.
Save mcfarhat/457823570afe52d0a5f8061b5979cb22 to your computer and use it in GitHub Desktop.
meal options sample code
<?php $item_options = array('Meal1','Meal2','Meal3','Meal4','Meal5','Meal6');
//exit if no user is logged in
if (!is_user_logged_in())
return;
//to display message if a save event took place
$updated = false;
//if this is a save event
if (isset($_GET['item']) && !empty($_GET['item'])){
$item_to_update = $_GET['item'];
foreach ($item_options as $option_name){
if (isset($_GET[$option_name]) && (trim($_GET[$option_name])!='')){
$updated = true;
$meal_name = get_option(strtolower($option_name).'name');
if ($meal_name == ""){
$meal_name = $option_name;
}
wc_update_order_item_meta( $item_to_update, $meal_name, $_GET[$option_name]);
}else{
$meal_name = get_option(strtolower($option_name).'name');
if ($meal_name == ""){
$meal_name = $option_name;
}
wc_delete_order_item_meta( $item_to_update, $meal_name);
}
}
//remove date to preserve order of display in Order page
wc_delete_order_item_meta( $item_to_update, 'Last Updated By User');
//add the date/time of last update
wc_update_order_item_meta( $item_to_update, 'Last Updated By User', date("Y-m-d H:i:s"));
}
if ($updated){
echo "<div id='success_msg'>Changes successfully saved!</div><br/>";
}
$userID = get_current_user_id();
//grab logged in user's subscriptions
$result = WC_Subscriptions_Manager::get_users_subscriptions( $userID );
$subs_found = false;
if (is_array($result)){
if (count($result)>0)
$subs_found = true;
}
$display_header = 1;
foreach ($result as $key => $value) {
//found an active subscription
if ($result[$key]["status"]=="active"){
$subs_found = true;
//print_r($result);
//get corresponding order id
$order_id = $result[$key]["order_id"];
$product_id = $result[$key]["product_id"];
//$product_name = get_the_title($product_id);
//echo $order_id;
//echo $product_name;
$order = new WC_Order($order_id);
$order_item = $order->get_items();
//print_r($order_item);
//print_r ($order->get_item_meta($order_id));
//loop through order items
foreach( $order_item as $item_id => $product ) {
$product_name = $product['name'];
//echo $id;
//echo $product_name;
//print_r($order);
//$customer = new WC_Customer($user_id);
//display header first time only
if ($display_header == 1){
?>
<table class="table">
<?php
//do not display again
$display_header = 0;
}
?>
<tr>
<td colspan="2"><?php echo $product_name;
echo "<input type='hidden' id='".$item_id."-product' name=".$item_id."-productname value='".$product_name."'>";
?></td>
</tr>
<?php
foreach ($item_options as $option_name){
$option_name_display = get_option(strtolower($option_name).'name');
if ($option_name_display == ""){
$option_name_display = $option_name;
}
$option_val = wc_get_order_item_meta( $item_id, $option_name_display);
echo "<tr>";
echo "<td>".$option_name_display."</td>";
echo "<td><input type='number' id='".$item_id."-".$option_name."' name=".$item_id."-".$option_name." value='".$option_val."'></td>";
echo "</tr>";
}
?>
<tr>
<td colspan="2">
<input type="button" onclick="save_values(<?php echo $order_id;?>, <?php echo $item_id;?>);" value="Update">
</td>
</tr>
</table>
<?php
//wc_update_order_item_meta( $item_id, "Protein", 9);//, $prev_value );
//only one order per user
break;
}
//only one subscription per user
break;
//print_r ($order->get_item_meta($id));
//print_r($customer);
}
}
//if no subscriptions found, display message and exit
if (!$subs_found){
echo "No active subscriptions found! Please subscribe first.";
return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment