/** * Gets addons assigned to a product by ID * * @param int $post_id ID of the product to get addons for * @param string $prefix for addon field names. Defaults to postid- * @param bool $inc_parent Set to false to not include parent product addons. * @param bool $inc_global Set to false to not include global addons. * @return array array of addons */ function get_product_addons($post_id, $prefix = false, $inc_parent = true, $inc_global = true) { if (!$post_id) { return array(); } $addons = array(); $raw_addons = array(); $product_terms = apply_filters('get_product_addons_product_terms', wp_get_post_terms($post_id, 'product_cat', array('fields' => 'ids')), $post_id); $exclude = get_post_meta($post_id, '_product_addons_exclude_global', true); // Product Parent Level Addons if ($inc_parent && ($parent_id = wp_get_post_parent_id($post_id))) { $raw_addons[10]['parent'] = apply_filters('get_parent_product_addons_fields', get_product_addons($parent_id, $parent_id . '-', false, false), $post_id, $parent_id); } // Product Level Addons $raw_addons[10]['product'] = apply_filters('get_product_addons_fields', array_filter((array) get_post_meta($post_id, '_product_addons', true)), $post_id); // Global level addons (all products) if ('1' !== $exclude && $inc_global) { $args = array('posts_per_page' => -1, 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_key' => '_priority', 'post_type' => 'global_product_addon', 'post_status' => 'publish', 'suppress_filters' => true, 'meta_query' => array(array('key' => '_all_products', 'value' => '1'))); $global_addons = get_posts($args); if ($global_addons) { foreach ($global_addons as $global_addon) { $priority = get_post_meta($global_addon->ID, '_priority', true); $raw_addons[$priority][$global_addon->ID] = apply_filters('get_product_addons_fields', array_filter((array) get_post_meta($global_addon->ID, '_product_addons', true)), $global_addon->ID); } } // Global level addons (categories) if ($product_terms) { $args = apply_filters('get_product_addons_global_query_args', array('posts_per_page' => -1, 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_key' => '_priority', 'post_type' => 'global_product_addon', 'post_status' => 'publish', 'suppress_filters' => true, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $product_terms, 'include_children' => false))), $product_terms); $global_addons = get_posts($args); if ($global_addons) { foreach ($global_addons as $global_addon) { $priority = get_post_meta($global_addon->ID, '_priority', true); $raw_addons[$priority][$global_addon->ID] = apply_filters('get_product_addons_fields', array_filter((array) get_post_meta($global_addon->ID, '_product_addons', true)), $global_addon->ID); } } } } ksort($raw_addons); foreach ($raw_addons as $addon_group) { if ($addon_group) { foreach ($addon_group as $addon) { $addons = array_merge($addons, $addon); } } } // Generate field names with unqiue prefixes if (!$prefix) { $prefix = apply_filters('product_addons_field_prefix', "{$post_id}-", $post_id); } foreach ($addons as $addon_key => $addon) { if (empty($addon['name'])) { unset($addons[$addon_key]); continue; } if (empty($addons[$addon_key]['field-name'])) { $addons[$addon_key]['field-name'] = sanitize_title($prefix . $addon['name']); } } return apply_filters('get_product_addons', $addons); }
/** * Adds Product Addons to the Line Item XML if available * * REQUIRES v2.0+ of XML Export; use `wc_customer_order_xml_export_suite_order_export_line_item_format` filter for earlier versions * * @param array $item_format line item XML data to write * @param \WC_Order $order * @param array $item the line item order data * @return array - modified line item XML data to write */ function sv_wc_xml_export_line_item_addons($item_format, $order, $item) { $product = $order->get_product_from_item($item); // bail if this line item isn't a product if (!($product && $product->exists())) { return $item_format; } // get the possible add-ons for this line item to check if they're in the order $addons = get_product_addons($product->id); $product_addons = sv_wc_xml_export_get_line_item_addons($item, $addons); if (!empty($product_addons)) { $item_format['AddOn'] = $product_addons; } return $item_format; }
public static function add_to_qlist() { global $dvin_qlist_products, $post_id; //if form serialized data exists, merge with POST vars if (isset($_POST['form_serialize_data'])) { parse_str($_POST['form_serialize_data'], $arr); $_POST = array_merge($_POST, $arr); //if addons extensions active if (class_exists('Product_Addon_Cart')) { $addons = get_product_addons($_POST['product_id']); //initialize the variables $addons_result_arr = $arr = array(); $obj = new Product_Addon_Cart(); $addons_result_arr = $obj->add_cart_item_data($arr, $_POST['product_id']); $_POST['addons'] = $addons_result_arr['addons']; } //check if it is a simple product if (!isset($_POST['variable_id']) && isset($_POST['add-to-cart'])) { $_POST['product_id'] = $_POST['add-to-cart']; } } $dvin_wcql_obj = new Dvin_Wcql($_POST); //create object //add to quotelist $ret_val = $dvin_wcql_obj->add(); if ($ret_val == "true") { $_POST['count_refresh'] = isset($_POST['count_refresh']) ? $_POST['count_refresh'] : 'false'; $_POST['widget_refresh'] = isset($_POST['widget_refresh']) ? $_POST['widget_refresh'] : 'false'; $data = self::get_refreshed_fragments(array('count_refresh' => $_POST['count_refresh'], 'widget_refresh' => $_POST['widget_refresh'])); $data['status'] = 'success'; $data['msg'] = __("Successfully added", "dvinwcql"); } elseif ($ret_val == "exists") { $data = self::get_refreshed_fragments(array('count_refresh' => "false", 'widget_refresh' => "false")); $data['status'] = 'exists'; $data['msg'] = __("Already Exists. Browse Quotelist.", "dvinwcql"); } elseif (count($dvin_wcql_obj->errors_arr) > 0) { $data = self::get_refreshed_fragments(array('count_refresh' => "false", 'widget_refresh' => "false")); $data['status'] = 'fail'; $data['msg'] = $dvin_wcql_obj->errors_arr[0]; } wp_send_json($data); exit; }
/** * check_required_addons function. * * @access private * @param mixed $product_id * @return void */ private function check_required_addons($product_id) { $addons = get_product_addons($product_id); if ($addons && !empty($addons)) { foreach ($addons as $addon) { if ('1' == $addon['required']) { return true; } } } return false; }
/** * Re-order */ public function re_add_cart_item_data($cart_item_meta, $product, $order) { global $woocommerce; // Disable validation remove_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_cart_item'), 10, 3); // Get addon data $product_addons = get_product_addons($product['product_id']); if (empty($cart_item_meta['addons'])) { $cart_item_meta['addons'] = array(); } if (is_array($product_addons) && !empty($product_addons)) { include_once 'fields/abstract-class-product-addon-field.php'; foreach ($product_addons as $addon) { $value = ''; switch ($addon['type']) { case 'checkbox': case 'radiobutton': include_once 'fields/class-product-addon-field-list.php'; $value = array(); foreach ($product['item_meta'] as $key => $meta) { if (stripos($key, $addon['name']) === 0) { if (1 < count($meta)) { $value[] = array_map('sanitize_title', $meta); } else { $value[] = sanitize_title($meta[0]); } } } if (empty($value)) { continue; } $field = new Product_Addon_Field_List($addon, $value); break; case 'select': include_once 'fields/class-product-addon-field-select.php'; $value = ''; foreach ($product['item_meta'] as $key => $meta) { if (stripos($key, $addon['name']) === 0) { $value = sanitize_title($meta[0]); } } if (empty($value)) { continue; } $chosen_option = ''; $loop = 0; foreach ($addon['options'] as $option) { $loop++; if (sanitize_title($option['label']) == $value) { $value = $value . '-' . $loop; break; } } $field = new Product_Addon_Field_Select($addon, $value); break; case 'custom': case 'custom_textarea': case 'custom_price': case 'input_multiplier': include_once 'fields/class-product-addon-field-custom.php'; $value = array(); foreach ($product['item_meta'] as $key => $meta) { foreach ($addon['options'] as $option) { if (stripos($key, $addon['name']) === 0 && stristr($key, $option['label'])) { $value[sanitize_title($option['label'])] = $meta[0]; } } } if (empty($value)) { continue; } $field = new Product_Addon_Field_Custom($addon, $value); break; case 'file_upload': include_once 'fields/class-product-addon-field-file-upload.php'; $value = array(); foreach ($product['item_meta'] as $key => $meta) { foreach ($addon['options'] as $option) { if (stripos($key, $addon['name']) === 0 && stristr($key, $option['label'])) { $value[sanitize_title($option['label'])] = $meta[0]; } } } if (empty($value)) { continue; } $field = new Product_Addon_Field_File_Upload($addon, $value); break; } // make sure a field is set (if not it could be product with no add-ons) if (isset($field)) { $data = $field->get_cart_item_data(); if (is_wp_error($data)) { $this->add_error($data->get_error_message()); } elseif ($data) { // get the post data $post_data = $_POST; $cart_item_meta['addons'] = array_merge($cart_item_meta['addons'], apply_filters('woocommerce_product_addon_reorder_cart_item_data', $data, $addon, $product['product_id'], $post_data)); } } } } return $cart_item_meta; }
/** * Checks if a product has any required addons. * * @param int $product_id id of product to check * @return boolean result */ function has_required_addons($product_id) { if (!function_exists('get_product_addons')) { return false; } $addons = get_product_addons($product_id); if ($addons && !empty($addons)) { foreach ($addons as $addon) { if ('1' == $addon['required']) { return true; } } } return false; }
/** * check_required_addons function. * * @access private * @param mixed $product_id * @return void */ private function check_required_addons($product_id) { $addons = get_product_addons($product_id, false, false, true); // No parent addons, but yes to global if ($addons && !empty($addons)) { foreach ($addons as $addon) { if ('1' == $addon['required']) { return true; } } } return false; }
public function re_add_cart_item_data($cart_item_meta, $product, $order) { global $woocommerce; // Disable validation remove_filter('woocommerce_add_to_cart_validation', array($this, 'validate_add_cart_item'), 10, 3); // Get addon data $product_addons = get_product_addons($product['product_id']); if (empty($cart_item_meta['addons'])) { $cart_item_meta['addons'] = array(); } if (!empty($product_addons) && is_array($product_addons) && sizeof($product_addons) > 0) { foreach ($product_addons as $addon) { $posted = ''; if (empty($addon['field-name'])) { continue; } switch ($addon['type']) { case "checkbox": case "radiobutton": $posted = array(); foreach ($product['item_meta'] as $key => $meta) { if (strpos($key, $addon['name']) === 0) { $posted[] = sanitize_title($meta[0]); } } if (empty($posted)) { continue; } foreach ($addon['options'] as $option) { if (array_search(sanitize_title($option['label']), $posted) !== FALSE) { $cart_item_meta['addons'][] = array('name' => esc_attr($addon['name']), 'value' => esc_attr($option['label']), 'price' => esc_attr($option['price'])); } } break; case "select": foreach ($product['item_meta'] as $key => $meta) { if (strpos($key, $addon['name']) === 0) { $posted = sanitize_title($meta[0]); } } if (empty($posted)) { continue; } $chosen_option = ''; $loop = 0; foreach ($addon['options'] as $option) { $loop++; if (sanitize_title($option['label']) == $posted) { $chosen_option = $option; break; } } if (!$chosen_option) { continue; } $cart_item_meta['addons'][] = array('name' => esc_attr($addon['name']), 'value' => esc_attr($chosen_option['label']), 'price' => esc_attr($chosen_option['price'])); break; case "custom": case "custom_textarea": case "custom_price": case "input_multiplier": // Posted var = label, value = custom foreach ($addon['options'] as $option) { $label = !empty($option['label']) ? trim($option['label']) : trim($addon['name']); foreach ($product['item_meta'] as $key => $meta) { if (strpos($key, $label) === 0) { $posted = $meta[0]; } } if (empty($posted)) { continue; } if ($addon['type'] == "custom_price") { $price = floatval(woocommerce_clean($posted)); if ($price >= 0) { $cart_item_meta['addons'][] = array('name' => esc_attr($label), 'value' => esc_attr(strip_tags($price)), 'price' => esc_attr($price), 'display' => esc_attr(strip_tags(woocommerce_price($price)))); } } elseif ($addon['type'] == "input_multiplier") { $cart_item_meta['addons'][] = array('name' => esc_attr($label), 'value' => esc_attr(woocommerce_clean($posted)), 'price' => esc_attr(woocommerce_clean($posted) * $option['price'])); } elseif ($addon['type'] == "custom_textarea") { $cart_item_meta['addons'][] = array('name' => esc_attr($label), 'value' => esc_attr(wp_kses_post($posted)), 'price' => esc_attr($option['price'])); } else { $cart_item_meta['addons'][] = array('name' => esc_attr($label), 'value' => esc_attr(woocommerce_clean($posted)), 'price' => esc_attr($option['price'])); } } break; case "file_upload": $label = !empty($option['label']) ? trim($option['label']) : trim($addon['name']); foreach ($product['item_meta'] as $key => $meta) { if (strpos($key, $addon['name']) === 0) { $posted = $meta[0]; } } $cart_item_meta['addons'][] = array('name' => esc_attr($label), 'value' => esc_attr($posted), 'display' => esc_attr(basename($posted)), 'price' => esc_attr($option['price'])); break; } } } return $cart_item_meta; }