public function is_bulk_variation_form() { global $post; if (!$post) { return false; } /** Remove validation (_bv_type) -- 03/02/2016 **/ if (!is_product()) { return false; } /****/ /** Validate if exits role -- 03/02/2016 **/ if ($this->role_exists('wholesale_customer')) { if (!current_user_can('wholesale_customer')) { return false; } } // 2.0 Compat if (function_exists('get_product')) { $product = get_product($post->ID); } else { $product = new WC_Product($post->ID); } if ($product && !$product->has_child() && !$product->is_type('variable')) { return false; } return apply_filters('woocommerce_bv_render_form', true); }
/** * Gte woocommerce data for product * * @param $value * @param $data * * @return string */ function vc_gitem_template_attribute_woocommerce_product($value, $data) { $label = ''; /** * @var null|Wp_Post $post ; * @var string $data ; */ extract(array_merge(array('post' => null, 'data' => ''), $data)); require_once WC()->plugin_path() . '/includes/abstracts/abstract-wc-product.php'; $product = new WC_Product($post); if (preg_match('/_labeled$/', $data)) { $data = preg_replace('/_labeled$/', '', $data); $label = apply_filters('vc_gitem_template_attribute_woocommerce_product_' . $data . '_label', Vc_Vendor_Woocommerce::getProductFieldLabel($data) . ': '); } $price_format = get_woocommerce_price_format(); switch ($data) { case 'id': $value = (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id; break; case 'sku': $value = $product->get_sku(); break; case 'price': $value = sprintf($price_format, wc_format_decimal($product->get_price(), 2), get_woocommerce_currency()); break; case 'regular_price': $value = sprintf($price_format, wc_format_decimal($product->get_regular_price(), 2), get_woocommerce_currency()); break; case 'sale_price': $value = sprintf(get_woocommerce_price_format(), $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : '', get_woocommerce_currency()); break; case 'price_html': $value = $product->get_price_html(); break; case 'reviews_count': $value = count(get_comments(array('post_id' => $post->ID, 'approve' => 'approve'))); break; case 'short_description': $value = apply_filters('woocommerce_short_description', $product->get_post_data()->post_excerpt); break; case 'dimensions': $units = get_option('woocommerce_dimension_unit'); $value = $product->length . $units . 'x' . $product->width . $units . 'x' . $product->height . $units; break; case 'raiting_count': $value = $product->get_rating_count(); break; case 'weight': $value = $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : ''; break; case 'on_sale': $value = $product->is_on_sale() ? 'yes' : 'no'; // @todo change break; default: $value = $product->{$data}; } return strlen($value) > 0 ? $label . apply_filters('vc_gitem_template_attribute_woocommerce_product_' . $data . '_value', $value) : ''; }
/** * Bulk edit * @param integer $post_id * @param WC_Product $product */ public function bulk_edit_save($post_id, $product) { $old_regular_price = $product->regular_price; $old_sale_price = $product->sale_price; // Save fields if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) { update_post_meta($post_id, '_weight', wc_clean(stripslashes($_REQUEST['_weight']))); } if (!empty($_REQUEST['change_dimensions'])) { if (isset($_REQUEST['_length'])) { update_post_meta($post_id, '_length', wc_clean(stripslashes($_REQUEST['_length']))); } if (isset($_REQUEST['_width'])) { update_post_meta($post_id, '_width', wc_clean(stripslashes($_REQUEST['_width']))); } if (isset($_REQUEST['_height'])) { update_post_meta($post_id, '_height', wc_clean(stripslashes($_REQUEST['_height']))); } } if (!empty($_REQUEST['_tax_status'])) { update_post_meta($post_id, '_tax_status', wc_clean($_REQUEST['_tax_status'])); } if (!empty($_REQUEST['_tax_class'])) { $tax_class = wc_clean($_REQUEST['_tax_class']); if ('standard' == $tax_class) { $tax_class = ''; } update_post_meta($post_id, '_tax_class', $tax_class); } if (!empty($_REQUEST['_stock_status'])) { $stock_status = wc_clean($_REQUEST['_stock_status']); if ($product->is_type('variable')) { foreach ($product->get_children() as $child_id) { if ('yes' !== get_post_meta($child_id, '_manage_stock', true)) { wc_update_product_stock_status($child_id, $stock_status); } } WC_Product_Variable::sync_stock_status($post_id); } else { wc_update_product_stock_status($post_id, $stock_status); } } if (!empty($_REQUEST['_shipping_class'])) { $shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']); wp_set_object_terms($post_id, $shipping_class, 'product_shipping_class'); } if (!empty($_REQUEST['_visibility'])) { if (update_post_meta($post_id, '_visibility', wc_clean($_REQUEST['_visibility']))) { do_action('woocommerce_product_set_visibility', $post_id, wc_clean($_REQUEST['_visibility'])); } } if (!empty($_REQUEST['_featured'])) { if (update_post_meta($post_id, '_featured', stripslashes($_REQUEST['_featured']))) { delete_transient('wc_featured_products'); } } // Sold Individually if (!empty($_REQUEST['_sold_individually'])) { if ($_REQUEST['_sold_individually'] == 'yes') { update_post_meta($post_id, '_sold_individually', 'yes'); } else { update_post_meta($post_id, '_sold_individually', ''); } } // Handle price - remove dates and set to lowest if ($product->is_type('simple') || $product->is_type('external')) { $price_changed = false; if (!empty($_REQUEST['change_regular_price'])) { $change_regular_price = absint($_REQUEST['change_regular_price']); $regular_price = esc_attr(stripslashes($_REQUEST['_regular_price'])); switch ($change_regular_price) { case 1: $new_price = $regular_price; break; case 2: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals()); } else { $new_price = $old_regular_price + $regular_price; } break; case 3: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals())); } else { $new_price = max(0, $old_regular_price - $regular_price); } break; default: break; } if (isset($new_price) && $new_price != $old_regular_price) { $price_changed = true; $new_price = round($new_price, wc_get_price_decimals()); update_post_meta($post_id, '_regular_price', $new_price); $product->regular_price = $new_price; } } if (!empty($_REQUEST['change_sale_price'])) { $change_sale_price = absint($_REQUEST['change_sale_price']); $sale_price = esc_attr(stripslashes($_REQUEST['_sale_price'])); switch ($change_sale_price) { case 1: $new_price = $sale_price; break; case 2: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = $old_sale_price + $old_sale_price * $percent; } else { $new_price = $old_sale_price + $sale_price; } break; case 3: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $old_sale_price - $old_sale_price * $percent); } else { $new_price = max(0, $old_sale_price - $sale_price); } break; case 4: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $product->regular_price - $product->regular_price * $percent); } else { $new_price = max(0, $product->regular_price - $sale_price); } break; default: break; } if (isset($new_price) && $new_price != $old_sale_price) { $price_changed = true; $new_price = !empty($new_price) || '0' === $new_price ? round($new_price, wc_get_price_decimals()) : ''; update_post_meta($post_id, '_sale_price', $new_price); $product->sale_price = $new_price; } } if ($price_changed) { update_post_meta($post_id, '_sale_price_dates_from', ''); update_post_meta($post_id, '_sale_price_dates_to', ''); if ($product->regular_price < $product->sale_price) { $product->sale_price = ''; update_post_meta($post_id, '_sale_price', ''); } if ($product->sale_price) { update_post_meta($post_id, '_price', $product->sale_price); } else { update_post_meta($post_id, '_price', $product->regular_price); } } } // Handle stock if (!$product->is_type('grouped')) { if (!empty($_REQUEST['change_stock'])) { update_post_meta($post_id, '_manage_stock', 'yes'); wc_update_product_stock($post_id, wc_stock_amount($_REQUEST['_stock'])); } if (!empty($_REQUEST['_manage_stock'])) { if ($_REQUEST['_manage_stock'] == 'yes') { update_post_meta($post_id, '_manage_stock', 'yes'); } else { update_post_meta($post_id, '_manage_stock', 'no'); wc_update_product_stock($post_id, 0); } } if (!empty($_REQUEST['_backorders'])) { update_post_meta($post_id, '_backorders', wc_clean($_REQUEST['_backorders'])); } } do_action('woocommerce_product_bulk_edit_save', $product); }
/** * Replaces variation SKUs with the parent SKU * * @param array $line_item the line item's data in the export * @param array $item the order item being exported * @param \WC_Product $product the product associated with the item * @return array - updated line item */ function sv_wc_csv_export_order_line_item_sku($line_item, $item, $product) { if ($product->is_type('variation')) { $line_item['sku'] = $product->parent->get_sku(); } return $line_item; }
/** * Checks a given product to determine if it is a subscription. * * Can be passed either a product object or product ID. * * @since 1.0 */ public static function is_subscription($product) { if (!is_object($product)) { $product = new WC_Product($product); } // Shouldn't matter if product is variation as all we need is the product_type return $product->is_type(WC_Subscriptions::$name) ? true : false; }
/** * Backports WC_Product::get_id() method to 2.4.x * * @link https://github.com/woothemes/woocommerce/pull/9765 * * @since 4.2.0 * @param \WC_Product $product product object * @return string|int product ID */ public static function product_get_id(WC_Product $product) { if (self::is_wc_version_gte_2_5()) { return $product->get_id(); } else { return $product->is_type('variation') ? $product->variation_id : $product->id; } }
/** * Add the product id & variation ID to the individual line item entry * * @param array $line_item the original line item data * @param array $item WC order item data * @param WC_Product $product the product * @return array $line_item the updated line item data */ function sv_wc_csv_export_order_line_item_id($line_item, $item, $product) { $line_item['item_id'] = $product->id; $line_item['variation_id'] = ''; // set the variation id for variable products if ($product->is_type('variation')) { $line_item['variation_id'] = $product->get_variation_id(); } return $line_item; }
/** * Pluggable function to render the frontend product page voucher fields * * @since 1.2 * @param WC_Product $product the voucher product */ function wc_pdf_product_vouchers_render_product_voucher_fields($product) { if ($product->is_type('variable')) { foreach ($product->get_children() as $variation_product_id) { $products[] = wc_get_product($variation_product_id); } } else { $products[] = $product; } foreach ($products as $product) { $voucher = WC_PDF_Product_Vouchers_Product::get_voucher($product); if ($voucher) { $fields = $voucher->get_user_input_voucher_fields(); $images = $voucher->get_image_urls(); if ($fields || $images) { // load the template file wc_get_template('single-product/product-voucher.php', array('product' => $product, 'product_id' => isset($product->variation_id) ? $product->variation_id : $product->id, 'voucher' => $voucher, 'fields' => $fields, 'images' => $images), '', wc_pdf_product_vouchers()->get_plugin_path() . '/templates/'); } } } }
/** * Write panel for Product Bundles **/ function woo_bundles_product_write_panel() { global $woocommerce, $post, $wpdb; ?> <div id="bundled_product_data" class="panel woocommerce_options_panel"> <div class="options_group"> <p><label for="bundled_ids"><?php _e('Bundled Products', 'woo-bundles'); ?> </label> </p> <div class="wc-bundled_products"> <div class="bundled_products_info"> <?php _e('Note', 'woo-bundles'); echo '<img class="help_tip" data-tip="' . __('Select the products that you want to include in your bundle, kit, or assembly. Any simple or variable product can be added - physical, or downloadable.<br/><strong>Important</strong>: v2.0 has introduced the ability to bundle <strong>multiple instances of the same variable product</strong> and configure each instance separately - for details, check out the online documentation.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />'; ?> </div> <div class="bundled_products_selector"> <select id="bundled_ids" name="bundled_ids[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e('Search for a product…', 'woo-bundles'); ?> "> <?php $item_ids = get_post_meta($post->ID, '_bundled_ids', true); $bundled_variable_num = 0; if ($item_ids) { foreach ($item_ids as $item_id) { // remove suffix $sep = explode('_', $item_id); $product_id = $sep[0]; if ($terms = wp_get_object_terms($product_id, 'product_type')) { $product_type = current($terms)->slug; } if ($product_type == 'variable') { $bundled_variable_num++; } $title = get_the_title($product_id) . ($product_id != $item_id ? ' #' . $sep[1] : ''); $sku = get_post_meta($product_id, '_sku', true); if (!$title) { continue; } if (isset($sku) && $sku) { $sku = ' (SKU: ' . $sku . ')'; } echo '<option value="' . $product_id . '" selected="selected">' . $title . $sku . '</option>'; } } ?> </select> </div> </div> <?php if ($item_ids) { ?> <p><label for="bundled_ids_notice"><?php _e('Bundle Configuration', 'woo-bundles'); ?> </label></p> <?php $allowed_variations = get_post_meta($post->ID, '_allowed_variations', true); $default_attributes = (array) maybe_unserialize(get_post_meta($post->ID, '_bundle_defaults', true)); foreach ($item_ids as $item_id) { // remove suffix $sep = explode('_', $item_id); $product_id = $sep[0]; $title = get_the_title($product_id) . ($product_id != $item_id ? ' #' . $sep[1] : ''); $sku = get_post_meta($product_id, '_sku', true); if (isset($sku) && $sku) { $sku = ' (SKU: ' . $sku . ')'; } if (!$title) { continue; } ?> <div class="wc-bundled-item"> <div class="item-description"> <?php echo $title . ' – #' . $product_id; ?> <br/><?php echo $sku; ?> </div> <div class="item-data"> <?php if ($this->is_wc_v2()) { $bundled_product = get_product($product_id); } else { $bundled_product = new WC_Product($product_id); } if ($bundled_product->is_type('variable')) { ?> <div class="filtering"> <?php woocommerce_wp_checkbox(array('id' => 'filter_variations_' . $item_id, 'wrapper_class' => 'filter_variations', 'label' => __('Filter Variations', 'woo-bundles'), 'description' => '<img class="help_tip" data-tip="' . __('Check to enable only a subset of the available variations.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />')); ?> </div> <div class="bundle_variation_filters indented"> <select multiple="multiple" name="allowed_variations[<?php echo $item_id; ?> ][]" style="width: 450px; display: none; " data-placeholder="Choose variations…" title="Variations" class="chosen_select" > <?php $args = array('post_type' => 'product_variation', 'post_status' => array('private', 'publish'), 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'asc', 'post_parent' => $product_id, 'fields' => 'ids'); $variations = get_posts($args); foreach ($variations as $variation) { $product_custom_fields = get_post_custom($variation); ksort($product_custom_fields); $description = ''; foreach ($product_custom_fields as $name => $value) { if (!strstr($name, 'attribute_')) { continue; } $attribute_name = substr($name, strlen('attribute_')); $description_name = ucwords($woocommerce->attribute_label($attribute_name)); if (!$value[0]) { $description_value = __('Any', 'woocommerce') . ' ' . $description_name; } else { $term = get_term_by('slug', $value[0], $attribute_name); $description_value = $term == false ? '' : $term->name; } if (!$description_value) { $description_value = $value[0]; } $description .= $description_name . ': ' . apply_filters('woocommerce_variation_option_name', $description_value) . ', '; } if (is_array($allowed_variations[$item_id]) && in_array($variation, $allowed_variations[$item_id])) { $selected = 'selected="selected"'; } else { $selected = ''; } echo '<option value="' . $variation . '" ' . $selected . '>#' . $variation . ' - ' . rtrim($description, ', ') . '</option>'; } ?> </select> <?php woocommerce_wp_checkbox(array('id' => 'hide_filtered_variations_' . $item_id, 'wrapper_class' => 'hide_filtered_variations', 'label' => __('Hide Filtered-Out Options', 'woo-bundles'), 'description' => '<img class="help_tip" data-tip="' . __('Check to remove any filtered-out variation options from this item\'s drop-downs. If you leave the box unchecked, the options corresponding to filtered-out variations will be disabled but still visible.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />')); ?> </div> <div class="defaults"> <?php woocommerce_wp_checkbox(array('id' => 'override_defaults_' . $item_id, 'wrapper_class' => 'override_defaults', 'label' => __('Override Default Selections', 'woo-bundles'), 'description' => '<img class="help_tip" data-tip="' . __('In effect for this bundle only. The available options are in sync with the filtering settings above. Always save any changes made above before configuring this section.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />')); ?> </div> <div class="bundle_selection_defaults indented"> <?php // get normal variation attributes $attributes = $bundled_product->get_variation_attributes(); // filtered variation attributes $filtered_attributes = array(); // get filter-active setting $filtered = get_post_meta($post->ID, 'filter_variations_' . $item_id, true) == 'yes' ? true : false; // if filter is active, show only the attributes of the selected variations if ($filtered) { foreach ($allowed_variations[$item_id] as $allowed_variation_id) { // get variation meta of allowed variations $product_custom_fields = get_post_custom($allowed_variation_id); foreach ($product_custom_fields as $name => $value) { if (!strstr($name, 'attribute_')) { continue; } $attribute_name = substr($name, strlen('attribute_')); if (!isset($filtered_attributes[$attribute_name])) { $filtered_attributes[$attribute_name][] = $value[0]; } elseif (!in_array($value[0], $filtered_attributes[$attribute_name])) { $filtered_attributes[$attribute_name][] = $value[0]; } } } } foreach ($attributes as $name => $values) { sort($values); // Get current value for variation (if set) $variation_selected_value = isset($default_attributes[$item_id][sanitize_title($name)]) ? $default_attributes[$item_id][sanitize_title($name)] : ''; // Name will be something like attribute_pa_color echo '<select name="default_attributes[' . $item_id . '][' . sanitize_title($name) . ']"><option value="">' . __('No default', 'woocommerce') . ' ' . $woocommerce->attribute_label($name) . '…</option>'; foreach ($values as $value) { // if filters exist, only show the entries available in the filtered_attributes array if ($filtered && isset($filtered_attributes[sanitize_title($name)])) { if (!in_array($value, $filtered_attributes[sanitize_title($name)]) && !in_array('', $filtered_attributes[sanitize_title($name)])) { continue; } } $term = get_term_by('slug', $value, $name); $value_name = $term == false ? '' : $term->name; if (!$value_name) { $value_name = ucwords($value); } echo '<option ' . selected($variation_selected_value, $value, false) . ' value="' . $value . '">' . apply_filters('woocommerce_variation_option_name', $value_name) . '</option>'; } echo '</select>'; } ?> </div> <?php } $item_quantity = get_post_meta($post->ID, 'bundle_quantity_' . $item_id, true); if (!isset($item_quantity) || $item_quantity < 1 || get_post_meta($product_id, '_downloadable', true) == 'yes' && get_post_meta($product_id, '_virtual', true) == 'yes' && get_option('woocommerce_limit_downloadable_product_qty') == 'yes') { $item_quantity = 1; } ?> <div class="quantity"> <?php woocommerce_wp_text_input(array('id' => 'bundle_quantity_' . $item_id, 'class' => 'bundle_quantity', 'label' => __('Quantity', 'woocommerce'))); ?> </div> <div class="item_visibility"> <label for="item_visibility"><?php _e('Front-End Visibility', 'woo-bundles'); ?> </label> <select name="visibility_<?php echo $item_id; ?> "> <?php $visible = get_post_meta($post->ID, 'visibility_' . $item_id, true) == 'hidden' ? false : true; echo '<option ' . selected($visible, true, false) . ' value="visible">' . __('Visible', 'woo-bundles') . '</option>'; echo '<option ' . selected($visible, false, false) . ' value="hidden">' . __('Hidden', 'woo-bundles') . '</option>'; echo '<img class="help_tip" data-tip="' . __('Check this option to hide the thumbnail of this bundled product.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />'; ?> </select> </div> <div class="images"> <?php woocommerce_wp_checkbox(array('id' => 'hide_thumbnail_' . $item_id, 'wrapper_class' => 'hide_thumbnail', 'label' => __('Hide Product Thumbnail', 'woo-bundles'), 'description' => '')); ?> </div> <div class="override_title"> <?php woocommerce_wp_checkbox(array('id' => 'override_title_' . $item_id, 'wrapper_class' => 'override_title', 'label' => __('Override Title', 'woo-bundles'), 'description' => '<img class="help_tip" data-tip="' . __('Check this option to override the default product title.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />')); ?> <?php $item_title = get_post_meta($post->ID, 'product_title_' . $item_id, true); ?> <div class="custom_title indented"> <?php woocommerce_wp_text_input(array('id' => 'product_title_' . $item_id, 'class' => 'product_title', 'label' => __('Product Title', 'woo-bundles'))); ?> </div> </div> <div class="override_description"> <?php woocommerce_wp_checkbox(array('id' => 'override_description_' . $item_id, 'wrapper_class' => 'override_description', 'label' => __('Override Short Description', 'woo-bundles'), 'description' => '<img class="help_tip" data-tip="' . __('Check this option to override the default short product description.', 'woo-bundles') . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" />')); ?> <?php $item_description = get_post_meta($post->ID, 'product_description_' . $item_id, true); ?> <div class="custom_description indented"> <?php woocommerce_wp_textarea_input(array('id' => 'product_description_' . $item_id, 'class' => 'product_description', 'label' => __('Product Short Description', 'woo-bundles'))); ?> </div> </div> </div> </div> <?php } } else { ?> <p><label for="bundled_ids_notice"><?php _e('Bundle Configuration', 'woo-bundles'); ?> </label></p> <p><em><?php _e('To configure additional options, first select some products and then save your changes.', 'woo-bundles'); ?> </em></p> <?php } ?> </div> <!-- options group --> </div> <?php }
/** * Bulk edit. * * @param integer $post_id * @param WC_Product $product */ public function bulk_edit_save($post_id, $product) { $old_regular_price = $product->get_regular_price(); $old_sale_price = $product->get_sale_price(); // Save fields if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) { $product->set_weight(wc_clean(stripslashes($_REQUEST['_weight']))); } if (!empty($_REQUEST['change_dimensions'])) { if (isset($_REQUEST['_length'])) { $product->set_length(wc_clean(stripslashes($_REQUEST['_length']))); } if (isset($_REQUEST['_width'])) { $product->set_width(wc_clean(stripslashes($_REQUEST['_width']))); } if (isset($_REQUEST['_height'])) { $product->set_height(wc_clean(stripslashes($_REQUEST['_height']))); } } if (!empty($_REQUEST['_tax_status'])) { $product->set_tax_status(wc_clean($_REQUEST['_tax_status'])); } if (!empty($_REQUEST['_tax_class'])) { $tax_class = wc_clean($_REQUEST['_tax_class']); if ('standard' == $tax_class) { $tax_class = ''; } $product->set_tax_class($tax_class); } if (!empty($_REQUEST['_shipping_class'])) { $shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']); $shipping_class_id = $data_store->get_shipping_class_id_by_slug($shipping_class); if ($shipping_class_id) { $product->set_shipping_class_id($shipping_class_id); } } if (!empty($_REQUEST['_visibility'])) { $product->set_catalog_visibility(wc_clean($_REQUEST['_visibility'])); } if (!empty($_REQUEST['_featured'])) { $product->set_featured(stripslashes($_REQUEST['_featured'])); } // Sold Individually if (!empty($_REQUEST['_sold_individually'])) { if ('yes' === $_REQUEST['_sold_individually']) { $product->set_sold_individually('yes'); } else { $product->set_sold_individually(''); } } // Handle price - remove dates and set to lowest $change_price_product_types = apply_filters('woocommerce_bulk_edit_save_price_product_types', array('simple', 'external')); $can_product_type_change_price = false; foreach ($change_price_product_types as $product_type) { if ($product->is_type($product_type)) { $can_product_type_change_price = true; break; } } if ($can_product_type_change_price) { $price_changed = false; if (!empty($_REQUEST['change_regular_price'])) { $change_regular_price = absint($_REQUEST['change_regular_price']); $regular_price = esc_attr(stripslashes($_REQUEST['_regular_price'])); switch ($change_regular_price) { case 1: $new_price = $regular_price; break; case 2: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals()); } else { $new_price = $old_regular_price + $regular_price; } break; case 3: if (strstr($regular_price, '%')) { $percent = str_replace('%', '', $regular_price) / 100; $new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals())); } else { $new_price = max(0, $old_regular_price - $regular_price); } break; default: break; } if (isset($new_price) && $new_price != $old_regular_price) { $price_changed = true; $new_price = round($new_price, wc_get_price_decimals()); $product->set_regular_price($new_price); } } if (!empty($_REQUEST['change_sale_price'])) { $change_sale_price = absint($_REQUEST['change_sale_price']); $sale_price = esc_attr(stripslashes($_REQUEST['_sale_price'])); switch ($change_sale_price) { case 1: $new_price = $sale_price; break; case 2: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = $old_sale_price + $old_sale_price * $percent; } else { $new_price = $old_sale_price + $sale_price; } break; case 3: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $old_sale_price - $old_sale_price * $percent); } else { $new_price = max(0, $old_sale_price - $sale_price); } break; case 4: if (strstr($sale_price, '%')) { $percent = str_replace('%', '', $sale_price) / 100; $new_price = max(0, $product->regular_price - $product->regular_price * $percent); } else { $new_price = max(0, $product->regular_price - $sale_price); } break; default: break; } if (isset($new_price) && $new_price != $old_sale_price) { $price_changed = true; $new_price = !empty($new_price) || '0' === $new_price ? round($new_price, wc_get_price_decimals()) : ''; $product->set_sale_price($new_price); } } if ($price_changed) { $product->set_date_on_sale_to(''); $product->set_date_on_sale_from(''); if ($product->get_regular_price() < $product->get_sale_price()) { $product->set_sale_price(''); } } } // Handle Stock Data $was_managing_stock = $product->get_manage_stock() ? 'yes' : 'no'; $stock_status = $product->get_stock_status(); $backorders = $product->get_backorders(); $backorders = !empty($_REQUEST['_backorders']) ? wc_clean($_REQUEST['_backorders']) : $backorders; $stock_status = !empty($_REQUEST['_stock_status']) ? wc_clean($_REQUEST['_stock_status']) : $stock_status; if (!empty($_REQUEST['_manage_stock'])) { $manage_stock = 'yes' === wc_clean($_REQUEST['_manage_stock']) && 'grouped' !== $product->product_type ? 'yes' : 'no'; } else { $manage_stock = $was_managing_stock; } $stock_amount = 'yes' === $manage_stock && isset($_REQUEST['_change_stock']) ? wc_stock_amount($_REQUEST['_change_stock']) : ''; if ('yes' === get_option('woocommerce_manage_stock')) { // Apply product type constraints to stock status if ($product->is_type('external')) { // External always in stock $stock_status = 'instock'; } elseif ($product->is_type('variable')) { // Stock status is always determined by children foreach ($product->get_children() as $child_id) { $child = wc_get_product($child_id); if (!$product->get_manage_stock()) { $child->set_stock_status($stock_status); $child->save(); } } $product = WC_Product_Variable::sync($product, false); } $product->set_manage_stock($manage_stock); $product->set_backorders($backorders); $product->save(); if (!$product->is_type('variable')) { wc_update_product_stock_status($post_id, $stock_status); } wc_update_product_stock($post_id, $stock_amount); } else { $product->save(); wc_update_product_stock_status($post_id, $stock_status); } do_action('woocommerce_product_bulk_edit_save', $product); }
function hocwp_wc_is_variable(WC_Product $product) { return $product->is_type('variable'); }
/** * Gets a product meta field value, regardless of product type * * @since 3.5.0 * @param WC_Product $product the product * @param string $field_name the field name * @return mixed meta value */ public static function get_product_meta($product, $field_name) { // use magic __get $meta = $product->{$field_name}; // WC < 2.2.5 magic __get only returns parent meta for custom fields (core fields will work) if ($product->is_type('variation')) { $variation_meta = get_post_meta($product->variation_id, '_' . $field_name, true); $meta = empty($variation_meta) ? $meta : $variation_meta; } return $meta; }
/** * Get the attributes for a product or product variation * * @since 2.1 * @param WC_Product|WC_Product_Variation $product * @return array */ private function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // variation attributes foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` $attributes[] = array('name' => ucwords(str_replace('attribute_', '', str_replace('pa_', '', $attribute_name))), 'option' => $attribute); } } else { foreach ($product->get_attributes() as $attribute) { $attributes[] = array('name' => ucwords(str_replace('pa_', '', $attribute['name'])), 'position' => $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } } return $attributes; }
/** * Adjust discounted product price HTML * * @since 1.3.0 * @param string $html * @param WC_Product $product * @return float|string */ public function on_price_html($html, $product) { /** * Controls whether or not member prices should use discount format when displayed * * @since 1.3.0 * @param bool $use_discount_format Defaults to true */ if (!apply_filters('wc_memberships_member_prices_use_discount_format', true)) { return $html; } $tax_display_mode = get_option('woocommerce_tax_display_shop'); $this->disable_price_adjustments(); $base_price = 'incl' == $tax_display_mode ? $product->get_price_including_tax() : $product->get_price_excluding_tax(); $product_id = $product->is_type('variation') ? $product->variation_id : $product->id; $this->enable_price_adjustments(); if (!$this->has_discounted_price($base_price, $product_id)) { return $html; } /** * Controls whether or not member prices should display sale prices as well * * @since 1.3.0 * @param bool $display_sale_price Defaults to false */ $display_sale_price = apply_filters('wc_memberships_member_prices_display_sale_price', false); add_filter('woocommerce_get_variation_prices_hash', array($this, 'nonmember_variation_prices_hash'), 10, 3); if (!$display_sale_price) { add_filter('woocommerce_product_is_on_sale', array($this, 'disable_sale_price')); } $this->disable_price_adjustments(); $this->disable_price_html_adjustments(); $_html = $product->get_price_html(); $this->enable_price_adjustments(); $this->enable_price_html_adjustments(); remove_filter('woocommerce_get_variation_prices_hash', array($this, 'nonmember_variation_prices_hash')); if (!$display_sale_price) { remove_filter('woocommerce_product_is_on_sale', array($this, 'disable_sale_price')); } if ($html != $_html) { $html = '<del>' . $_html . '</del> <ins> ' . $html . '</ins>'; } return $html; }
/** * Bundle Containers need no processing - let it be decided by bundled items only. * * @param boolean $is_needed product needs processing: true/false * @param WC_Product $product the product * @param int $order_id the order id * @return boolean modified product needs processing status */ function woo_bundles_container_items_need_no_processing($is_needed, $product, $order_id) { if ($product->is_type('bundle')) { return false; } return $is_needed; }
/** * Add a product to the cart * * @param string product_id contains the id of the product to add to the cart * @param string quantity contains the quantity of the item to add * @param int variation_id * @param array variation attribute values */ function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '') { global $woocommerce; if ($quantity < 1) { return false; } // Load cart item data - may be added by other plugins $cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', array(), $product_id); // Generate a ID based on product ID, variation ID, variation data, and other cart item data $cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data); // See if this product and its options is already in the cart $cart_item_key = $this->find_product_in_cart($cart_id); if ($variation_id > 0) { $product_data = new WC_Product_Variation($variation_id); } else { $product_data = new WC_Product($product_id); } // Type/Exists check if ($product_data->is_type('external') || !$product_data->exists()) { $woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce')); return false; } // Price set check if ($product_data->get_price() === '') { $woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce')); return false; } // Stock check - only check if we're managing stock and backorders are not allowed if (!$product_data->has_enough_stock($quantity)) { $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity())); return false; } elseif (!$product_data->is_in_stock()) { $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce')); return false; } if ($cart_item_key) { $quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity']; // Stock check - this time accounting for whats already in-cart if (!$product_data->has_enough_stock($quantity)) { $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock and you already have %s in your cart.', 'woocommerce'), $product_data->get_stock_quantity(), $this->cart_contents[$cart_item_key]['quantity'])); return false; } elseif (!$product_data->is_in_stock()) { $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce')); return false; } $this->set_quantity($cart_item_key, $quantity); } else { // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item $this->cart_contents[$cart_id] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data))); } $this->set_session(); return true; }
function get_form($options) { global $woocommerce; $product = null; if (function_exists('get_product')) { $product = get_product($this->product_id); } else { $product = new WC_Product($this->product_id); } extract(shortcode_atts(array('display_title' => true, 'display_description' => true, 'display_inactive' => false, 'field_values' => false, 'ajax' => false, 'tabindex' => 1, 'label_subtotal' => __('Subtotal', 'wc_gf_addons'), 'label_options' => __('Options', 'wc_gf_addons'), 'label_total' => __('Total', 'wc_gf_addons'), 'disable_label_subtotal' => 'no', 'disable_label_options' => 'no', 'disable_label_total' => 'no', 'disable_calculations' => 'no'), $options)); //Get the form meta so we can make sure the form exists. $form_meta = RGFormsModel::get_form_meta($this->form_id); if (!empty($form_meta)) { if (!empty($_POST)) { $_POST['gform_submit'] = isset($_POST['gform_old_submit']) ? $_POST['gform_old_submit'] : ''; $_POST['gform_old_submit'] = $_POST['gform_submit']; } $form = RGForms::get_form($this->form_id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex); unset($_POST['gform_submit']); $form = str_replace('</form>', '', $form); $form = str_replace('gform_submit', 'gform_old_submit', $form); $this->current_page = GFFormDisplay::get_current_page($this->form_id); $this->next_page = $this->current_page + 1; $this->previous_page = $this->current_page - 1; $this->next_page = $this->next_page > $this->get_max_page_number($form_meta) ? 0 : $this->next_page; if ($product->product_type == 'variable' || $product->product_type == 'variable-subscription') { echo '<div class="gform_variation_wrapper gform_wrapper single_variation_wrap">'; } else { echo '<div class="gform_variation_wrapper gform_wrapper">'; } if ($product->is_type('variable')) { //echo '<input type="hidden" name="add-to-cart" value="variation" />'; echo '<input type="hidden" id="product_id" name="product_id" value="' . $this->product_id . '" />'; } elseif ($product->has_child()) { //echo '<input type="hidden" name="add-to-cart" value="group" />'; echo '<input type="hidden" id="product_id" name="product_id" value="' . $this->product_id . '" />'; } else { //echo '<input type="hidden" name="add-to-cart" value="' . $this->product_id . '" />'; echo '<input type="hidden" id="product_id" name="product_id" value="' . $this->product_id . '" />'; } if (wc_is_21x()) { wp_nonce_field('add_to_cart'); } else { $woocommerce->nonce_field('add_to_cart'); } echo '<a id="_form_' . $this->form_id . '" href="#_form_' . $this->form_id . '" class="gform_anchor"></a>'; echo $form; echo '<input type="hidden" name="gform_form_id" id="gform_form_id" value="' . $this->form_id . '" />'; echo '<input type="hidden" id="woocommerce_get_action" value="" />'; echo '<input type="hidden" id="woocommerce_product_base_price" value="' . $product->get_price() . '" />'; $description_class = rgar($form_meta, "descriptionPlacement") == "above" ? "description_above" : "description_below"; ?> <?php $this->on_print_scripts(); if ($disable_calculations == 'no') { ?> <div class="product_totals"> <ul id="gform_totals_<?php echo $this->form_id; ?> " class="gform_fields <?php echo $form_meta['labelPlacement'] . ' ' . $description_class; ?> "> <li class="gfield" <?php if ($disable_label_subtotal == 'yes') { echo 'style="display:none;"'; } ?> > <label class="gfield_label"><?php echo $label_subtotal; ?> </label> <div class="ginput_container"> <span class="formattedBasePrice ginput_total"></span> </div> </li> <li class="gfield" <?php if ($disable_label_options == 'yes') { echo 'style="display:none;"'; } ?> > <label class="gfield_label"><?php echo $label_options; ?> </label> <div class="ginput_container"> <span class="formattedVariationTotal ginput_total"></span> </div> </li> <li class="gfield" <?php if ($disable_label_total == 'yes') { echo 'style="display:none;"'; } ?> > <label class="gfield_label"><?php echo $label_total; ?> </label> <div class="ginput_container"> <span class="formattedTotalPrice ginput_total"></span> </div> </li> </ul> </div> <style> .single_variation .price { display:none !important; } </style> <?php } ?> <style> .hidden-total { display:none !important; } </style> <?php echo '</div>'; } }
/** * Add a product line item to the order. This is the only line item type with * it's own method because it saves looking up order amounts (costs are added up for you). * @param \WC_Product $product * @param int $qty * @param array $args * @return int order item ID * @throws WC_Data_Exception */ public function add_product($product, $qty = 1, $args = array()) { if ($product) { $default_args = array('name' => $product->get_name(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(), 'variation_id' => $product->is_type('variation') ? $product->get_id() : 0, 'variation' => $product->is_type('variation') ? $product->get_attributes() : array(), 'subtotal' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'total' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'quantity' => $qty); } else { $default_args = array('quantity' => $qty); } $args = wp_parse_args($args, $default_args); // BW compatibility with old args if (isset($args['totals'])) { foreach ($args['totals'] as $key => $value) { if ('tax' === $key) { $args['total_tax'] = $value; } elseif ('tax_data' === $key) { $args['taxes'] = $value; } else { $args[$key] = $value; } } } $item = new WC_Order_Item_Product(); $item->set_props($args); $item->set_backorder_meta(); $item->set_order_id($this->get_id()); $item->save(); $this->add_item($item); wc_do_deprecated_action('woocommerce_order_add_product', array($this->get_id(), $item->get_id(), $product, $qty, $args), '2.7', 'Use woocommerce_new_order_item action instead.'); return $item->get_id(); }
/** * Prepare links for the request. * * @param WC_Product $product Product object. * @return array Links for the given product. */ protected function prepare_links($product) { $links = array('self' => array('href' => rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $product->id))), 'collection' => array('href' => rest_url(sprintf('/%s/%s', $this->namespace, $this->rest_base)))); if ($product->is_type('variation') && $product->parent) { $links['up'] = array('href' => rest_url(sprintf('/%s/products/%d', $this->namespace, $product->parent->id))); } elseif ($product->is_type('simple') && !empty($product->post->post_parent)) { $links['up'] = array('href' => rest_url(sprintf('/%s/products/%d', $this->namespace, $product->post->post_parent))); } return $links; }
/** * Add a product to the cart * * @param string $product_id contains the id of the product to add to the cart * @param string $quantity contains the quantity of the item to add * @param int $variation_id * @param array $variation attribute values * @param array $cart_item_data extra cart item data we want to pass into the item * @return bool */ function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array()) { global $woocommerce; if ($quantity < 1) { return false; } // Load cart item data - may be added by other plugins $cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', $cart_item_data, $product_id); // Generate a ID based on product ID, variation ID, variation data, and other cart item data $cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data); // See if this product and its options is already in the cart $cart_item_key = $this->find_product_in_cart($cart_id); if ($variation_id > 0) { $product_data = new WC_Product_Variation($variation_id); } else { $product_data = new WC_Product($product_id); } // Force quantity to 1 if sold individually if ($product_data->is_sold_individually()) { $quantity = 1; } // Type/Exists check if ($product_data->is_type('external') || !$product_data->exists()) { $woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce')); return false; } // Price set check if ($product_data->get_price() === '') { $woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce')); return false; } // Stock check - only check if we're managing stock and backorders are not allowed if (!$product_data->has_enough_stock($quantity)) { $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity())); return false; } elseif (!$product_data->is_in_stock()) { $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce')); return false; } // Downloadable/virtual qty check if ($product_data->is_sold_individually()) { $in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] + $quantity : $quantity; // If its greater than 1, its already in the cart if ($in_cart_quantity > 1) { $woocommerce->add_error(sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('You already have this item in your cart.', 'woocommerce'))); return false; } } // Stock check - this time accounting for whats already in-cart $product_qty_in_cart = $this->get_cart_item_quantities(); if ($product_data->managing_stock()) { // Variations if ($variation_id && $product_data->variation_has_stock) { if (isset($product_qty_in_cart[$variation_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$variation_id] + $quantity)) { $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$variation_id])); return false; } // Products } else { if (isset($product_qty_in_cart[$product_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$product_id] + $quantity)) { $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$product_id])); return false; } } } // If cart_item_key is set, the item is already in the cart if ($cart_item_key) { $new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity']; $this->set_quantity($cart_item_key, $new_quantity); } else { $cart_item_key = $cart_id; // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item $this->cart_contents[$cart_item_key] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)), $cart_item_key); } do_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data); $woocommerce->cart_has_contents_cookie(true); $this->set_session(); return true; }
/** * Get the attributes for a product or product variation. * * @param WC_Product|WC_Product_Variation $product Product instance. * @return array */ protected function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // Variation attributes. foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { $name = str_replace('attribute_', '', $attribute_name); if (!$attribute) { continue; } // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`. if (0 === strpos($attribute_name, 'attribute_pa_')) { $option_term = get_term_by('slug', $attribute, $name); $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $option_term && !is_wp_error($option_term) ? $option_term->name : $attribute); } else { $attributes[] = array('id' => 0, 'name' => $name, 'option' => $attribute); } } } else { foreach ($product->get_attributes() as $attribute) { if ($attribute['is_taxonomy']) { $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($attribute['name']), 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } else { $attributes[] = array('id' => 0, 'name' => $attribute['name'], 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } } } return $attributes; }
/** * Display the form associated to the product */ public function display_form() { global $post, $woocommerce; if ($this->apply_addon()) { $product = null; if (function_exists('get_product')) { $product = get_product($post->ID); } else { $product = new WC_Product($post->ID); } $form_content = cp_calculatedfieldsf_filter_content($this->form); // Initialize form fields if (!empty($_SESSION['cp_cff_form_data']) && !empty($_REQUEST['cp_calculatedfieldsf_id']) && !empty($_REQUEST['cp_calculatedfieldsf_pform_psequence'])) { global $wpdb; $result = $wpdb->get_row($wpdb->prepare("SELECT form_data.paypal_post AS paypal_post FROM " . CP_CALCULATEDFIELDSF_POSTS_TABLE_NAME . " AS form_data WHERE form_data.id=%d AND form_data.formid=%d", $_SESSION['cp_cff_form_data'], $_REQUEST['cp_calculatedfieldsf_id'])); if (!is_null($result)) { $arr = array(); $submitted_data = unserialize($result->paypal_post); foreach ($submitted_data as $key => $val) { if (preg_match('/^fieldname\\d+$/', $key)) { $arr[$key . $_REQUEST['cp_calculatedfieldsf_pform_psequence']] = $val; } } ?> <script> cpcff_default = ( typeof cpcff_default != 'undefined' ) ? cpcff_default : {}; cpcff_default[ 'form_structure<?php echo $_REQUEST['cp_calculatedfieldsf_pform_psequence']; ?> ' ] = <?php echo json_encode($arr); ?> ; </script> <?php } } unset($_SESSION['cp_cff_form_data']); // Remove the form tags if (preg_match('/<form[^>]*>/', $form_content, $match)) { $form_content = str_replace($match[0], '', $form_content); $form_content = preg_replace('/<\\/form>/', '', $form_content); } $tmp = get_post_meta($post->ID, 'woocommerce_cpcff_calculate_price', true); $request_cost = !empty($tmp) ? cp_calculatedfieldsf_get_option('request_cost', false, $this->form['id']) : false; echo '<div class="cpcff-woocommerce-wrapper">' . $form_content . (method_exists($woocommerce, 'nonce_field') ? $woocommerce->nonce_field('add_to_cart') : '') . '<input type="hidden" name="woocommerce_cpcff_product" value="' . $post->ID . '" />' . ($request_cost ? '<input type="hidden" name="woocommerce_cpcff_field" value="' . $request_cost . '" /><input type="hidden" name="woocommerce_cpcff_form" value="' . $this->form['id'] . '">' : '') . '</div>'; $add_to_cart_value = ''; if ($product->is_type('variable')) { $add_to_cart_value = 'variation'; } elseif ($product->has_child()) { $add_to_cart_value = 'group'; } else { $add_to_cart_value = $product->id; } if (!function_exists('get_product')) { //1.x only if (method_exists($woocommerce, 'nonce_field')) { $woocommerce->nonce_field('add_to_cart'); } echo '<input type="hidden" name="add-to-cart" value="' . $add_to_cart_value . '" />'; } else { echo '<input type="hidden" name="add-to-cart" value="' . $post->ID . '" />'; } } echo '<div class="clear"></div>'; }
/** * Given a product ID & variations, find the correct variation ID to use for * calculation. We can't just trust input from the CLI to pass a variation_id * manually, otherwise you could pass the cheapest variation ID but provide * other information so we have to look up the variation ID. * * @since 2.5.0 * @param WC_Product $product Product instance * @return int Returns an ID if a valid variation was found for this product */ protected function get_variation_id($product, $variations = array()) { $variation_id = null; $variations_normalized = array(); if ($product->is_type('variable') && $product->has_child()) { if (isset($variations) && is_array($variations)) { // start by normalizing the passed variations foreach ($variations as $key => $value) { $key = str_replace('attribute_', '', str_replace('pa_', '', $key)); // from get_attributes in class-wc-api-products.php $variations_normalized[$key] = strtolower($value); } // now search through each product child and see if our passed variations match anything foreach ($product->get_children() as $variation) { $meta = array(); foreach (get_post_meta($variation) as $key => $value) { $value = $value[0]; $key = str_replace('attribute_', '', str_replace('pa_', '', $key)); $meta[$key] = strtolower($value); } // if the variation array is a part of the $meta array, we found our match if ($this->array_contains($variations_normalized, $meta)) { $variation_id = $variation; break; } } } } return $variation_id; }
function woocommerce_gravityform() { global $post, $woocommerce; include_once 'gravityforms-product-addons-form.php'; $gravity_form_data = get_post_meta($post->ID, '_gravity_form_data', true); if (is_array($gravity_form_data) && $gravity_form_data['id']) { $product = null; if (function_exists('get_product')) { $product = get_product($post->ID); } else { $product = new WC_Product($post->ID); } $product_form = new woocommerce_gravityforms_product_form($gravity_form_data['id'], $post->ID); $product_form->get_form($gravity_form_data); $add_to_cart_value = ''; if ($product->is_type('variable')) { $add_to_cart_value = 'variation'; } elseif ($product->has_child()) { $add_to_cart_value = 'group'; } else { $add_to_cart_value = $product->id; } if (!function_exists('get_product')) { //1.x only $woocommerce->nonce_field('add_to_cart'); echo '<input type="hidden" name="add-to-cart" value="' . $add_to_cart_value . '" />'; } else { echo '<input type="hidden" name="add-to-cart" value="' . $post->ID . '" />'; } } echo '<div class="clear"></div>'; }
function woocommerce_admin_product_bulk_edit_save($post_id, $post) { if (is_int(wp_is_post_revision($post_id))) { return; } if (is_int(wp_is_post_autosave($post_id))) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if (!isset($_REQUEST['woocommerce_bulk_edit_nonce']) || isset($_REQUEST['woocommerce_bulk_edit_nonce']) && !wp_verify_nonce($_REQUEST['woocommerce_bulk_edit_nonce'], 'woocommerce_bulk_edit_nonce')) { return $post_id; } if (!current_user_can('edit_post', $post_id)) { return $post_id; } if ($post->post_type != 'product') { return $post_id; } global $woocommerce, $wpdb; $product = new WC_Product($post_id); // Save fields if (isset($_REQUEST['change_weight']) && $_REQUEST['change_weight'] == 1) { if (isset($_REQUEST['_weight'])) { update_post_meta($post_id, '_weight', esc_html(stripslashes($_REQUEST['_weight']))); } } if (isset($_REQUEST['change_dimensions']) && $_REQUEST['change_dimensions'] == 1) { if (isset($_REQUEST['_length'])) { update_post_meta($post_id, '_length', esc_html(stripslashes($_REQUEST['_length']))); } if (isset($_REQUEST['_width'])) { update_post_meta($post_id, '_width', esc_html(stripslashes($_REQUEST['_width']))); } if (isset($_REQUEST['_height'])) { update_post_meta($post_id, '_height', esc_html(stripslashes($_REQUEST['_height']))); } } if (isset($_REQUEST['_stock_status']) && $_REQUEST['_stock_status']) { update_post_meta($post_id, '_stock_status', stripslashes($_REQUEST['_stock_status'])); } if (isset($_REQUEST['_visibility']) && $_REQUEST['_visibility']) { update_post_meta($post_id, '_visibility', stripslashes($_REQUEST['_visibility'])); } if (isset($_REQUEST['_featured']) && $_REQUEST['_featured']) { update_post_meta($post_id, '_featured', stripslashes($_REQUEST['_featured'])); } // Handle price - remove dates and set to lowest if ($product->is_type('simple') || $product->is_type('external')) { $price_changed = false; if (isset($_REQUEST['change_regular_price']) && $_REQUEST['change_regular_price'] == 1) { if (isset($_REQUEST['_regular_price'])) { update_post_meta($post_id, '_regular_price', stripslashes($_REQUEST['_regular_price'])); } if (isset($_REQUEST['_regular_price']) && stripslashes($_REQUEST['_regular_price']) != $product->regular_price) { $price_changed = true; $product->regular_price = stripslashes($_REQUEST['_regular_price']); } } if (isset($_REQUEST['change_sale_price']) && $_REQUEST['change_sale_price'] == 1) { if (isset($_REQUEST['_sale_price'])) { update_post_meta($post_id, '_sale_price', stripslashes($_REQUEST['_sale_price'])); } if (isset($_REQUEST['_sale_price']) && stripslashes($_REQUEST['_sale_price']) != $product->sale_price) { $price_changed = true; $product->sale_price = stripslashes($_REQUEST['_sale_price']); } } if ($price_changed) { update_post_meta($post_id, '_sale_price_dates_from', ''); update_post_meta($post_id, '_sale_price_dates_to', ''); if ($product->sale_price) { update_post_meta($post_id, '_price', $product->sale_price); } else { update_post_meta($post_id, '_price', $product->regular_price); } } } // Handle stock if (!$product->is_type('grouped')) { if (isset($_REQUEST['_manage_stock']) && $_REQUEST['_manage_stock']) { if ($_REQUEST['_manage_stock'] == 'yes') { update_post_meta($post_id, '_manage_stock', 'yes'); if (isset($_REQUEST['change_stock']) && $_REQUEST['change_stock'] == 1) { update_post_meta($post_id, '_stock', (int) $_REQUEST['_stock']); } } else { update_post_meta($post_id, '_manage_stock', 'no'); update_post_meta($post_id, '_stock', '0'); } } } // Clear transient $woocommerce->clear_product_transients($post_id); }
/** * Get the min/max quantity range for this given product. At least, do * the best we can. The issue is that this is controlled ultimately by * template files, which could be changed by the user/theme. * * @see woocommerce-template.php woocommerce_quantity_input() * @see woocommerce/templates/single-product/add-to-cart/simple.php * @see woocommerce/templates/single-product/add-to-cart/variable.php * * @since 3.0 * @param WC_Product $product the product * @return array associative array with keys 'min_value' and 'max_value' */ public static function get_quantity_range($product) { // get the quantity min/max for this product $defaults = array('input_name' => 'quantity', 'input_value' => '1', 'max_value' => '', 'min_value' => '0'); $args = array(); if ($product->is_type('simple')) { $args = array('min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity()); } return apply_filters('woocommerce_quantity_input_args', wp_parse_args($args, $defaults)); }
/** * Apply the shop loop add to cart button text customization * * @since 1.2.0 * @param string $text add to cart text * @param WC_Product $product product object * @return string modified add to cart text */ public function customize_add_to_cart_text($text, $product) { // out of stock add to cart text if (isset($this->filters['out_of_stock_add_to_cart_text']) && !$product->is_in_stock()) { return $this->filters['out_of_stock_add_to_cart_text']; } if (isset($this->filters['add_to_cart_text']) && $product->is_type('simple')) { // simple add to cart text return $this->filters['add_to_cart_text']; } elseif (isset($this->filters['variable_add_to_cart_text']) && $product->is_type('variable')) { // variable add to cart text return $this->filters['variable_add_to_cart_text']; } elseif (isset($this->filters['grouped_add_to_cart_text']) && $product->is_type('grouped')) { // grouped add to cart text return $this->filters['grouped_add_to_cart_text']; } elseif (isset($this->filters['external_add_to_cart_text']) && $product->is_type('external')) { // external add to cart text return $this->filters['external_add_to_cart_text']; } return $text; }
/** * Get the order data format for a single column per line item, compatible with the CSV Import Suite plugin * * @since 3.0 * @param array $order_data an array of order data for the given order * @param WC_Order $order the WC_Order object * @return array modified order data */ private function get_import_one_column_per_line_item($order_data, WC_Order $order) { $count = 1; // add line items foreach ($order->get_items() as $_ => $item) { // sku/qty/price $product = $order->get_product_from_item($item); if (!is_object($product)) { $product = new WC_Product(0); } $sku = $product->get_sku(); // note that product ID must be prefixed with `product_id:` so the importer can properly parse it vs. the SKU $product_id = $product->is_type('variation') ? $product->variation_id : $product->id; $line_item = array($sku ? $sku : "product_id:{$product_id}", $item['qty'], $order->get_line_total($item)); // Add item meta $item_meta = new WC_Order_Item_Meta(SV_WC_Plugin_Compatibility::is_wc_version_gte_2_4() ? $item : $item['item_meta']); $formatted_meta = $item_meta->get_formatted(); if (!empty($formatted_meta)) { foreach ($formatted_meta as $meta_key => $meta) { // remove newlines $label = str_replace(array("\r", "\r\n", "\n"), '', $meta['label']); $value = str_replace(array("\r", "\r\n", "\n"), '', $meta['value']); // escape reserved chars (:;|) $label = str_replace(array(': ', ':', ';', '|'), array('\\: ', '\\:', '\\;', '\\|'), $meta['label']); $value = str_replace(array(': ', ':', ';', '|'), array('\\: ', '\\:', '\\;', '\\|'), $meta['value']); $line_item[] = wp_kses_post($label . ': ' . $value); } } $order_data["order_item_{$count}"] = implode('|', $line_item); $count++; } $count = 1; foreach ($order->get_items('shipping') as $_ => $shipping_item) { $order_data["shipping_method_{$count}"] = $shipping_item['method_id']; $order_data["shipping_cost_{$count}"] = wc_format_decimal($shipping_item['cost'], 2); $count++; } // fix order numbers $order_data['order_id'] = $order->id; $order_data['order_number_formatted'] = get_post_meta($order->id, '_order_number_formatted', true); $order_data['order_number'] = get_post_meta($order->id, '_order_number', true); // fix customer user $user = new WP_User($order_data['customer_id']); $order_data['customer_id'] = $user->user_email; return $order_data; }
/** * Get the attributes for a product or product variation * * @since 2.1 * @param WC_Product|WC_Product_Variation $product * @return array */ private function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // variation attributes foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` $attributes[] = array('name' => wc_attribute_label(str_replace('attribute_', '', $attribute_name)), 'slug' => str_replace('attribute_', '', str_replace('pa_', '', $attribute_name)), 'option' => $attribute); } } else { foreach ($product->get_attributes() as $attribute) { // taxonomy-based attributes are comma-separated, others are pipe (|) separated if ($attribute['is_taxonomy']) { $options = explode(',', $product->get_attribute($attribute['name'])); } else { $options = explode('|', $product->get_attribute($attribute['name'])); } $attributes[] = array('name' => wc_attribute_label($attribute['name']), 'slug' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', $options)); } } return $attributes; }
/** * Overrides the default woocommerce is on sale to ensure sale badges show properly. * @since 2.10.8 * @param bool $is_on_sale * @param WC_Product $product * @return bool */ public function on_get_product_is_on_sale($is_on_sale, $product) { if ($is_on_sale) { return $is_on_sale; } if ($product->is_type('variable')) { $is_on_sale = false; $prices = $product->get_variation_prices(); $regular = array_map('strval', $prices['regular_price']); $actual_prices = array_map('strval', $prices['price']); $diff = array_diff_assoc($regular, $actual_prices); if (!empty($diff)) { $is_on_sale = true; } } else { $dynamic_price = $this->on_get_price($product->price, $product, true); $regular_price = $product->get_regular_price(); if (empty($regular_price) || empty($dynamic_price)) { return $is_on_sale; } else { $is_on_sale = $regular_price != $dynamic_price; } } return $is_on_sale; }