/** * Remove add-ons if _any product_ in the cart is in a given category * Example: remove add-ons if any product is in the "Gift box" category */ function sv_wc_checkout_add_ons_remove_add_ons_for_giftboxes() { // bail if Checkout Add-ons isn't active if (!function_exists('wc_checkout_add_ons')) { return; } // set a flag $cat_check = false; // check each cart item for our category foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $product = $cart_item['data']; $product_in_cat = false; // replace 'gift_box' with your category's slug if (has_term('gift_box', 'product_cat', $product->id)) { // flag the cart as containing this category, we only need one "true" to break $cat_check = true; break; } } // get the position of checkout add-ons so we can remove them properly $position = get_option('wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details'); // if a product in the cart is in our category, remove the add-ons if ($position && $cat_check) { remove_action($position, array(wc_checkout_add_ons()->get_frontend_instance(), 'render_add_ons'), 20); } }
/** * Remove add-ons if _all products_ in the cart are in a given category * * Example: remove add-ons if all products in cart are in the "Gift box" category * (and thus are gift-wrapped already) */ function sv_wc_checkout_add_ons_remove_add_ons_only_giftboxes() { // bail if Checkout Add-ons isn't active if (!function_exists('wc_checkout_add_ons')) { return; } // holds checks for all products in cart to see if they're in our category $category_checks = array(); // check each cart item for our category foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $product = $cart_item['data']; // replace 'gift_box' with your category's slug $product_in_cat = has_term('gift_box', 'product_cat', $product->id) ? true : false; array_push($category_checks, $product_in_cat); } // get the position of checkout add-ons so we can remove them properly $position = get_option('wc_checkout_add_ons_position', 'woocommerce_checkout_after_customer_details'); // if all items are in this category, remove the checkout add-ons if (!in_array(false, $category_checks) && $position) { remove_action($position, array(wc_checkout_add_ons()->get_frontend_instance(), 'render_add_ons'), 20); } }
/** * Adds support for Customer/Order CSV Export by adding data for each * checkout add-on column header * * @since 1.1.0 * @param array $order_data generated order data matching the column keys in the header * @param WC_Order $order order being exported * @param \WC_Customer_Order_CSV_Export_Generator $csv_generator instance * @return array */ public function add_checkout_add_ons_to_csv_export_column_data($order_data, $order, $csv_generator) { $order_add_ons = wc_checkout_add_ons()->get_order_add_ons($order->id); $new_order_data = $add_on_data = array(); foreach (wc_checkout_add_ons()->get_add_ons() as $add_on) { $value = ''; $total = ''; if (isset($order_add_ons[$add_on->id])) { $value = 'file' == $add_on->type ? wp_get_attachment_url($order_add_ons[$add_on->id]['value']) : $add_on->normalize_value($order_add_ons[$add_on->id]['normalized_value'], true); $total = wc_format_decimal($order_add_ons[$add_on->id]['total'], 2); } $add_on_data['checkout_add_on_' . $add_on->id] = $value; $add_on_data['checkout_add_on_total_' . $add_on->id] = $total; } if (isset($csv_generator->order_format) && ('default_one_row_per_item' == $csv_generator->order_format || 'legacy_one_row_per_item' == $csv_generator->order_format)) { foreach ($order_data as $data) { $new_order_data[] = array_merge((array) $data, $add_on_data); } } else { $new_order_data = array_merge($order_data, $add_on_data); } return $new_order_data; }
function send_to_printer2($order_id, $PID = -1, $reqfrom = "woocommerce") { global $wpdb; $order = wc_get_order($order_id); if ($PID == -1 && $reqfrom == "woocommerce") { $PID = $_SESSION['sentimentPID']; } // Update Order So that it is stored in the flatterbox $currentOrders = get_field('order_count', $PID); if (strlen($currentOrders) > 0) { $currentOrders .= ','; } $currentOrders .= $order_id; __update_post_meta($PID, 'order_count', $value = $currentOrders); $orderAddOns = wc_checkout_add_ons()->get_order_add_ons($order_id); $gift = 'No'; $extracards = 0; if ($orderAddOns[2]['value'] == 1) { $gift = 'Yes'; } if ($orderAddOns[3]['value'] == 1) { $extracards = 10; } // Not needed I dont think //print_r($orderAddOns); $order_count = 1; //print_r($order); //print_r($_SESSION); do_action('woocommerce_order_details_after_customer_details', $order); $_customer = new WC_Customer($order_id); $shipping_address = $order->get_formatted_shipping_address(); $name = explode('<br/>', $shipping_address); $name = $name[0]; // echo $shipping_address; // print_r($_customer); $blankcolor = ''; $shipping_method = $order->get_shipping_method(); switch (strtoupper($shipping_method)) { case 'FEDEX EXPRESS SAVER': $shipping_method = 'FDEX_EXPRESSSAVER'; break; case 'FEDEX 2DAY': $shipping_method = 'FDEX_FEDEX2DAY'; break; case 'FEDEX PRIORITY OVERNIGHT': $shipping_method = 'FDEX_PRIORITYOVERNIGHT'; break; default: $shipping_method = 'FDXG_FEDEXGROUND'; break; } $xmlstring = '<?xml version="1.0" encoding="UTF-8"?>'; // Open Order $xmlstring .= ' <order xmlns="http://fbns/">'; if (count($order->get_items()) > 0) { foreach ($order->get_items() as $item) { // Reset Additional Count if ($orderAddOns[3]['value'] == 1) { $extracards = 10; } else { $extracards = 0; } // Add element $_product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); $qty = intval($item["pa_cardquantity"]) + $extracards; //print_r($_product); //print_r($item); //$_item = $order->get_item_meta(784); // 781 784 /* echo '------------'; echo $_item; print_r($_item); echo '------------'; */ // Start Box $xmlstring .= ' <subOrder> <orderId>' . $order_id . '-' . str_pad($order_count, 3, '0', STR_PAD_LEFT) . '</orderId> <orderDate><![CDATA[' . date('n/j/y', strtotime($order->order_date)) . ']]></orderDate> <shipTo><![CDATA[' . $name . ']]></shipTo> <shipAdd1><![CDATA[' . $_customer->get_shipping_address() . ']]></shipAdd1> <shipAdd2><![CDATA[' . $_customer->get_shipping_address_2() . ']]></shipAdd2> <shipCity><![CDATA[' . $_customer->get_shipping_city() . ']]></shipCity> <shipState><![CDATA[' . $_customer->get_shipping_state() . ']]></shipState> <shipZip><![CDATA[' . $_customer->get_shipping_postcode() . ']]></shipZip> <giftWrapped><![CDATA[' . $gift . ']]></giftWrapped>'; /* <specialInstructions><![CDATA['.htmlspecialchars_decode(stripslashes(get_field('special_instructions_to_flatterers', $PID))).']]></specialInstructions> */ $xmlstring .= ' <shipVia><![CDATA[' . $shipping_method . ']]></shipVia> <box> <shipQty>' . $item["qty"] . '</shipQty> <sku><![CDATA[' . $_product->get_sku() . '_' . $qty . ']]></sku> <font><![CDATA[Unknown]]></font> <substrate><![CDATA[Unknown]]></substrate> <boxType><![CDATA[' . $item["pa_boxtype"]; if (strlen($item["pa_boxcolor"]) > 0) { $xmlstring .= '-' . $item["pa_boxcolor"]; } $xmlstring .= ']]></boxType> <cardcolor><![CDATA[' . xmlPrepare($item["pa_cardcolor"]) . ']]></cardcolor>'; $blankcolor = xmlPrepare($item["pa_cardcolor"]); if (get_field('title_card_headline', $PID)) { $xmlstring .= ' <titlecard> <heading><![CDATA[' . xmlPrepare(get_field('title_card_headline', $PID)) . ']]></heading> <to><![CDATA[' . xmlPrepare(get_field('who_is_this_for', $PID)) . ']]></to> <from><![CDATA[' . xmlPrepare(get_field('title_card_name', $PID)) . ']]></from> </titlecard>'; } else { // For the blank parts $xmlstring .= ' <titlecard> <heading><![CDATA[]]></heading> <to><![CDATA[]]></to> <from><![CDATA[]]></from> </titlecard>'; } $sentiment_results = $wpdb->get_results("SELECT * FROM sentiments WHERE approved = 1 AND PID = " . $PID, ARRAY_A); $sentiment_count = $wpdb->num_rows; // Start Cards if ($sentiment_results) { foreach ($sentiment_results as $row) { $xmlstring .= ' <card> <note><![CDATA[' . xmlPrepare(preg_replace('/\\r/', '/R', preg_replace('/\\n(\\s*\\n)+/', '/R', preg_replace('/\\r\\n/', '/R', $row["sentiment_text"])))) . ']]></note> <author><![CDATA[' . xmlPrepare($row["sentiment_name"]) . ']]></author> <image><![CDATA[' . xmlPrepare($item["pa_cardcolor"]) . ']]></image> </card>'; } } // End Card $order_count++; // Increase Count // Extra Cards $addCards = intval($item["pa_cardquantity"]) - $sentiment_count; if ($addCards > 0) { $extracards = $extracards + $addCards; } for ($i = 0; $i < $extracards; $i++) { $xmlstring .= ' <card> <note><![CDATA[]]></note> <author><![CDATA[]]></author> <image><![CDATA[' . $blankcolor . ']]></image> </card>'; } // End Extra Cards $xmlstring .= ' </box> </subOrder>'; } // Close Box } // Close Order $xmlstring .= ' </order>'; // GET DATE $thedate = date("Ymd_Hi"); // Save File $file = $order_id . '_' . $thedate . '.xml'; //$myfile = fopen('orderlist/'.$file, "w"); //echo '<script>window.alert("HELLO -- '.$order_id.' -- '.getcwd().'");</script>'; if ($reqfrom == 'ajaxfunction') { file_put_contents('../../../orderlist/' . $file, $xmlstring); // Staging requires the ../ to be removed } else { file_put_contents('../orderlist/' . $file, $xmlstring); // Staging requires the ../ to be removed } if (false) { // removed as they are presently pulling $response = sftp_printer($file); } //echo '<script>window.alert("HELLO -- '.$order_id.' -- '.$response.'");</script>'; }
/** * Build the formatted_names array for use in displaying on the admin order screen * * @since 1.0 * @param array $items * @param object $order * @return array $items */ public function build_formatted_names($items, $order) { // Bail out if WC 2.2+ if (SV_WC_Plugin_Compatibility::is_wc_version_gte_2_2()) { return $items; } // Bail out if not in admin if (!is_admin()) { return $items; } // bail if not on view order screen if (!isset($GLOBALS['current_screen']) || 'shop_order' != $GLOBALS['current_screen']->id) { return $items; } // Check if any of the fees are checkout add-ons foreach ($items as $key => $item) { if ('fee' == $item['type'] && $item['wc_checkout_add_on_id']) { $add_on = wc_checkout_add_ons()->get_add_on($item['wc_checkout_add_on_id']); if ('file' == $add_on->type) { $label = $add_on->normalize_value($item['wc_checkout_add_on_value'], false); $this->file_labels[] = $label; } else { $label = maybe_unserialize($item['wc_checkout_add_on_label']); } if ($label) { $this->formatted_names[$items[$key]['name']] = $items[$key]['name'] . $this->label_separator . (is_array($label) ? implode(', ', $label) : $label); if ('file' != $add_on->type) { $this->formatted_names[$items[$key]['name']] = esc_html($this->formatted_names[$items[$key]['name']]); } } } } return $items; }
<select name="wc-checkout-add-on-tax_class[<?php echo $index; ?> ]" class="js-wc-checkout-add-on-tax_class"> <?php foreach ($classes_options as $value => $label) { ?> <option value="<?php echo esc_attr($value); ?> " <?php selected(isset($add_on->tax_class) && $add_on->tax_class == $value); ?> ><?php echo esc_html($label); ?> </option> <?php } ?> </select> </td> <td class="js-wc-checkout-add-on-draggable"> <img src="<?php echo wc_checkout_add_ons()->get_plugin_url(); ?> /assets/images/draggable-handle.png" /> </td> </tr>
/** * Render the checkout add-ons editor table * * @since 1.0 */ private function render_editor() { ?> <div class="wc-checkout-add-ons-editor-content"> <table class="widefat wc-checkout-add-ons-editor"> <thead> <tr> <th class="check-column"><input type="checkbox" /></th> <th class="wc-checkout-add-on-name"> <?php _e('Name', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> <abbr class="required" title="required">*</abbr> <img class="help_tip" width="16" height="16" data-tip='<?php _e('Add-on name displayed in the admin and order details, e.g., "Gift Message"', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> ' src="<?php echo WC()->plugin_url(); ?> /assets/images/help.png" /> </th> <th class="wc-checkout-add-on-label"> <?php _e('Checkout Label', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> <img class="help_tip" width="16" height="16" data-tip='<?php _e('Optional descriptive label shown on checkout, e.g., "Add a Gift Message?". This will default to Name if blank.', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> ' src="<?php echo WC()->plugin_url(); ?> /assets/images/help.png" /> </th> <th width="1%" class="wc-checkout-add-on-type"><?php _e('Type', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> </th> <th class="wc-checkout-add-on-options-costs"> <?php _e('Options / Costs', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> <img class="help_tip" width="16" height="16" data-tip='<?php _e('Use Pipe (|) to separate options and surround options with double stars (**) to set as a default. To set a price for an option, append it to the option with an equation mark. Example: Yes=5.00|No', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> ' src="<?php echo WC()->plugin_url(); ?> /assets/images/help.png" /> </th> <th class="wc-checkout-add-on-attributes"><?php _e('Attributes', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> </th> <th class="wc-checkout-add-on-taxes"><?php _e('Taxes', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> </th> <th class="js-wc-checkout-add-on-draggable"></th> </tr> </thead> <tfoot> <tr> <th colspan="3"> <button type="button" class="button button-secondary js-wc-checkout-add-ons-add-new"> + <?php _e('New Add-On', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> </button> <button type="button" class="button button-secondary js-wc-checkout-add-ons-remove"><?php _e('Remove Selected', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> </button> </th> <th colspan="5"><input type="submit" class="button-primary" value="<?php _e('Save Add-Ons', WC_Checkout_Add_Ons::TEXT_DOMAIN); ?> "/></th> </tr> </tfoot> <tbody> <?php $index = 0; foreach (wc_checkout_add_ons()->get_add_ons() as $add_on_id => $add_on) { echo $this->get_row_html($index, $add_on_id, $add_on); $index++; } ?> </tbody> </table> </div> <?php wp_nonce_field(__FILE__); }
/** * Add checkout add-on meta to order row label for display purposes in * my-account/view-order and order emails. * * @since 1.0 * @param array $total_rows * @param object $order * @return array $total_rows */ public function append_order_add_on_fee_meta($total_rows, $order) { foreach ($total_rows as $row_key => $row) { $parts = explode('_', $row_key); $item_type = $parts[0]; $item_id = isset($parts[1]) ? $parts[1] : null; if ('fee' == $item_type) { $add_on_id = wc_get_order_item_meta($item_id, '_wc_checkout_add_on_id'); if ($add_on_id) { $value = wc_get_order_item_meta($item_id, '_wc_checkout_add_on_value'); $label = wc_get_order_item_meta($item_id, '_wc_checkout_add_on_label'); // Get label (link) for file add-on if (!$label) { $add_on = wc_checkout_add_ons()->get_add_on($add_on_id); if ('file' == $add_on->type) { $label = $add_on->normalize_value($value, false); } } if ($label) { $total_rows[$row_key]['label'] .= $this->label_separator . (is_array($label) ? implode(', ', $label) : $label); } } } } return $total_rows; }
/** * Save checkout add-on values * * @since 1.2.0 */ public static function save_order_item_values_ajax() { check_ajax_referer('save-checkout-add-ons', 'security'); if (isset($_POST['order_id']) && isset($_POST['items'])) { $order_id = absint($_POST['order_id']); // Parse the jQuery serialized items $items = array(); parse_str($_POST['items'], $items); // Save order items wc_checkout_add_ons()->save_order_item_values($order_id, $items); } exit; }
function send_to_printer2($order_id) { global $wpdb; $order = wc_get_order($order_id); $orderAddOns = wc_checkout_add_ons()->get_order_add_ons($order_id); $gift = 'No'; $extracards = 0; if ($orderAddOns[2]['value'] == 1) { $gift = 'Yes'; } if ($orderAddOns[3]['value'] == 1) { $extracards = 10; } //print_r($orderAddOns); $order_count = 1; //print_r($order); //print_r($_SESSION); $PID = $_SESSION['sentimentPID']; do_action('woocommerce_order_details_after_customer_details', $order); $_customer = new WC_Customer($order_id); $shipping_address = $order->get_formatted_shipping_address(); $name = explode('<br/>', $shipping_address); $name = $name[0]; // echo $shipping_address; // print_r($_customer); $blankcolor = ''; $xmlstring = '<?xml version="1.0" encoding="UTF-8"?>'; // Open Order $xmlstring .= ' <order>'; if (count($order->get_items()) > 0) { foreach ($order->get_items() as $item) { // Add element $_product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); //print_r($_product); //print_r($item); //$_item = $order->get_item_meta(784); // 781 784 /* echo '------------'; echo $_item; print_r($_item); echo '------------'; */ // Start Box $xmlstring .= ' <orderId>' . $order_id . '-' . str_pad($order_count, 3, '0', STR_PAD_LEFT) . '</orderId> <orderDate><![CDATA[' . date('n/j/y', strtotime($order->order_date)) . ']]></orderDate> <shipTo><![CDATA[' . $name . ']]></shipTo> <shipAdd1><![CDATA[' . $_customer->get_shipping_address() . ']]></shipAdd1> <shipAdd2><![CDATA[' . $_customer->get_shipping_address_2() . ']]></shipAdd2> <shipCity><![CDATA[' . $_customer->get_shipping_city() . ']]></shipCity> <shipState><![CDATA[' . $_customer->get_shipping_state() . ']]></shipState> <shipZip><![CDATA[' . $_customer->get_shipping_postcode() . ']]></shipZip> <giftWrapped><![CDATA[' . $gift . ']]></giftWrapped> <shipVia>NotAvailable</shipVia> <box> <shipQty>' . $item["qty"] . '</shipQty> <sku><![CDATA[' . $_product->get_sku() . '_' . $item["pa_cardquantity"] . ']]></sku> <font><![CDATA[Unknown]]></font> <substrate><![CDATA[Unknown]]></substrate> <boxType><![CDATA[' . $item["pa_boxtype"]; if (strlen($item["pa_boxcolor"]) > 0) { $xmlstring .= '-' . $item["pa_boxcolor"]; } $xmlstring .= ']]></boxType>'; $sentiment_results = $wpdb->get_results("SELECT * FROM sentiments WHERE approved = 1 AND PID = " . $PID, ARRAY_A); // Start Card if ($sentiment_results) { foreach ($sentiment_results as $row) { $xmlstring .= ' <card> <note><![CDATA[' . $row["sentiment_text"] . ']]></note> <author><![CDATA[' . $row["sentiment_name"] . ']]></author> <image><![CDATA[' . $item["pa_cardcolor"] . ']]></image> </card>'; $blankcolor = $item["pa_cardcolor"]; } } // End Card $order_count++; // Increase Count } // Extra Cards for ($i = 0; $i < $extracards; $i++) { $xmlstring .= ' <card> <note><![CDATA[]]></note> <author><![CDATA[]]></author> <image><![CDATA[' . $blankcolor . ']]></image> </card>'; } // End Extra Cards $xmlstring .= ' </box>'; // Close Box } // Close Order $xmlstring .= ' </order>'; // Save File $file = $order_id . '.xml'; file_put_contents('orderlist/' . $file, $xmlstring); $response = sftp_printer($file); //echo '<script>window.alert("HELLO -- '.$order_id.' -- '.$response.'");</script>'; }