本文整理汇总了PHP中WC_Product_Variable::get_available_variations方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variable::get_available_variations方法的具体用法?PHP WC_Product_Variable::get_available_variations怎么用?PHP WC_Product_Variable::get_available_variations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variable
的用法示例。
在下文中一共展示了WC_Product_Variable::get_available_variations方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_product_attr
private function get_product_attr($postID)
{
global $woocommerce;
$return = array();
$product = new WC_Product_Variable($postID);
#Step 1: Get product varations
$available_variations = $product->get_available_variations();
#Step 2: Get product variation id
$variation_id = $available_variations[0]['variation_id'];
// Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
#Step 3: Create the variable product object
$variable_product1 = new WC_Product_Variation($variation_id);
#Step 4: You have the data. Have fun :)
$return['regular_price'] = get_post_meta($postID, '_regular_price', true);
return $return;
}
开发者ID:VanessaKing,项目名称:xox-woocommerce-slidercarousel,代码行数:16,代码来源:class-xox-woo-carousel-public.php
示例2: duplicate
/**
* Handle variation duplicate
*
* @return boolean false if the from product contains no variatoins
*/
public function duplicate()
{
$fromVariation = $this->from->get_available_variations();
if (empty($fromVariation)) {
return false;
}
if ($this->to->id === $this->from->id) {
/*
* In such a case just add the duplicate meta key
*/
foreach ($fromVariation as $variation) {
if (!metadata_exists('post', $variation['variation_id'], self::DUPLICATE_KEY)) {
update_post_meta($variation['variation_id'], self::DUPLICATE_KEY, $variation['variation_id']);
}
}
} else {
/* This could be a very long operation */
set_time_limit(0);
foreach ($fromVariation as $variation) {
/*
* First we check if the "to" product contains the duplicate meta
* key to find out if we have to update or insert
*/
$posts = get_posts(array('meta_key' => self::DUPLICATE_KEY, 'meta_value' => $variation['variation_id'], 'post_type' => 'product_variation', 'post_parent' => $this->to->id));
switch (count($posts)) {
case 1:
// update
$this->update(wc_get_product($variation['variation_id']), $posts[0], $variation);
break;
case 0:
// insert
$this->insert(wc_get_product($variation['variation_id']), $variation);
break;
default:
// we can not handle, something wrong here
break;
}
}
/* Restore original timeout */
set_time_limit(ini_get('max_execution_time'));
}
}
示例3: filter_custom_fields
/**
* Add woo attributes to a custom field with the same name
*
* @param $custom_fields
* @param $post_id
*
* @return mixed
*/
public function filter_custom_fields($custom_fields, $post_id)
{
if (!isset($custom_fields)) {
$custom_fields = array();
}
// Get the product correponding to this post
$product = wc_get_product($post_id);
if (false === $product) {
// Not a product
return $custom_fields;
}
switch ($product->get_type()) {
case self::PRODUCT_TYPE_VARIABLE:
$product_variable = new WC_Product_Variable($product);
foreach ($product_variable->get_available_variations() as $variation_array) {
foreach ($variation_array['attributes'] as $attribute_name => $attribute_value) {
if (!isset($custom_fields[$attribute_name])) {
$custom_fields[$attribute_name] = array();
}
if (!in_array($attribute_value, $custom_fields[$attribute_name], true)) {
array_push($custom_fields[$attribute_name], $attribute_value);
}
}
}
break;
default:
foreach ($product->get_attributes() as $attribute) {
//$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
// Remove the eventual 'pa_' prefix from the global attribute name
$attribute_name = $attribute['name'];
if (substr($attribute_name, 0, 3) === 'pa_') {
$attribute_name = substr($attribute_name, 3, strlen($attribute_name));
}
$custom_fields[$attribute_name] = explode(',', $product->get_attribute($attribute['name']));
}
break;
}
return $custom_fields;
}
示例4: extract
//.........这里部分代码省略.........
$display_type = '';
}
if (!isset($columns)) {
$columns = '4';
}
if (isset($per_page)) {
$post_count = $per_page;
}
if (isset($number)) {
$post_count = $number;
}
if (!isset($order)) {
$order = 'ASC';
}
if (!isset($orderby)) {
$orderby = 'date';
}
if (!isset($category)) {
$category = '';
}
if (!isset($ids)) {
$ids = '';
}
if ($ids) {
$ids = explode(',', $ids);
$ids = array_map('trim', $ids);
}
if ($columns == "2") {
$columns = 6;
} elseif ($columns == "3") {
$columns = 4;
} elseif ($columns == "4") {
$columns = 3;
}
$meta_query = '';
if ($display_type == "recent_products") {
$meta_query = WC()->query->get_meta_query();
}
if ($display_type == "featured_products") {
$meta_query = array(array('key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'), array('key' => '_featured', 'value' => 'yes'));
}
if ($display_type == "top_rated_products") {
add_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses'));
$meta_query = WC()->query->get_meta_query();
}
$args = array('post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $post_count, 'orderby' => $orderby, 'order' => $order, 'meta_query' => $meta_query);
if ($display_type == "sale_products") {
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$args['meta_query'] = $meta_query;
$args['post__in'] = $product_ids_on_sale;
}
if ($display_type == "best_selling_products") {
$args['meta_key'] = 'total_sales';
$args['orderby'] = 'meta_value_num';
$args['meta_query'] = array(array('key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'));
}
if ($display_type == "product_category") {
$args['tax_query'] = array(array('taxonomy' => 'product_cat', 'terms' => array(esc_attr($category)), 'field' => 'slug', 'operator' => 'IN'));
}
if ($display_type == "product_categories") {
$args['tax_query'] = array(array('taxonomy' => 'product_cat', 'terms' => $ids, 'field' => 'term_id', 'operator' => 'IN'));
}
$query = new WP_Query($args);
$output .= '<ul class="wcmp-product-list wcmp-img-' . $img_position . ' ' . $order . '">';
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$product_id = get_the_ID();
$post = get_post($product_id);
$product_title = get_the_title();
$product = new WC_Product($product_id);
$attachment_ids = $product->get_gallery_attachment_ids();
$price = $product->get_price_html();
$rating = $product->get_rating_html();
$product_var = new WC_Product_Variable($product_id);
$available_variations = $product_var->get_available_variations();
$output .= '<li>';
$output .= '<a href="' . get_permalink($product_id) . '">';
$product_img = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'full');
$output .= '<img style="' . $style . '" src="' . $product_img[0] . '"/>';
$output .= '<span style="' . $title_style . '">' . $product_title . '</span>';
$output .= '</a>';
if ($display_type == "top_rated_products") {
$output .= '<div style="' . $rating_style . '">' . $rating . '</div>';
}
$output .= '<span class="amount" style="' . $pricing_style . '">' . $price . '</span>';
$output .= '</li>';
}
}
$output .= "\n" . '</ul>';
$output .= "\n" . '</div>';
if ($display_type == "top_rated_products") {
remove_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses'));
}
wp_reset_postdata();
return $output;
}
示例5: idwc_payment_vars
function idwc_payment_vars($item, $status, $order_id, $qty_num)
{
$order = new WC_Order($order_id);
if (isset($item['product_id'])) {
$product_id = $item['product_id'];
}
if (isset($item['variation_id'])) {
$variation_id = $item['variation_id'];
}
if (isset($product_id)) {
$project_id = get_post_meta($product_id, '_wc_project_pairing', true);
if (isset($project_id)) {
$product = new WC_Product_Variable($product_id);
if (!empty($product)) {
$variations = $product->get_available_variations();
$v_array = array();
foreach ($variations as $variant) {
$v_array[] = $variant['variation_id'];
}
$level = array_search($variation_id, $v_array) + 1;
}
$first_name = get_post_meta($order_id, '_billing_first_name', true);
$last_name = get_post_meta($order_id, '_billing_last_name', true);
$email = get_post_meta($order_id, '_billing_email', true);
$address = get_post_meta($order_id, '_billing_address_1', true);
$city = get_post_meta($order_id, '_billing_city', true);
$state = get_post_meta($order_id, '_billing_state', true);
$zip = get_post_meta($order_id, '_billing_postcode', true);
$country = get_post_meta($order_id, '_billing_country', true);
$transaction_id = get_post_meta($order_id, '_order_key', true);
$price = get_post_meta($variation_id, '_price', true);
$date = $order->order_date;
$vars = array('id' => null, 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'address' => $address, 'state' => $state, 'city' => $city, 'zip' => $zip, 'country' => $country, 'product_id' => $project_id, 'transaction_id' => $transaction_id . '-v' . $variation_id . '-' . $qty_num, 'preapproval_key' => '', 'product_level' => $level, 'prod_price' => $price, 'status' => $status, 'created_at' => $date);
}
}
return isset($vars) ? $vars : array();
}
开发者ID:christopherreay,项目名称:freeFreeCrowdfunding_ignitiondeck_crowdfunding,代码行数:37,代码来源:ignitiondeck-idf.php
示例6: get_product_from_post
//.........这里部分代码省略.........
$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();
示例7: widget
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance)
{
?>
<div class="checkout_summary">
<div class="before_widget">
<?php
echo $args['before_widget'];
?>
</div>
<div class="before_title">
<?php
echo $args['before_title'];
?>
</div>
<div class="summary_title">
<?php
echo $instance['title'];
?>
</div>
<div class="after_title">
<?php
echo $args['after_title'];
?>
</div>
<div class="csw_content">
<!-- Widget content start from here -->
<?php
global $woocommerce, $current_user, $product, $post;
if (sizeof($woocommerce->cart->get_cart()) > 0) {
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$product = $values['data'];
// $attributes = $product->get_attributes();
// $attributes= array_map( 'trim', explode( '|', $attributes['servings']['value'] ) );
//show thumbnail
$thumbnail = apply_filters('woocommerce_in_cart_product_thumbnail', $product->get_image(), $values, $cart_item_key);
if (!$product->is_visible() || !empty($product->variation_id) && !$product->parent_is_visible()) {
echo $thumbnail;
} else {
printf('<a href="%s">%s</a>', esc_url(get_permalink(apply_filters('woocommerce_in_cart_product_id', $values['product_id']))), $thumbnail);
}
//end Thumbnail
if (!$product->is_visible() || !empty($product->variation_id) && !$product->parent_is_visible()) {
echo apply_filters('woocommerce_in_cart_product_title', $product->get_title(), $values, $cart_item_key);
} else {
printf('<a class="product-title" href="%s">%s</a>', esc_url(get_permalink(apply_filters('woocommerce_in_cart_product_id', $values['product_id']))), apply_filters('woocommerce_in_cart_product_title', $product->get_title(), $values, $cart_item_key));
}
$product_id = $product->parent->id ? $product->parent->id : $product->id;
$object = new WC_Product_Variable($product_id);
$cardDetails = $woocommerce->cart->get_cart();
foreach ($cardDetails as $cDK => $cDV) {
$pType = $cDV['data']->parent->product_type ? $cDV['data']->parent->product_type : $cDV['data']->product_type;
break;
}
$attributes = $object->get_available_variations();
foreach ($attributes as $name => $option) {
if ('variable-subscription' == $pType) {
echo '<div class="plan_content">';
echo '<div class="plan_option">';
}
$checked = "";
$single_f_vari = $product->variation_data['attribute_servings'];
$html = strip_tags($option['price_html']);
if ($product->variation_data['attribute_servings'] == $option['attributes']['attribute_servings']) {
$checked = "checked=\"checked\"";
}
if ('variable-subscription' == $pType) {
echo '<input type="radio" ' . $checked . ' vari_id="' . $option["variation_id"] . '" vari_name="' . $option["attributes"]["attribute_servings"] . '" prod_id="' . $product_id . '" class="choose_plan" id="' . $html . '" value="' . $html . '" />' . apply_filters('woocommerce_variation_option_name', $option["attributes"]["attribute_servings"]) . '';
echo '</div>';
echo '<div class="plan_price">';
echo strip_tags($option['price_html']);
echo '</div>';
echo '</div>';
}
}
}
if ('variable-subscription' == $pType) {
//for subscription
echo '<div class="delivery_date_view">';
echo '<h3>First Delivery</h3>';
echo get_delivery_date();
echo '</div>';
} else {
//for single delivery
echo '<div class="delivery_date_view">';
echo '<h3 style="float: left;">Delivery Date</h3>';
echo '<div style="margin: 20px 0 0 160px;">' . get_delivery_date() . '</div>';
echo '<div style="clear: both;"></div>';
echo '</div>';
echo '<div class="delivery_date_view" style="margin-top: -40px;">';
echo '<h3 style="float: left;margin-top: 2px;">Servings</h3>';
echo '<div style="margin: 4px 0 0 160px;font-weight: bold;">' . $single_f_vari . '</div>';
//.........这里部分代码省略.........
示例8: hocwp_wc_insert_order
function hocwp_wc_insert_order($data)
{
$post_id = hocwp_get_value_by_key($data, 'post_id');
if (hocwp_id_number_valid($post_id)) {
$post = get_post($post_id);
if (is_a($post, 'WP_Post') && 'product' == $post->post_type) {
$product = wc_get_product($post_id);
$variable_product = new WC_Product_Variable($product);
$variations = $variable_product->get_available_variations();
$variation_args = array();
$variation_id = null;
foreach ($variations as $variation) {
$variation_id = $variation['variation_id'];
$variation_args['variation'] = $variation['attributes'];
}
$name = hocwp_get_value_by_key($data, 'name');
$phone = hocwp_get_value_by_key($data, 'phone');
$email = hocwp_get_value_by_key($data, 'email');
$address = hocwp_get_value_by_key($data, 'address');
$message = hocwp_get_value_by_key($data, 'message');
$name = hocwp_sanitize_first_and_last_name($name);
$attributes = hocwp_get_value_by_key($data, 'attributes');
$addresses = array('first_name' => $name['first_name'], 'last_name' => $name['last_name'], 'email' => $email, 'phone' => $phone, 'address_1' => $address);
$args = array('customer_note' => $message, 'created_via' => 'programmatically');
if (is_user_logged_in()) {
$current = wp_get_current_user();
$args['customer_id'] = $current->ID;
}
$order = wc_create_order($args);
$gateway = WC_Payment_Gateways::instance();
$gateways = $gateway->get_available_payment_gateways();
if (hocwp_array_has_value($gateways)) {
$gateway = current($gateways);
$order->set_payment_method($gateway);
}
$order->set_address($addresses);
$order->set_address($addresses, 'shipping');
if (hocwp_array_has_value($attributes) && hocwp_id_number_valid($variation_id)) {
foreach ($attributes as $attribute) {
$attribute_name = hocwp_get_value_by_key($attribute, 'name');
$attribute_value = hocwp_get_value_by_key($attribute, 'value');
if (!empty($attribute_name) && !empty($attribute_value)) {
if (isset($variation_args['variation'][$attribute_name])) {
$variation_args['variation'][$attribute_name] = $attribute_value;
}
}
}
$variation_product = new WC_Product_Variation($variation_id);
$order->add_product($variation_product, 1, $variation_args);
} else {
$order->add_product($product);
}
$order->record_product_sales();
$order->calculate_totals();
$order->payment_complete();
return $order;
}
}
return false;
}