function as_ajax_add_compare_product() { $product_id = $_REQUEST["product_id"]; $response = array("status" => 0, "message" => "something error"); if (!empty($product_id) && is_numeric($product_id)) { $product = wc_get_product($product_id); if (!empty($product)) { $compare_product = as_get_product_compare(); if (isset($compare_product[$product_id])) { $response["message"] = __("Product is in compare", AS_DOMAIN) . ' | <a target="_blank" href="' . get_page_link(get_option("as-compare-page-id")) . '">' . __("Go to compare", AS_DOMAIN) . '</a>'; } else { $product_attrs = $product->get_attributes(); $new_product_attrs = array(); foreach ($product_attrs as $attribute) { if (empty($attribute['is_visible']) || $attribute['is_taxonomy'] && !taxonomy_exists($attribute['name'])) { continue; } else { if ($attribute['is_taxonomy']) { $values = wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names')); $values = apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } else { // Convert pipes to commas and display values $values = array_map('trim', explode(WC_DELIMITER, $attribute['value'])); $values = apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } $data_attr = array("name" => $attribute["name"], "title" => wc_attribute_label($attribute['name']), "values" => $values); $new_product_attrs[$attribute["name"]] = $data_attr; } } $data_product = array("id" => $product_id, "name" => $product->get_title(), "price" => $product->get_price(), "image" => $product->get_image(), "attributes" => $new_product_attrs); as_save_product_compare($data_product); $response = array("status" => 1, "message" => "Success", "notice" => __("Product is in compare", AS_DOMAIN) . ' | <a target="_blank" href="' . get_page_link(get_option("as-compare-page-id")) . '">' . __("Go to compare", AS_DOMAIN) . '</a>'); } } else { $response["message"] = __("Product does not existed", AS_DOMAIN); } } else { $response["message"] = __("Product does not true", AS_DOMAIN); } wp_send_json($response); }
public static function wc_get_product_terms($object_id, $taxonomy, $fields = 'all') { global $woocommerce; if (self::is_wc_version_gte_2_1()) { return wc_get_product_terms($object_id, $taxonomy, array('fields' => $fields)); } else { return woocommerce_get_product_terms($object_id, $taxonomy, $fields); } }
/** * Returns the product categories as HTML links with adjustable sorting. * * @since 1.0.0 * * @param string $sep * @param string $before * @param string $after * @param string $orderby * @param string $order * * @return string */ public function get_categories($sep = ', ', $before = '', $after = '', $orderby = 'name', $order = 'ASC') { $cats = wc_get_product_terms($this->id, 'product_cat', ['orderby' => $orderby, 'order' => $order, 'fields' => 'ids']); $links = []; foreach ($cats as $k => $v) { if (($cat = get_term((int) $v, 'product_cat')) && is_object($cat)) { $link = get_term_link($cat); $links[] = '<a href="' . esc_url($link) . '" rel="tag">' . esc_html($cat->name) . '</a>'; } } $links = apply_filters("term_links-product_cat", $links); return $before . join($sep, $links) . $after; }
/** * Add single product trailings * * @since 4.0.0 */ private function add_single_product() { $terms = false; if (function_exists('wc_get_product_terms')) { global $post; $terms = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent', 'order' => 'DESC')); } if ($terms) { $main_term = apply_filters('woocommerce_breadcrumb_main_term', $terms[0], $terms); $this->term_ancestors($main_term->term_id, 'product_cat'); $this->_add_item('link_format', $main_term->name, get_term_link($main_term)); } $this->_add_item('target_format', get_the_title($post->ID)); $this->page_title = false; }
function wc_bundles_get_product_terms($product_id, $attribute_name, $args) { if (WC_PB_Core_Compatibility::is_wc_version_gte_2_3()) { return wc_get_product_terms($product_id, $attribute_name, $args); } else { $orderby = wc_attribute_orderby(sanitize_title($attribute_name)); switch ($orderby) { case 'name': $args = array('orderby' => 'name', 'hide_empty' => false, 'menu_order' => false); break; case 'id': $args = array('orderby' => 'id', 'order' => 'ASC', 'menu_order' => false); break; case 'menu_order': $args = array('menu_order' => 'ASC'); break; } $terms = get_terms(sanitize_title($attribute_name), $args); return $terms; } }
function wc_radio_variation_attribute_options($args = array()) { $args = wp_parse_args(apply_filters('woocommerce_radio_variation_attribute_options_args', $args), array('options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '')); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute) . '_' . uniqid(); $id = $args['id'] ? $args['id'] : sanitize_title($attribute) . uniqid(); $class = $args['class']; if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } echo '<ul id="radio_select_' . esc_attr($id) . '" class="' . esc_attr($class) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '">'; if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { echo '<li>'; echo '<input class="radio-option" name="' . esc_attr($name) . '" id="radio_' . esc_attr($id) . '_' . esc_attr($term->slug) . '" type="radio" data-value="' . esc_attr($term->slug) . '" value="' . esc_attr($term->slug) . '" ' . checked(sanitize_title($args['selected']), $term->slug, false) . ' /><label for="radio_' . esc_attr($id) . '_' . esc_attr($term->slug) . '">' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) . '</label>'; echo '</li>'; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false); echo '<li>'; echo '<input class="radio-option" name="' . esc_attr($name) . '" id="radio_' . esc_attr($id) . '_' . esc_attr($option) . '" type="radio" data-value="' . esc_attr($option) . '" value="' . esc_attr($option) . '" ' . $selected . ' /><label for="radio_' . esc_attr($id) . '_' . esc_attr($option) . '">' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</label>'; echo '</li>'; } } } echo '</ul>'; }
} ?> </select> <!-- Creamos una variable global que almacenara el id del select oculto --> <script> var _sl_clousure_type_ = "<?php echo esc_attr( sanitize_title( $name ) ); ?>" </script> <ul id="js-chkbox-type-closeures" data-attribute_name="attribute_<?php echo sanitize_title( $name ); ?>" class="list-inline list-type-closueres hidden-xs"> <?php /* Aquí desplagamos las variaciones del producto */ global $post; $tax_var = "pa_tipo-de-cierre"; //taxonomia tipo de cierre if ( taxonomy_exists( $tax_var ) ) { //si la taxonomia existe $terms = wc_get_product_terms( $post->ID, $tax_var , array( 'fields' => 'all' ) ); foreach ( $terms as $term ) { $image_html = s8_get_taxonomy_image( $term , array(200,210)); $atribute = "attribute_" . sanitize_title( $name ); echo '<li><a href="#" data-attr="'. $term->name .'"><figure>' . $image_html . '</figure><p>'. $term->name . '<span>' . $term->description . '</span></p></a></li>'; } } ?> </ul> <!-- /js-chkbox-type-closeures --> </td> </tr>
function cupid_filter_breadcrumb_items() { $item = array(); $shop_page_id = wc_get_page_id('shop'); if (get_option('page_on_front') != $shop_page_id) { $shop_name = $shop_page_id ? get_the_title($shop_page_id) : ''; if (!is_shop()) { $item[] = '<li typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="' . get_permalink($shop_page_id) . '">' . $shop_name . '</a></li>'; } else { $item['last'] = $shop_name; } } if (is_tax('product_cat') || is_tax('product_tag')) { $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); } elseif (is_product()) { global $post; $terms = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent', 'order' => 'DESC')); $current_term = $terms[0]; } if (!empty($current_term)) { if (is_taxonomy_hierarchical($current_term->taxonomy)) { $item = array_merge($item, cupid_breadcrumb_get_term_parents($current_term->parent, $current_term->taxonomy)); } if (is_tax('product_cat') || is_tax('product_tag')) { $item['last'] = $current_term->name; } else { $item[] = '<li typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="' . get_term_link($current_term->term_id, $current_term->taxonomy) . '">' . $current_term->name . '</a></li>'; } } if (is_product()) { $item['last'] = get_the_title(); } return apply_filters('cupid_filter_breadcrumb_items', $item); }
/** * Return/show product attribute */ public function get_product_attribute($attribute_name, $product) { // first, check the text attributes $attributes = $product->get_attributes(); $attribute_key = @wc_attribute_taxonomy_name($attribute_name); if (array_key_exists(sanitize_title($attribute_name), $attributes)) { $attribute = $product->get_attribute($attribute_name); return $attribute; } elseif (array_key_exists(sanitize_title($attribute_key), $attributes)) { $attribute = $product->get_attribute($attribute_key); return $attribute; } // not a text attribute, try attribute taxonomy $attribute_key = @wc_attribute_taxonomy_name($attribute_name); $product_terms = @wc_get_product_terms($product->id, $attribute_key, array('fields' => 'names')); // check if not empty, then display if (!empty($product_terms)) { $attribute = array_shift($product_terms); return $attribute; } else { // no attribute under this name return false; } }
/** * @deprecated */ function woocommerce_get_product_terms($object_id, $taxonomy, $fields = 'all') { return wc_get_product_terms($object_id, $taxonomy, array('fields' => $fields)); }
/** * Link all variations via ajax function. */ public static function link_all_variations() { if (!defined('WC_MAX_LINKED_VARIATIONS')) { define('WC_MAX_LINKED_VARIATIONS', 49); } check_ajax_referer('link-variations', 'security'); if (!current_user_can('edit_products')) { die(-1); } if (function_exists('set_time_limit') && false === strpos(ini_get('disable_functions'), 'set_time_limit') && !ini_get('safe_mode')) { @set_time_limit(0); } $post_id = intval($_POST['post_id']); if (!$post_id) { die; } $variations = array(); $_product = wc_get_product($post_id, array('product_type' => 'variable')); // Put variation attributes into an array foreach ($_product->get_attributes() as $attribute) { if (!$attribute['is_variation']) { continue; } $attribute_field_name = 'attribute_' . sanitize_title($attribute['name']); if ($attribute['is_taxonomy']) { $options = wc_get_product_terms($post_id, $attribute['name'], array('fields' => 'slugs')); } else { $options = explode(WC_DELIMITER, $attribute['value']); } $options = array_map('trim', $options); $variations[$attribute_field_name] = $options; } // Quit out if none were found if (sizeof($variations) == 0) { die; } // Get existing variations so we don't create duplicates $available_variations = array(); foreach ($_product->get_children() as $child_id) { $child = $_product->get_child($child_id); if (!empty($child->variation_id)) { $available_variations[] = $child->get_variation_attributes(); } } // Created posts will all have the following data $variation_post_data = array('post_title' => 'Product #' . $post_id . ' Variation', 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation'); $variation_ids = array(); $added = 0; $possible_variations = wc_array_cartesian($variations); foreach ($possible_variations as $variation) { // Check if variation already exists if (in_array($variation, $available_variations)) { continue; } $variation_id = wp_insert_post($variation_post_data); $variation_ids[] = $variation_id; foreach ($variation as $key => $value) { update_post_meta($variation_id, $key, $value); } // Save stock status update_post_meta($variation_id, '_stock_status', 'instock'); $added++; do_action('product_variation_linked', $variation_id); if ($added > WC_MAX_LINKED_VARIATIONS) { break; } } delete_transient('wc_product_children_' . $post_id); echo $added; die; }
/** * Output a list of variation attributes for use in the cart forms. * * @param array $args * @since 2.4.0 */ function pedal_dropdown_variation_attribute_options($args = array()) { $args = wp_parse_args($args, array('options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '', 'show_option_none' => __('Choose an option', 'woocommerce'))); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute); $id = $args['id'] ? $args['id'] : sanitize_title($attribute); $class = $args['class']; if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } echo '<select id="' . esc_attr($id) . '" class="' . esc_attr($class) . '" name="' . esc_attr($name) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '">'; if ($args['show_option_none']) { echo '<option value="">' . esc_html($args['show_option_none']) . '</option>'; } if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>'; } } } else { foreach ($options as $option) { ///PEDAL MODIFICATION if ($product->id == 218) { $pedal_append = pedal_function_test_bookings_in_term(esc_attr($option)); $class = 'available'; if (!$pedal_append) { $disabled = 'disabled'; $pedal_append = 'FULLY BOOKED'; $class = 'booked'; } } // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false); echo '<option class="' . $class . '" value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . ' ' . $pedal_append . '</option>'; } } } echo '</select>'; }
function hpy_dropdown_variation_attribute_options($args = array()) { $_pf = new WC_Product_Factory(); $args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args', $args), array('options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '')); $sortby = get_option('hpy_variant_sort'); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute); $id = $args['id'] ? $args['id'] : sanitize_title($attribute); $class = $args['class']; if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } echo '<select id="' . esc_attr($id) . '" class="' . esc_attr($class) . '" name="' . esc_attr($name) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '">'; if ($sortby == 'price-low' || $sortby == 'price-high') { $variantIDs = $product->children['visible']; $variant_list = array(); foreach ($variantIDs as $variants) { $variant_product = $_pf->get_product($variants); $variant_list[] = array($variant_product->get_regular_price() => $variants); } $varcount = count($variant_list); $i = 0; $v = 0; $tmp = array(); while ($i < $varcount) { $keys[] = key($variant_list[$i]); $i++; } if (count(array_unique($keys)) == 1) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { $clearname = str_replace("-", " ", $term->name); echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . apply_filters('woocommerce_variation_option_name', $clearname) . '</option>'; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false); $clearname = str_replace("-", " ", $option); echo '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $clearname)) . '</option>'; } } } else { foreach ($variant_list as $list) { $tmp = array_merge($tmp, $list); } if ($sortby == 'price-low') { ksort($tmp); foreach ($tmp as $variant) { $current_variant = get_post_meta($variant); $option = $current_variant['attribute_' . strtolower($attribute)][0]; if ($v == 0) { $selected = ' selected="selected"'; $v++; } else { $selected = ''; } $clearname = str_replace("-", " ", $option); echo '<option value="' . esc_attr($option) . '" ' . $selected . '>' . ucwords(esc_html(apply_filters('woocommerce_variation_option_name', $clearname))) . '</option>'; } } else { if ($sortby == 'price-high') { krsort($tmp); foreach ($tmp as $variant) { $current_variant = get_post_meta($variant); $option = $current_variant['attribute_' . strtolower($attribute)][0]; if ($v == 0) { $selected = ' selected="selected"'; $v++; } else { $selected = ''; } $clearname = str_replace("-", " ", $option); echo '<option value="' . esc_attr($option) . '" ' . $selected . '>' . ucwords(esc_html(apply_filters('woocommerce_variation_option_name', $clearname))) . '</option>'; } } else { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>'; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false); echo '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>'; } } } } } } else { if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>'; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false); echo '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>'; } } } } echo '</select>'; }
<strong><?php echo wc_attribute_label($name); ?> </strong><br /> <?php $attribute = $attribute_name; $name = 'attribute_' . sanitize_title($attribute); $id = sanitize_title($attribute); if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { echo '<div class="wvdrb-one-third"><input type="radio" value="' . esc_attr($term->slug) . '" ' . checked(sanitize_title($selected), $term->slug, false) . ' id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '"> ' . apply_filters('woocommerce_variation_option_name', $term->name) . '</div><div class="wvdrb-two-thirds"><pre>' . $term->description . '</pre></div><br />'; } } } else { foreach ($options as $key => $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. // Use attribute key to get the variation id from the $available_variations array $var_id = $available_variations[$key]['variation_id']; // Then use the variation_id to get the value from _isa_woo_variation_desc $var_description = get_post_meta($var_id, '_isa_woo_variation_desc', true); $selected = sanitize_title($selected) === $selected ? checked($selected, sanitize_title($option), false) : checked($selected, $option, false); echo '<div class="wvdrb-one-third"><input type="radio" value="' . esc_attr($option) . '" ' . $selected . ' id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '"> ' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</div><div class="wvdrb-two-thirds">' . $var_description . '</div><br />'; }
private function get_product_from_post($post_id) { $woocommerce_ver_below_2_1 = false; if (version_compare(WOOCOMMERCE_VERSION, '2.1', '<')) { $woocommerce_ver_below_2_1 = true; } if ($woocommerce_ver_below_2_1) { $product = new WC_Product_Simple($post_id); } else { $product = new WC_Product($post_id); } //$post_categories = wp_get_post_categories( $post_id ); //$categories = get_the_category(); try { $thumbnail = $product->get_image(); if ($thumbnail) { if (preg_match('/data-lazy-src="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { if (preg_match('/data-lazy-original="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { if (preg_match('/lazy-src="([^\\"]+)"/s', $thumbnail, $match)) { // Animate Lazy Load Wordpress Plugin $thumbnail = $match[1]; } else { if (preg_match('/data-echo="([^\\"]+)"/s', $thumbnail, $match)) { $thumbnail = $match[1]; } else { preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $thumbnail, $result); $thumbnail = array_pop($result); } } } } } } catch (Exception $e) { $err_msg = "exception raised in thumbnails"; self::send_error_report($err_msg); $thumbnail = ''; } // handling scheduled sale price update if (!$woocommerce_ver_below_2_1) { $sale_price_dates_from = get_post_meta($post_id, '_sale_price_dates_from', true); $sale_price_dates_to = get_post_meta($post_id, '_sale_price_dates_to', true); if ($sale_price_dates_from || $sale_price_dates_to) { self::schedule_sale_price_update($post_id, null); } if ($sale_price_dates_from) { self::schedule_sale_price_update($post_id, $sale_price_dates_from); } if ($sale_price_dates_to) { self::schedule_sale_price_update($post_id, $sale_price_dates_to); } } $product_tags = array(); foreach (wp_get_post_terms($post_id, 'product_tag') as $tag) { $product_tags[] = $tag->name; } $product_brands = array(); if (taxonomy_exists('product_brand')) { foreach (wp_get_post_terms($post_id, 'product_brand') as $brand) { $product_brands[] = $brand->name; } } $taxonomies = array(); try { $all_taxonomies = get_option('wcis_taxonomies'); if (is_array($all_taxonomies)) { foreach ($all_taxonomies as $taxonomy) { if (taxonomy_exists($taxonomy) && !array_key_exists($taxonomy, $taxonomies)) { foreach (wp_get_post_terms($post_id, $taxonomy) as $taxonomy_value) { if (!array_key_exists($taxonomy, $taxonomies)) { $taxonomies[$taxonomy] = array(); } $taxonomies[$taxonomy][] = $taxonomy_value->name; } } } } } catch (Exception $e) { } $acf_fields = array(); try { if (class_exists('acf') && function_exists('get_field')) { $all_acf_fields = get_option('wcis_acf_fields'); if (is_array($all_acf_fields)) { foreach ($all_acf_fields as $acf_field_name) { $acf_field_value = get_field($acf_field_name, $post_id); if ($acf_field_value) { $acf_fields[$acf_field_name] = $acf_field_value; } } } } } catch (Exception $e) { } $send_product = array('product_id' => $product->id, 'currency' => get_woocommerce_currency(), 'price' => $product->get_price(), 'url' => get_permalink($product->id), 'thumbnail_url' => $thumbnail, 'action' => 'insert', 'description' => $product->get_post_data()->post_content, 'short_description' => $product->get_post_data()->post_excerpt, 'name' => $product->get_title(), 'sku' => $product->get_sku(), 'categories' => $product->get_categories(), 'tag' => $product_tags, 'store_id' => get_current_blog_id(), 'identifier' => (string) $product->id, 'product_brand' => $product_brands, 'taxonomies' => $taxonomies, 'acf_fields' => $acf_fields, 'sellable' => $product->is_purchasable(), 'visibility' => $product->is_visible(), 'stock_quantity' => $product->get_stock_quantity(), 'is_managing_stock' => $product->managing_stock(), 'is_backorders_allowed' => $product->backorders_allowed(), 'is_purchasable' => $product->is_purchasable(), 'is_in_stock' => $product->is_in_stock(), 'product_status' => get_post_status($post_id)); try { $variable = new WC_Product_Variable($post_id); $variations = $variable->get_available_variations(); $variations_sku = ''; if (!empty($variations)) { foreach ($variations as $variation) { if ($product->get_sku() != $variation['sku']) { $variations_sku .= $variation['sku'] . ' '; } } } $send_product['variations_sku'] = $variations_sku; $all_attributes = $product->get_attributes(); $attributes = array(); if (!empty($all_attributes)) { foreach ($all_attributes as $attr_mame => $value) { if ($all_attributes[$attr_mame]['is_taxonomy']) { if (!$woocommerce_ver_below_2_1) { $attributes[$attr_mame] = wc_get_product_terms($post_id, $attr_mame, array('fields' => 'names')); } else { $attributes[$attr_mame] = woocommerce_get_product_terms($post_id, $attr_mame, 'names'); } } else { $attributes[$attr_mame] = $product->get_attribute($attr_mame); } } } $send_product['attributes'] = $attributes; $send_product['total_variable_stock'] = $variable->get_total_stock(); try { if (version_compare(WOOCOMMERCE_VERSION, '2.2', '>=')) { if (function_exists('wc_get_product')) { $original_product = wc_get_product($product->id); if (is_object($original_product)) { $send_product['visibility'] = $original_product->is_visible(); $send_product['product_type'] = $original_product->product_type; } } } else { if (function_exists('get_product')) { $original_product = get_product($product->id); if (is_object($original_product)) { $send_product['visibility'] = $original_product->is_visible(); $send_product['product_type'] = $original_product->product_type; } } } } catch (Exception $e) { } } catch (Exception $e) { $err_msg = "exception raised in attributes"; self::send_error_report($err_msg); } if (!$woocommerce_ver_below_2_1) { try { $send_product['price_compare_at_price'] = $product->get_regular_price(); $send_product['price_min'] = $variable->get_variation_price('min'); $send_product['price_max'] = $variable->get_variation_price('max'); $send_product['price_min_compare_at_price'] = $variable->get_variation_regular_price('min'); $send_product['price_max_compare_at_price'] = $variable->get_variation_regular_price('max'); } catch (Exception $e) { $send_product['price_compare_at_price'] = null; $send_product['price_min'] = null; $send_product['price_max'] = null; $send_product['price_min_compare_at_price'] = null; $send_product['price_max_compare_at_price'] = null; } } else { $send_product['price_compare_at_price'] = null; $send_product['price_min'] = null; $send_product['price_max'] = null; $send_product['price_min_compare_at_price'] = null; $send_product['price_max_compare_at_price'] = null; } $send_product['description'] = self::content_filter_shortcode_with_content($send_product['description']); $send_product['short_description'] = self::content_filter_shortcode_with_content($send_product['short_description']); $send_product['description'] = self::content_filter_shortcode($send_product['description']); $send_product['short_description'] = self::content_filter_shortcode($send_product['short_description']); try { if (defined('ICL_SITEPRESS_VERSION') && is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php') && function_exists('wpml_get_language_information')) { if (version_compare(ICL_SITEPRESS_VERSION, '3.2', '>=')) { $language_info = apply_filters('wpml_post_language_details', NULL, $post_id); } else { $language_info = wpml_get_language_information($post_id); } if ($language_info && is_array($language_info) && array_key_exists('locale', $language_info)) { // WP_Error could be returned from wpml_get_language_information(...) $send_product['lang'] = $language_info['locale']; } } } catch (Exception $e) { } return $send_product; }
/** * Get attribute options. * * @param int $product_id * @param array $attribute * @return array */ protected function get_attribute_options($product_id, $attribute) { if (isset($attribute['is_taxonomy']) && $attribute['is_taxonomy']) { return wc_get_product_terms($product_id, $attribute['name'], array('fields' => 'names')); } elseif (isset($attribute['value'])) { return array_map('trim', explode('|', $attribute['value'])); } return array(); }
/** * Single post trail * * @param int $post_id * @param string $permalink */ private function add_crumbs_single($post_id = 0, $permalink = '') { if (!$post_id) { global $post; } else { $post = get_post($post_id); } if ('product' === get_post_type($post)) { $this->prepend_shop_page(); if ($terms = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent', 'order' => 'DESC'))) { $main_term = apply_filters('woocommerce_breadcrumb_main_term', $terms[0], $terms); $this->term_ancestors($main_term->term_id, 'product_cat'); $this->add_crumb($main_term->name, get_term_link($main_term)); } } elseif ('post' != get_post_type($post)) { $post_type = get_post_type_object(get_post_type($post)); $this->add_crumb($post_type->labels->singular_name, get_post_type_archive_link(get_post_type($post))); } else { $cat = current(get_the_category($post)); if ($cat) { $this->term_ancestors($cat->term_id, 'post_category'); $this->add_crumb($cat->name, get_term_link($cat)); } } $this->add_crumb(get_the_title($post), $permalink); }
/** * widget function. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget($args, $instance) { global $wp_query, $post; $c = isset($instance['count']) ? $instance['count'] : $this->settings['count']['std']; $h = isset($instance['hierarchical']) ? $instance['hierarchical'] : $this->settings['hierarchical']['std']; $s = isset($instance['show_children_only']) ? $instance['show_children_only'] : $this->settings['show_children_only']['std']; $d = isset($instance['dropdown']) ? $instance['dropdown'] : $this->settings['dropdown']['std']; $o = isset($instance['orderby']) ? $instance['orderby'] : $this->settings['orderby']['std']; $dropdown_args = array('hide_empty' => false); $list_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat', 'hide_empty' => false); // Menu Order $list_args['menu_order'] = false; if ($o == 'order') { $list_args['menu_order'] = 'asc'; } else { $list_args['orderby'] = 'title'; } // Setup Current Category $this->current_cat = false; $this->cat_ancestors = array(); if (is_tax('product_cat')) { $this->current_cat = $wp_query->queried_object; $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat'); } elseif (is_singular('product')) { $product_category = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent')); if ($product_category) { $this->current_cat = end($product_category); $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat'); } } // Show Siblings and Children Only if ($s && $this->current_cat) { // Top level is needed $top_level = get_terms('product_cat', array('fields' => 'ids', 'parent' => 0, 'hierarchical' => true, 'hide_empty' => false)); // Direct children are wanted $direct_children = get_terms('product_cat', array('fields' => 'ids', 'parent' => $this->current_cat->term_id, 'hierarchical' => true, 'hide_empty' => false)); // Gather siblings of ancestors $siblings = array(); if ($this->cat_ancestors) { foreach ($this->cat_ancestors as $ancestor) { $ancestor_siblings = get_terms('product_cat', array('fields' => 'ids', 'parent' => $ancestor, 'hierarchical' => false, 'hide_empty' => false)); $siblings = array_merge($siblings, $ancestor_siblings); } } if ($h) { $include = array_merge($top_level, $this->cat_ancestors, $siblings, $direct_children, array($this->current_cat->term_id)); } else { $include = array_merge($direct_children); } $dropdown_args['include'] = implode(',', $include); $list_args['include'] = implode(',', $include); if (empty($include)) { return; } } elseif ($s) { $dropdown_args['depth'] = 1; $dropdown_args['child_of'] = 0; $dropdown_args['hierarchical'] = 1; $list_args['depth'] = 1; $list_args['child_of'] = 0; $list_args['hierarchical'] = 1; } $this->widget_start($args, $instance); // Dropdown if ($d) { $dropdown_defaults = array('show_counts' => $c, 'hierarchical' => $h, 'show_uncategorized' => 0, 'orderby' => $o, 'selected' => $this->current_cat ? $this->current_cat->slug : ''); $dropdown_args = wp_parse_args($dropdown_args, $dropdown_defaults); // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258 wc_product_dropdown_categories(apply_filters('woocommerce_product_categories_widget_dropdown_args', $dropdown_args)); wc_enqueue_js("\n\t\t\t\tjQuery( '.dropdown_product_cat' ).change( function() {\n\t\t\t\t\tif ( jQuery(this).val() != '' ) {\n\t\t\t\t\t\tvar this_page = '';\n\t\t\t\t\t\tvar home_url = '" . esc_js(home_url('/')) . "';\n\t\t\t\t\t\tif ( home_url.indexOf( '?' ) > 0 ) {\n\t\t\t\t\t\t\tthis_page = home_url + '&product_cat=' + jQuery(this).val();\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tthis_page = home_url + '?product_cat=' + jQuery(this).val();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlocation.href = this_page;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t"); // List } else { include_once WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php'; $list_args['walker'] = new WC_Product_Cat_List_Walker(); $list_args['title_li'] = ''; $list_args['pad_counts'] = 1; $list_args['show_option_none'] = __('No product categories exist.', 'woocommerce'); $list_args['current_category'] = $this->current_cat ? $this->current_cat->term_id : ''; $list_args['current_category_ancestors'] = $this->cat_ancestors; echo '<ul class="product-categories">'; wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $list_args)); echo '</ul>'; } $this->widget_end($args); }
function mk_theme_breadcrumbs() { global $mk_options, $post; $post_id = global_get_post_id(); if ($post_id) { $local_skining = get_post_meta($post_id, '_enable_local_backgrounds', true); $breadcrumb_skin = get_post_meta($post_id, '_breadcrumb_skin', true); if ($local_skining == 'true' && !empty($breadcrumb_skin)) { $breadcrumb_skin_class = $breadcrumb_skin; } else { $breadcrumb_skin_class = $mk_options['breadcrumb_skin']; } } else { $breadcrumb_skin_class = $mk_options['breadcrumb_skin']; } $delimiter = ' / '; echo '<div id="mk-breadcrumbs"><div class="mk-breadcrumbs-inner ' . $breadcrumb_skin_class . '-skin">'; if (!is_front_page()) { echo '<a href="'; echo home_url(); echo '">' . __('Home', 'mk_framework'); echo "</a>" . $delimiter; } if (function_exists('is_woocommerce') && is_woocommerce() && is_archive()) { $shop_page_id = wc_get_page_id('shop'); $shop_page = get_post($shop_page_id); $permalinks = get_option('woocommerce_permalinks'); if ($shop_page_id && $shop_page && get_option('page_on_front') !== $shop_page_id) { echo '<a href="' . get_permalink($shop_page) . '">' . $shop_page->post_title . '</a> '; } } if (is_category() && !is_singular('portfolio')) { $category = get_the_category(); $ID = $category[0]->cat_ID; echo is_wp_error($cat_parents = get_category_parents($ID, TRUE, '', FALSE)) ? '' : '<span>' . $cat_parents . '</span>'; } else { if (is_singular('news')) { echo '<span>' . get_the_title() . '</span>'; } else { if (is_single() && !is_attachment()) { if (get_post_type() == 'product') { if ($terms = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent', 'order' => 'DESC'))) { $main_term = $terms[0]; $ancestors = get_ancestors($main_term->term_id, 'product_cat'); $ancestors = array_reverse($ancestors); foreach ($ancestors as $ancestor) { $ancestor = get_term($ancestor, 'product_cat'); if (!is_wp_error($ancestor) && $ancestor) { echo '<a href="' . get_term_link($ancestor->slug, 'product_cat') . '">' . $ancestor->name . '</a>' . $delimiter; } } echo '<a href="' . get_term_link($main_term->slug, 'product_cat') . '">' . $main_term->name . '</a>' . $delimiter; } echo get_the_title(); } elseif (is_singular('portfolio')) { $portfolio_category = get_the_term_list($post->ID, 'portfolio_category', '', ' / '); if (!empty($portfolio_category)) { echo $portfolio_category . $delimiter; } echo '<span>' . get_the_title() . '</span>'; } elseif (get_post_type() != 'post') { if (function_exists('is_bbpress') && is_bbpress()) { } else { $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; echo '<a href="' . get_post_type_archive_link(get_post_type()) . '">' . $post_type->labels->singular_name . '</a>' . $delimiter; echo get_the_title(); } } else { $cat = current(get_the_category()); echo get_category_parents($cat, true, $delimiter); echo get_the_title(); } } elseif (is_page() && !$post->post_parent) { echo get_the_title(); } elseif (is_page() && $post->post_parent) { $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs as $crumb) { echo $crumb . '' . $delimiter; } echo get_the_title(); } elseif (is_attachment()) { $parent = get_post($post->post_parent); $cat = get_the_category($parent->ID); $cat = $cat[0]; /* admin@innodron.com patch: Fix for Catchable fatal error: Object of class WP_Error could not be converted to string ref: https://wordpress.org/support/topic/catchable-fatal-error-object-of-class-wp_error-could-not-be-converted-to-string-11 */ echo is_wp_error($cat_parents = get_category_parents($cat, TRUE, '' . $delimiter . '')) ? '' : $cat_parents; /* end admin@innodron.com patch */ echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>' . $delimiter; echo get_the_title(); } elseif (is_search()) { echo __('Search results for “', 'mk_framework') . get_search_query() . '”'; } elseif (is_tag()) { echo __('Tag “', 'mk_framework') . single_tag_title('', false) . '”'; } elseif (is_author()) { $userdata = get_userdata(get_the_author_meta('ID')); echo __('Author:', 'mk_framework') . ' ' . $userdata->display_name; } elseif (is_day()) { echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $delimiter; echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $delimiter; echo get_the_time('d'); } elseif (is_month()) { echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $delimiter; echo get_the_time('F'); } elseif (is_year()) { echo get_the_time('Y'); } } } if (get_query_var('paged')) { echo ' (' . __('Page', 'mk_framework') . ' ' . get_query_var('paged') . ')'; } if (is_tax()) { $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); if (function_exists('is_woocommerce') && is_woocommerce() && is_archive()) { echo $delimiter; } echo '<span>' . $term->name . '</span>'; } if (function_exists('is_bbpress') && is_bbpress()) { $item = array(); $post_type_object = get_post_type_object(bbp_get_forum_post_type()); if (!empty($post_type_object->has_archive) && !bbp_is_forum_archive()) { $item[] = '<a href="' . get_post_type_archive_link(bbp_get_forum_post_type()) . '">' . bbp_get_forum_archive_title() . '</a>'; } if (bbp_is_forum_archive()) { $item[] = bbp_get_forum_archive_title(); } elseif (bbp_is_topic_archive()) { $item[] = bbp_get_topic_archive_title(); } elseif (bbp_is_single_view()) { $item[] = bbp_get_view_title(); } elseif (bbp_is_single_topic()) { $topic_id = get_queried_object_id(); $item = array_merge($item, mk_breadcrumbs_get_parents(bbp_get_topic_forum_id($topic_id))); if (bbp_is_topic_split() || bbp_is_topic_merge() || bbp_is_topic_edit()) { $item[] = '<a href="' . bbp_get_topic_permalink($topic_id) . '">' . bbp_get_topic_title($topic_id) . '</a>'; } else { $item[] = bbp_get_topic_title($topic_id); } if (bbp_is_topic_split()) { $item[] = __('Split', 'mk_framework'); } elseif (bbp_is_topic_merge()) { $item[] = __('Merge', 'mk_framework'); } elseif (bbp_is_topic_edit()) { $item[] = __('Edit', 'mk_framework'); } } elseif (bbp_is_single_reply()) { $reply_id = get_queried_object_id(); $item = array_merge($item, mk_breadcrumbs_get_parents(bbp_get_reply_topic_id($reply_id))); if (!bbp_is_reply_edit()) { $item[] = bbp_get_reply_title($reply_id); } else { $item[] = '<a href="' . bbp_get_reply_url($reply_id) . '">' . bbp_get_reply_title($reply_id) . '</a>'; $item[] = __('Edit', 'mk_framework'); } } elseif (bbp_is_single_forum()) { $forum_id = get_queried_object_id(); $forum_parent_id = bbp_get_forum_parent_id($forum_id); if (0 !== $forum_parent_id) { $item = array_merge($item, mk_breadcrumbs_get_parents($forum_parent_id)); } $item[] = bbp_get_forum_title($forum_id); } elseif (bbp_is_single_user() || bbp_is_single_user_edit()) { if (bbp_is_single_user_edit()) { $item[] = '<a href="' . bbp_get_user_profile_url() . '">' . bbp_get_displayed_user_field('display_name') . '</a>'; $item[] = __('Edit', 'mk_framework'); } else { $item[] = bbp_get_displayed_user_field('display_name'); } } echo implode($delimiter, $item); } echo "</div></div>"; }
function extract_usable_attributes($product) { $attributes = $product->get_attributes(); $usable_attributes = array(); foreach ($attributes as $attribute) { if ($attribute["is_visible"] && $attribute["is_variation"]) { if ($attribute["is_taxonomy"]) { $values = wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names')); $taxonomy = get_taxonomy($attribute["name"]); $usable_attributes[$attribute["name"]] = array("label" => $taxonomy->labels->name, "values" => $values); //$values; // var_dump($values); } else { // Convert pipes to commas and display values $values = array_map('trim', explode(WC_DELIMITER, $attribute['value'])); $usable_attributes[$attribute["name"]] = array("label" => $attribute["name"], "values" => $values); } } } return $usable_attributes; }
/** * widget function. * * @see WP_Widget * @access public * @param array $args * @param array $instance * @return void */ public function widget($args, $instance) { extract($args); global $wp_query, $post, $woocommerce; $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $c = isset($instance['count']) && $instance['count'] ? '1' : '0'; $h = $instance['hierarchical'] ? true : false; $s = isset($instance['show_children_only']) && $instance['show_children_only'] ? '1' : '0'; $d = isset($instance['dropdown']) && $instance['dropdown'] ? '1' : '0'; $o = $instance['orderby'] ? $instance['orderby'] : 'order'; echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } $dropdown_args = array(); $cat_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat'); // Menu Order $cat_args['menu_order'] = false; if ($o == 'order') { $cat_args['menu_order'] = 'asc'; } else { $cat_args['orderby'] = 'title'; } // Setup Current Category $this->current_cat = false; $this->cat_ancestors = array(); if (is_tax('product_cat')) { $this->current_cat = $wp_query->queried_object; $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat'); } elseif (is_singular('product')) { $product_category = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent')); if ($product_category) { $this->current_cat = end($product_category); $this->cat_ancestors = get_ancestors($this->current_cat->term_id, 'product_cat'); } } // Show Siblings and Children Only if ($s && $this->current_cat) { // Top level is needed $top_level = get_terms('product_cat', array('fields' => 'ids', 'parent' => 0, 'hierarchical' => false, 'hide_empty' => false)); // Direct children are wanted $direct_children = get_terms('product_cat', array('fields' => 'ids', 'parent' => $this->current_cat->term_id, 'hierarchical' => true, 'hide_empty' => false)); // Gather siblings of ancestors $siblings = array(); if ($this->cat_ancestors) { foreach ($this->cat_ancestors as $ancestor) { $siblings = array_merge($siblings, get_terms('product_cat', array('fields' => 'ids', 'parent' => $ancestor, 'hierarchical' => false, 'hide_empty' => false))); } } $include = array_merge($top_level, $this->cat_ancestors, $siblings, $direct_children, array($this->current_cat->term_id)); $dropdown_args['include'] = implode(',', $include); $cat_args['include'] = implode(',', $include); } elseif ($s) { $dropdown_args['depth'] = 1; $dropdown_args['child_of'] = 0; $cat_args['depth'] = 1; $cat_args['child_of'] = 0; } // Dropdown if ($d) { $dropdown_defaults = array('show_counts' => $c, 'hierarchical' => $h, 'show_uncategorized' => 0, 'orderby' => $o, 'selected' => $this->current_cat ? $this->current_cat->slug : ''); $dropdown_args = wp_parse_args($dropdown_args, $dropdown_defaults); // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258 wc_product_dropdown_categories($dropdown_args); ?> <script type='text/javascript'> /* <![CDATA[ */ var product_cat_dropdown = document.getElementById("dropdown_product_cat"); function onProductCatChange() { if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) { location.href = "<?php echo home_url(); ?> /?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value; } } product_cat_dropdown.onchange = onProductCatChange; /* ]]> */ </script> <?php // List } else { include_once WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php'; $cat_args['walker'] = new WC_Product_Cat_List_Walker(); $cat_args['title_li'] = ''; $cat_args['pad_counts'] = 1; $cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce'); $cat_args['current_category'] = $this->current_cat ? $this->current_cat->term_id : ''; $cat_args['current_category_ancestors'] = $this->cat_ancestors; echo '<ul class="product-categories">'; wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $cat_args)); echo '</ul>'; } echo $after_widget; }
/** * Returns a single product attribute. * * @param mixed $attr * @return string */ public function get_attribute($attr) { $attributes = $this->get_attributes(); $attr = sanitize_title($attr); if (isset($attributes[$attr]) || isset($attributes['pa_' . $attr])) { $attribute = isset($attributes[$attr]) ? $attributes[$attr] : $attributes['pa_' . $attr]; if ($attribute['is_taxonomy']) { return implode(', ', wc_get_product_terms($this->id, $attribute['name'], array('fields' => 'names'))); } else { return $attribute['value']; } } return ''; }
/** * Check the customer's cart, and notify them if their cart contents don't match the restrictions. * * Executed during the 'woocommerce_check_cart_items' hook */ public function CheckCart() { $this->LoadRestrictions(); if ($this->GetStorewideMultipleOf() > 0) { // Entire cart contents must be a multiple of x. This takes precedence over all other product group rules $modulus = WC()->cart->cart_contents_count % $this->cart_must_be_multiple_of; if ($modulus != 0) { $message = $this->GetMultipleOfMessage(); $message = str_replace('%gap%', $this->cart_must_be_multiple_of - $modulus, $message); $message = str_replace('%mod%', $this->cart_must_be_multiple_of, $message); $message = str_replace('%productlist%', '', $message); wc_add_notice($message, 'error'); } } foreach (WC()->cart->get_cart() as $cart_item_id => $cart_item) { // Product Attribute/Variation restrictions if (isset($cart_item['data']) && $cart_item['data'] instanceof WC_Product_Variation && is_array($cart_item['variation'])) { foreach ($cart_item['variation'] as $variation_name => $variation_value) { if (!$variation_value) { continue; } if (taxonomy_exists(esc_attr(str_replace('attribute_', '', $variation_name)))) { $term = get_term_by('slug', $variation_value, esc_attr(str_replace('attribute_', '', $variation_name))); if (!$term) { continue; } if (isset($this->multiple_ofs_attributes[$term->term_id])) { if (isset($this->product_variation_quantities[$term->term_id]['qty'])) { $this->product_variation_quantities[$term->term_id]['qty'] += $cart_item['quantity']; } else { $this->product_variation_quantities[$term->term_id]['qty'] = $cart_item['quantity']; } $this->product_variation_quantities[$term->term_id]['products'][$cart_item['product_id']] = true; } } } } // Product Category restrictions $categories = wc_get_product_terms($cart_item['product_id'], 'product_cat'); foreach ($categories as $category) { $restriction = $this->GetMultipleOfForCategory($category->term_id); if ($restriction) { if (isset($this->product_category_quantities[$restriction])) { // This product could be assigned to multiple categories (each of which could have matching restrictions) // Only count the first one if (!array_key_exists($cart_item['product_id'], $this->product_category_quantities[$restriction]['products'])) { $this->product_category_quantities[$restriction]['qty'] += $cart_item['quantity']; } } else { $this->product_category_quantities[$restriction]['qty'] = $cart_item['quantity']; } $this->product_category_quantities[$restriction]['products'][$cart_item['product_id']] = true; } } } // Check the Attribute/Variation Restrictions foreach ($this->product_variation_quantities as $term_id => $data) { $quantity = $data['qty']; $groupof = $this->multiple_ofs_attributes[$term_id]; $modulus = $quantity % $groupof; if ($modulus != 0) { $message = $this->GetMultipleOfMessage(); $message = str_replace('%gap%', $groupof - $modulus, $message); $message = str_replace('%mod%', $groupof, $message); $productlist = '<ul>'; foreach (array_keys($data['products']) as $product_id) { $product = wc_get_product($product_id); $product_name = ''; $variation_info = ''; foreach (WC()->cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $product_id) { $product_name = $product->get_title(); $variation_info = WC()->cart->get_item_data($cart_item, true); break; } } $productlist .= '<li>' . sprintf(__('<a href="%1$s">%2$s</a> <small>%3$s</small>', 'woocommerce-product-restrictions'), get_permalink($product_id), $product_name, $variation_info) . '</li>'; } $productlist .= '</ul>'; $message = str_replace('%productlist%', $productlist, $message); wc_add_notice($message, 'error'); } } // Check the Product Category restrictions foreach ($this->product_category_quantities as $groupof => $data) { $quantity = $data['qty']; $modulus = $quantity % $groupof; if ($modulus != 0) { $message = $this->GetMultipleOfMessage(); $message = str_replace('%gap%', $groupof - $modulus, $message); $message = str_replace('%mod%', $groupof, $message); $productlist = '<ul>'; foreach (array_keys($data['products']) as $product_id) { // get_product() exists in WooCommerce 2.0+ only $product = wc_get_product($product_id); $product_name = ''; $variation_info = ''; foreach (WC()->cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $product_id) { $product_name = $product->get_title(); $variation_info = WC()->cart->get_item_data($cart_item, true); break; } } $productlist .= '<li>' . sprintf(__('<a href="%1$s">%2$s</a> <small>%3$s</small>', 'woocommerce-product-restrictions'), get_permalink($product_id), $product_name, $variation_info) . '</li>'; } $productlist .= '</ul>'; $message = str_replace('%productlist%', $productlist, $message); wc_add_notice($message, 'error'); } } }
<option value=""><?php echo __('Choose an option', 'woocommerce'); ?> …</option> <?php if (is_array($options)) { if (isset($_REQUEST['attribute_' . sanitize_title($name)])) { $selected_value = $_REQUEST['attribute_' . sanitize_title($name)]; } elseif (isset($selected_attributes[sanitize_title($name)])) { $selected_value = $selected_attributes[sanitize_title($name)]; } else { $selected_value = ''; } // Get terms if this is a taxonomy - ordered if (taxonomy_exists($name)) { $terms = wc_get_product_terms($post->ID, $name, array('fields' => 'all')); foreach ($terms as $term) { if (!in_array($term->slug, $options)) { continue; } echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($selected_value), sanitize_title($term->slug), false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>'; } } else { foreach ($options as $option) { echo '<option value="' . esc_attr(sanitize_title($option)) . '" ' . selected(sanitize_title($selected_value), sanitize_title($option), false) . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>'; } } } ?> </select> <?php if (sizeof($attributes) === $loop) {
foreach ($product->get_attributes() as $attribute) { if (empty($attribute['is_visible']) || $attribute['is_taxonomy'] && !taxonomy_exists($attribute['name'])) { continue; } else { $show = true; } ?> <li class="clearfix"> <div class="halfcol left"><?php echo wc_attribute_label($attribute['name']); ?> </div> <div class="halfcol right"> <?php if ($attribute['is_taxonomy']) { $values = wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names')); echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } else { $values = array_map('trim', explode(WC_DELIMITER, $attribute['value'])); echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values); } ?> </div> </li> <?php } ?> </ul> </div> </div> </div>
<?php $sanitized_name = sanitize_title($name); if (isset($_REQUEST['attribute_' . $sanitized_name])) { $checked_value = $_REQUEST['attribute_' . $sanitized_name]; } elseif (isset($selected_attributes[$sanitized_name])) { $checked_value = $selected_attributes[$sanitized_name]; } else { $checked_value = ''; } ?> <td class="value"> <?php if (!empty($options)) { if (taxonomy_exists($name)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $name, array('fields' => 'all')); foreach ($terms as $term) { if (!in_array($term->slug, $options)) { continue; } print_attribute_radio($checked_value, $term->slug, $term->name, $sanitized_name); } } else { foreach ($options as $option) { print_attribute_radio($checked_value, $option, $option, $sanitized_name); } } } echo end($attribute_keys) === $name ? '<a class="reset_variations" href="#">' . __('Clear selection', 'woocommerce') . '</a>' : ''; ?> </td>
/** * Output a list of variation attributes for use in the cart forms. * * @param array $args * @since 2.4.0 */ function wc_dropdown_variation_attribute_options($args = array()) { $args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args', $args), array('options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '', 'show_option_none' => __('Choose an option', 'woocommerce'))); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute); $id = $args['id'] ? $args['id'] : sanitize_title($attribute); $class = $args['class']; if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } $html = '<select id="' . esc_attr($id) . '" class="' . esc_attr($class) . '" name="' . esc_attr($name) . '" data-attribute_name="attribute_' . esc_attr(sanitize_title($attribute)) . '">'; if ($args['show_option_none']) { $html .= '<option value="">' . esc_html($args['show_option_none']) . '</option>'; } if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { $html .= '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) . '</option>'; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $selected = sanitize_title($args['selected']) === $args['selected'] ? selected($args['selected'], sanitize_title($option), false) : selected($args['selected'], $option, false); $html .= '<option value="' . esc_attr($option) . '" ' . $selected . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>'; } } } $html .= '</select>'; echo apply_filters('woocommerce_dropdown_variation_attribute_options_html', $html, $args); }
$_name = wc_get_page_id('shop') ? get_the_title(wc_get_page_id('shop')) : ''; if (!$_name) { $product_post_type = get_post_type_object('product'); $_name = $product_post_type->labels->singular_name; } if (is_search()) { echo $before . '<a href="' . get_post_type_archive_link('product') . '">' . $_name . '</a>' . $delimiter . __('Search results for “', 'woocommerce') . get_search_query() . '”' . $after; } elseif (is_paged()) { echo $before . '<a href="' . get_post_type_archive_link('product') . '">' . $_name . '</a>' . $after; } else { echo $before . $_name . $after; } } elseif (is_single() && !is_attachment()) { if ('product' == get_post_type()) { echo $prepend; if ($terms = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent', 'order' => 'DESC'))) { $main_term = $terms[0]; $ancestors = get_ancestors($main_term->term_id, 'product_cat'); $ancestors = array_reverse($ancestors); foreach ($ancestors as $ancestor) { $ancestor = get_term($ancestor, 'product_cat'); if (!is_wp_error($ancestor) && $ancestor) { echo $before . '<a href="' . get_term_link($ancestor) . '">' . $ancestor->name . '</a>' . $after . $delimiter; } } echo $before . '<a href="' . get_term_link($main_term) . '">' . $main_term->name . '</a>' . $after . $delimiter; } echo $before . get_the_title() . $after; } elseif ('post' != get_post_type()) { $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite;
/** * Link all variations via ajax function */ public static function link_all_variations() { if (!defined('WC_MAX_LINKED_VARIATIONS')) { define('WC_MAX_LINKED_VARIATIONS', 49); } check_ajax_referer('link-variations', 'security'); if (!current_user_can('edit_products')) { die(-1); } if (function_exists('set_time_limit') && false === strpos(ini_get('disable_functions'), 'set_time_limit') && !ini_get('safe_mode')) { set_time_limit(0); } $post_id = intval($_POST['post_id']); if (!$post_id) { die; } $variations = array(); $_product = wc_get_product($post_id, array('product_type' => 'variable')); // Put variation attributes into an array foreach ($_product->get_attributes() as $attribute) { if (!$attribute['is_variation']) { continue; } $attribute_field_name = 'attribute_' . sanitize_title($attribute['name']); if ($attribute['is_taxonomy']) { $options = wc_get_product_terms($post_id, $attribute['name'], array('fields' => 'slugs')); } else { $options = explode(WC_DELIMITER, $attribute['value']); } $options = array_map('sanitize_title', array_map('trim', $options)); $variations[$attribute_field_name] = $options; } // Quit out if none were found if (sizeof($variations) == 0) { die; } // Get existing variations so we don't create duplicates $available_variations = array(); foreach ($_product->get_children() as $child_id) { $child = $_product->get_child($child_id); if (!empty($child->variation_id)) { $available_variations[] = $child->get_variation_attributes(); } } // Created posts will all have the following data $variation_post_data = array('post_title' => 'Product #' . $post_id . ' Variation', 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation'); // Now find all combinations and create posts if (!function_exists('array_cartesian')) { /** * @param array $input * @return array */ function array_cartesian($input) { $result = array(); while (list($key, $values) = each($input)) { // If a sub-array is empty, it doesn't affect the cartesian product if (empty($values)) { continue; } // Special case: seeding the product array with the values from the first sub-array if (empty($result)) { foreach ($values as $value) { $result[] = array($key => $value); } } else { // Second and subsequent input sub-arrays work like this: // 1. In each existing array inside $product, add an item with // key == $key and value == first item in input sub-array // 2. Then, for each remaining item in current input sub-array, // add a copy of each existing array inside $product with // key == $key and value == first item in current input sub-array // Store all items to be added to $product here; adding them on the spot // inside the foreach will result in an infinite loop $append = array(); foreach ($result as &$product) { // Do step 1 above. array_shift is not the most efficient, but it // allows us to iterate over the rest of the items with a simple // foreach, making the code short and familiar. $product[$key] = array_shift($values); // $product is by reference (that's why the key we added above // will appear in the end result), so make a copy of it here $copy = $product; // Do step 2 above. foreach ($values as $item) { $copy[$key] = $item; $append[] = $copy; } // Undo the side effecst of array_shift array_unshift($values, $product[$key]); } // Out of the foreach, we can add to $results now $result = array_merge($result, $append); } } return $result; } } $variation_ids = array(); $added = 0; $possible_variations = array_cartesian($variations); foreach ($possible_variations as $variation) { // Check if variation already exists if (in_array($variation, $available_variations)) { continue; } $variation_id = wp_insert_post($variation_post_data); $variation_ids[] = $variation_id; foreach ($variation as $key => $value) { update_post_meta($variation_id, $key, $value); } // Save stock status update_post_meta($variation_id, '_stock_status', 'instock'); $added++; do_action('product_variation_linked', $variation_id); if ($added > WC_MAX_LINKED_VARIATIONS) { break; } } delete_transient('wc_product_children_' . $post_id); echo $added; die; }
/** * Output a list of variation attributes for use in the cart forms. * used in the plugins template instead of wc_dropdown_variation_attribute_options * * @param array $args * @since 0.0.1 */ function wc_radio_variation_attribute_options($args = array()) { $args = wp_parse_args($args, array('options' => false, 'attribute' => false, 'product' => false, 'selected' => false, 'name' => '', 'id' => '', 'class' => '', 'show_option_none' => __('Choose an option', 'woocommerce'))); $options = $args['options']; $product = $args['product']; $attribute = $args['attribute']; $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title($attribute); $id = $args['id'] ? $args['id'] : sanitize_title($attribute); $class = $args['class']; $selectedValue = $args['selected']; $first = true; if (empty($options) && !empty($product) && !empty($attribute)) { $attributes = $product->get_variation_attributes(); $options = $attributes[$attribute]; } echo '<fieldset id=\'' . esc_attr($id) . '\' class=\'' . esc_attr($class) . '\' name=\'' . esc_attr($name) . '\' data-attribute_name=\'' . esc_attr($name) . '\'>'; echo '<legend>' . wc_attribute_label($attribute) . '</legend>'; do_action("before_variations_with_radio_buttons_list", $args); echo '<div class=\'product_variable_list\'>'; if (!empty($options)) { if ($product && taxonomy_exists($attribute)) { // Get terms if this is a taxonomy - ordered. We need the names too. $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); foreach ($terms as $term) { if (in_array($term->slug, $options)) { $checkedString = $selectedValue ? checked(sanitize_title($selectedValue), $term->slug, false) : checked($first, true, false); wc_radio_select_button_for_add_to_cart(array("value" => esc_attr($term->slug), "checked" => $checkedString, "content" => $term->name, "name" => sanitize_title($attribute), "description" => $term->description)); $first = false; } } } else { foreach ($options as $option) { // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. $checkedString = $selectedValue ? sanitize_title($selectedValue) === $selectedValue ? checked($selectedValue, sanitize_title($option), false) : checked($selectedValue, $option, false) : checked($first, true, false); wc_radio_select_button_for_add_to_cart(array("value" => esc_attr($option), "checked" => $checkedString, "content" => esc_html($option), "name" => sanitize_title($attribute))); $first = true; } } } echo '</div>'; do_action("after_variations_with_radio_buttons_list", $args); echo '</fieldset>'; }