本文整理汇总了PHP中WC_Product::managing_stock方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product::managing_stock方法的具体用法?PHP WC_Product::managing_stock怎么用?PHP WC_Product::managing_stock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product
的用法示例。
在下文中一共展示了WC_Product::managing_stock方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_product_data
/**
* Get standard product data that applies to every product type
*
* @since 2.1
* @param WC_Product $product
* @return array
*/
private function get_product_data($product)
{
return array('title' => $product->get_name(), 'id' => $product->get_id(), 'created_at' => $this->server->format_datetime($product->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($product->get_date_modified(), false, true), 'type' => $product->get_type(), 'status' => $product->get_status(), 'downloadable' => $product->is_downloadable(), 'virtual' => $product->is_virtual(), 'permalink' => $product->get_permalink(), 'sku' => $product->get_sku(), 'price' => wc_format_decimal($product->get_price(), 2), 'regular_price' => wc_format_decimal($product->get_regular_price(), 2), 'sale_price' => $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : null, 'price_html' => $product->get_price_html(), 'taxable' => $product->is_taxable(), 'tax_status' => $product->get_tax_status(), 'tax_class' => $product->get_tax_class(), 'managing_stock' => $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => $product->is_in_stock(), 'backorders_allowed' => $product->backorders_allowed(), 'backordered' => $product->is_on_backorder(), 'sold_individually' => $product->is_sold_individually(), 'purchaseable' => $product->is_purchasable(), 'featured' => $product->is_featured(), 'visible' => $product->is_visible(), 'catalog_visibility' => $product->get_catalog_visibility(), 'on_sale' => $product->is_on_sale(), 'weight' => $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : null, 'dimensions' => array('length' => $product->get_length(), 'width' => $product->get_width(), 'height' => $product->get_height(), 'unit' => get_option('woocommerce_dimension_unit')), 'shipping_required' => $product->needs_shipping(), 'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_class' => $product->get_shipping_class(), 'shipping_class_id' => 0 !== $product->get_shipping_class_id() ? $product->get_shipping_class_id() : null, 'description' => apply_filters('the_content', $product->get_description()), 'short_description' => apply_filters('woocommerce_short_description', $product->get_short_description()), 'reviews_allowed' => $product->get_reviews_allowed(), 'average_rating' => wc_format_decimal($product->get_average_rating(), 2), 'rating_count' => $product->get_rating_count(), 'related_ids' => array_map('absint', array_values(wc_get_related_products($product->get_id()))), 'upsell_ids' => array_map('absint', $product->get_upsell_ids()), 'cross_sell_ids' => array_map('absint', $product->get_cross_sell_ids()), 'categories' => wc_get_object_terms($product->get_id(), 'product_cat', 'name'), 'tags' => wc_get_object_terms($product->get_id(), 'product_tag', 'name'), 'images' => $this->get_images($product), 'featured_src' => wp_get_attachment_url(get_post_thumbnail_id($product->get_id())), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => $product->get_download_limit(), 'download_expiry' => $product->get_download_expiry(), 'download_type' => 'standard', 'purchase_note' => apply_filters('the_content', $product->get_purchase_note()), 'total_sales' => $product->get_total_sales(), 'variations' => array(), 'parent' => array());
}
示例2: switch
/**
* Bundled product availability that takes quantity into account.
*
* @param WC_Product $product the product
* @param int $quantity the quantity
* @return array availability data
*/
function get_bundled_product_availability($product, $quantity)
{
$availability = $class = '';
if ($product->managing_stock()) {
if ($product->is_in_stock() && $product->get_total_stock() > get_option('woocommerce_notify_no_stock_amount') && $product->get_total_stock() >= $quantity) {
switch (get_option('woocommerce_stock_format')) {
case 'no_amount':
$availability = __('In stock', 'woocommerce');
break;
case 'low_amount':
if ($product->get_total_stock() <= get_option('woocommerce_notify_low_stock_amount')) {
$availability = sprintf(__('Only %s left in stock', 'woocommerce'), $product->get_total_stock());
if ($product->backorders_allowed() && $product->backorders_require_notification()) {
$availability .= ' ' . __('(can be backordered)', 'woocommerce');
}
} else {
$availability = __('In stock', 'woocommerce');
}
break;
default:
$availability = sprintf(__('%s in stock', 'woocommerce'), $product->get_total_stock());
if ($product->backorders_allowed() && $product->backorders_require_notification()) {
$availability .= ' ' . __('(can be backordered)', 'woocommerce');
}
break;
}
$class = 'in-stock';
} elseif ($product->backorders_allowed() && $product->backorders_require_notification()) {
if ($product->get_total_stock() >= $quantity || get_option('woocommerce_stock_format') == 'no_amount') {
$availability = __('Available on backorder', 'woocommerce');
} else {
$availability = __('Available on backorder', 'woocommerce') . ' ' . sprintf(__('(only %s left in stock)', 'woocommerce-product-bundles'), $product->get_total_stock());
}
$class = 'available-on-backorder';
} elseif ($product->backorders_allowed()) {
$availability = __('In stock', 'woocommerce');
$class = 'in-stock';
} else {
if ($product->is_in_stock() && $product->get_total_stock() > get_option('woocommerce_notify_no_stock_amount')) {
if (get_option('woocommerce_stock_format') == 'no_amount') {
$availability = __('Insufficient stock', 'woocommerce-product-bundles');
} else {
$availability = __('Insufficient stock', 'woocommerce-product-bundles') . ' ' . sprintf(__('(only %s left in stock)', 'woocommerce-product-bundles'), $product->get_total_stock());
}
$class = 'out-of-stock';
} else {
$availability = __('Out of stock', 'woocommerce');
$class = 'out-of-stock';
}
}
} elseif (!$product->is_in_stock()) {
$availability = __('Out of stock', 'woocommerce');
$class = 'out-of-stock';
}
_deprecated_function('get_bundled_product_availability', '4.8.8', 'WC_Bundled_Item::get_availability()');
return apply_filters('woocommerce_get_bundled_product_availability', array('availability' => $availability, 'class' => $class), $product);
}
示例3: get_product_data
/**
* Get standard product data that applies to every product type
*
* @since 2.1
* @param WC_Product $product
* @return WC_Product
*/
private function get_product_data($product)
{
return array('title' => $product->get_title(), 'id' => (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id, 'created_at' => $this->server->format_datetime($product->get_post_data()->post_date_gmt), 'updated_at' => $this->server->format_datetime($product->get_post_data()->post_modified_gmt), 'type' => $product->product_type, 'status' => $product->get_post_data()->post_status, 'downloadable' => $product->is_downloadable(), 'virtual' => $product->is_virtual(), 'permalink' => $product->get_permalink(), 'sku' => $product->get_sku(), 'price' => $product->get_price(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $product->get_sale_price() ? $product->get_sale_price() : null, 'price_html' => $product->get_price_html(), 'taxable' => $product->is_taxable(), 'tax_status' => $product->get_tax_status(), 'tax_class' => $product->get_tax_class(), 'managing_stock' => $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => $product->is_in_stock(), 'backorders_allowed' => $product->backorders_allowed(), 'backordered' => $product->is_on_backorder(), 'sold_individually' => $product->is_sold_individually(), 'purchaseable' => $product->is_purchasable(), 'featured' => $product->is_featured(), 'visible' => $product->is_visible(), 'catalog_visibility' => $product->visibility, 'on_sale' => $product->is_on_sale(), 'product_url' => $product->is_type('external') ? $product->get_product_url() : '', 'button_text' => $product->is_type('external') ? $product->get_button_text() : '', 'weight' => $product->get_weight() ? $product->get_weight() : null, 'dimensions' => array('length' => $product->length, 'width' => $product->width, 'height' => $product->height, 'unit' => get_option('woocommerce_dimension_unit')), 'shipping_required' => $product->needs_shipping(), 'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_class' => $product->get_shipping_class(), 'shipping_class_id' => 0 !== $product->get_shipping_class_id() ? $product->get_shipping_class_id() : null, 'description' => wpautop(do_shortcode($product->get_post_data()->post_content)), 'short_description' => apply_filters('woocommerce_short_description', $product->get_post_data()->post_excerpt), 'reviews_allowed' => 'open' === $product->get_post_data()->comment_status, 'average_rating' => wc_format_decimal($product->get_average_rating(), 2), 'rating_count' => (int) $product->get_rating_count(), 'related_ids' => array_map('absint', array_values($product->get_related())), 'upsell_ids' => array_map('absint', $product->get_upsells()), 'cross_sell_ids' => array_map('absint', $product->get_cross_sells()), 'parent_id' => $product->post->post_parent, 'categories' => wp_get_post_terms($product->id, 'product_cat', array('fields' => 'names')), 'tags' => wp_get_post_terms($product->id, 'product_tag', array('fields' => 'names')), 'images' => $this->get_images($product), 'featured_src' => (string) wp_get_attachment_url(get_post_thumbnail_id($product->is_type('variation') ? $product->variation_id : $product->id)), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => (int) $product->download_limit, 'download_expiry' => (int) $product->download_expiry, 'download_type' => $product->download_type, 'purchase_note' => wpautop(do_shortcode(wp_kses_post($product->purchase_note))), 'total_sales' => metadata_exists('post', $product->id, 'total_sales') ? (int) get_post_meta($product->id, 'total_sales', true) : 0, 'variations' => array(), 'parent' => array(), 'grouped_products' => array());
}
示例4: get_ticket
/**
* Gets an individual ticket
*
* @param $event_id
* @param $ticket_id
*
* @return null|Tribe__Tickets__Ticket_Object
*/
public function get_ticket($event_id, $ticket_id)
{
if (class_exists('WC_Product_Simple')) {
$product = new WC_Product_Simple($ticket_id);
} else {
$product = new WC_Product($ticket_id);
}
if (!$product) {
return null;
}
$return = new Tribe__Tickets__Ticket_Object();
$product_data = $product->get_post_data();
$qty = get_post_meta($ticket_id, 'total_sales', true);
$return->description = $product_data->post_excerpt;
$return->frontend_link = get_permalink($ticket_id);
$return->ID = $ticket_id;
$return->name = $product->get_title();
$return->price = $product->get_price();
$return->regular_price = $product->get_regular_price();
$return->on_sale = (bool) $product->is_on_sale();
$return->provider_class = get_class($this);
$return->admin_link = admin_url(sprintf(get_post_type_object($product_data->post_type)->_edit_link . '&action=edit', $ticket_id));
$return->start_date = get_post_meta($ticket_id, '_ticket_start_date', true);
$return->end_date = get_post_meta($ticket_id, '_ticket_end_date', true);
$return->purchase_limit = get_post_meta($ticket_id, '_ticket_purchase_limit', true);
$complete_totals = $this->count_order_items_by_status($ticket_id, 'complete');
$pending_totals = $this->count_order_items_by_status($ticket_id, 'incomplete');
$qty = $qty ? $qty : 0;
$pending = $pending_totals['total'] ? $pending_totals['total'] : 0;
// if any orders transitioned from complete back to one of the incomplete states, their quantities
// were already recorded in total_sales and we have to deduct them from there so they aren't
// double counted
$qty -= $pending_totals['recorded_sales'];
// let's calculate the stock based on the product stock minus the quantity purchased minus the
// pending purchases
$stock = $product->get_stock_quantity() - $qty - $pending;
// if any orders have reduced the stock of an order (check and cash on delivery payments do this, for example)
// we need to re-inflate the stock by that amount
$stock += $pending_totals['reduced_stock'];
$stock += $complete_totals['reduced_stock'];
$return->manage_stock($product->managing_stock());
$return->stock($stock);
$return->qty_sold($qty);
$return->qty_pending($pending);
$return->qty_cancelled($this->get_cancelled($ticket_id));
if (empty($return->purchase_limit) && 0 !== (int) $return->purchase_limit) {
/**
* Filter the default purchase limit for the ticket
*
* @var int
*
* @return int
*/
$return->purchase_limit = apply_filters('tribe_tickets_default_purchase_limit', 0);
}
return apply_filters('wootickets_get_ticket', $return, $event_id, $ticket_id);
}
示例5: woocommerce_custom_product_columns
//.........这里部分代码省略.........
echo '<div class="row-actions">';
$i = 0;
$action_count = sizeof($actions);
foreach ($actions as $action => $link) {
++$i;
$i == $action_count ? $sep = '' : ($sep = ' | ');
echo "<span class='{$action}'>{$link}{$sep}</span>";
}
echo '</div>';
get_inline_data($post);
/* Custom inline data for woocommerce */
echo '
<div class="hidden" id="woocommerce_inline_' . $post->ID . '">
<div class="sku">' . $product->sku . '</div>
<div class="regular_price">' . $product->regular_price . '</div>
<div class="sale_price">' . $product->sale_price . '</div>
<div class="weight">' . $product->weight . '</div>
<div class="length">' . $product->length . '</div>
<div class="width">' . $product->width . '</div>
<div class="height">' . $product->height . '</div>
<div class="visibility">' . $product->visibility . '</div>
<div class="stock_status">' . $product->stock_status . '</div>
<div class="stock">' . $product->stock . '</div>
<div class="manage_stock">' . $product->manage_stock . '</div>
<div class="featured">' . $product->featured . '</div>
<div class="product_type">' . $product->product_type . '</div>
<div class="product_is_virtual">' . $product->virtual . '</div>
</div>
';
break;
case "sku":
if ($product->get_sku()) {
echo $product->get_sku();
} else {
echo '<span class="na">–</span>';
}
break;
case "product_type":
if ($product->product_type == 'grouped') {
echo '<span class="product-type tips ' . $product->product_type . '" tip="' . __('Grouped', 'woocommerce') . '"></span>';
} elseif ($product->product_type == 'external') {
echo '<span class="product-type tips ' . $product->product_type . '" tip="' . __('External/Affiliate', 'woocommerce') . '"></span>';
} elseif ($product->product_type == 'simple') {
if ($product->is_virtual()) {
echo '<span class="product-type tips virtual" tip="' . __('Virtual', 'woocommerce') . '"></span>';
} elseif ($product->is_downloadable()) {
echo '<span class="product-type tips downloadable" tip="' . __('Downloadable', 'woocommerce') . '"></span>';
} else {
echo '<span class="product-type tips ' . $product->product_type . '" tip="' . __('Simple', 'woocommerce') . '"></span>';
}
} elseif ($product->product_type == 'variable') {
echo '<span class="product-type tips ' . $product->product_type . '" tip="' . __('Variable', 'woocommerce') . '"></span>';
} else {
// Assuming that we have other types in future
echo '<span class="product-type tips ' . $product->product_type . '" tip="' . ucwords($product->product_type) . '"></span>';
}
break;
case "price":
if ($product->get_price_html()) {
echo $product->get_price_html();
} else {
echo '<span class="na">–</span>';
}
break;
case "product_cat":
if (!($terms = get_the_term_list($post->ID, 'product_cat', '', ', ', ''))) {
echo '<span class="na">–</span>';
} else {
echo $terms;
}
break;
case "product_tags":
if (!($terms = get_the_term_list($post->ID, 'product_tag', '', ', ', ''))) {
echo '<span class="na">–</span>';
} else {
echo $terms;
}
break;
case "featured":
$url = wp_nonce_url(admin_url('admin-ajax.php?action=woocommerce-feature-product&product_id=' . $post->ID), 'woocommerce-feature-product');
echo '<a href="' . $url . '" title="' . __('Change', 'woocommerce') . '">';
if ($product->is_featured()) {
echo '<a href="' . $url . '"><img src="' . $woocommerce->plugin_url() . '/assets/images/featured.png" alt="yes" />';
} else {
echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/featured-off.png" alt="no" />';
}
echo '</a>';
break;
case "is_in_stock":
if ($product->is_in_stock()) {
echo '<mark class="instock">' . __('In stock', 'woocommerce') . '</mark>';
} else {
echo '<mark class="outofstock">' . __('Out of stock', 'woocommerce') . '</mark>';
}
if ($product->managing_stock()) {
echo ' × ' . $product->get_total_stock();
}
break;
}
}
示例6: foreach
function process_qty()
{
global $woocommerce;
// Save quantities
if (!empty($_POST['stock_quantity']) && !empty($_POST['save_stock'])) {
check_admin_referer('save', 'wc-stock-management');
$quantities = $_POST['stock_quantity'];
$current_quantities = $_POST['current_stock_quantity'];
foreach ($quantities as $id => $qty) {
if ($qty == '') {
continue;
}
if (isset($current_quantities[$id])) {
// Check the qty has not changed since showing the form
$current_stock = (int) get_post_meta($id, '_stock', true);
if ($current_stock == $current_quantities[$id]) {
$post = get_post($id);
// Format $qty
$qty = (int) $qty;
// Update stock amount
update_post_meta($id, '_stock', $qty);
// Update stock status
if ($post->post_type == 'product') {
// Update manage stock variable for products
update_post_meta($id, '_manage_stock', 'yes');
if (function_exists('get_product')) {
$product = get_product($post->ID);
} else {
$product = new WC_Product($post->ID);
}
if ($product->managing_stock() && !$product->backorders_allowed() && $product->get_total_stock() <= 0) {
update_post_meta($post->ID, '_stock_status', 'outofstock');
} elseif ($product->managing_stock() && ($product->backorders_allowed() || $product->get_total_stock() > 0)) {
update_post_meta($post->ID, '_stock_status', 'instock');
}
$woocommerce->clear_product_transients($post->ID);
// Clear transient
} else {
if (function_exists('get_product')) {
$product = get_product($post->post_parent);
} else {
$product = new WC_Product($post->post_parent);
}
if ($product->managing_stock() && !$product->backorders_allowed() && $product->get_total_stock() <= 0) {
update_post_meta($post->post_parent, '_stock_status', 'outofstock');
} elseif ($product->managing_stock() && ($product->backorders_allowed() || $product->get_total_stock() > 0)) {
update_post_meta($post->post_parent, '_stock_status', 'instock');
}
$woocommerce->clear_product_transients($post->post_parent);
// Clear transient
}
} else {
$this->messages[] = sprintf(__('Product # %s was not updated - the stock amount has changed since posting.', 'wc_stock_management'), $id);
}
}
}
$this->messages[] = __('Stock quantities saved.', 'wc_stock_management');
}
}
示例7: array
/**
* Check stock before attempting to call the add_to_cart function
* Some double checking happens, but it's better than partially adding items to the cart
**/
function validate_stock($product_id, $variation_id, $quantity, $exclude_cart, $silent)
{
global $woocommerce;
if ($variation_id > 0) {
if ($this->is_wc_v2()) {
$product_data = get_product($variation_id, array('product_type' => 'variation'));
} else {
$product_data = new WC_Product_Variation($variation_id);
}
} else {
if ($this->is_wc_v2()) {
$product_data = get_product($product_id, array('product_type' => 'simple'));
} else {
$product_data = new WC_Product($product_id);
}
}
// Stock check - only check if we're managing stock and backorders are not allowed.
if (!$product_data->is_in_stock()) {
if (!$silent) {
$woocommerce->add_error(sprintf(__('You cannot add this product to the cart since "%s" is out of stock.', 'woo-bundles'), $product_data->get_title()));
}
return false;
} elseif (!$product_data->has_enough_stock($quantity)) {
if (!$silent) {
$woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock of "%s". We have %s in stock.', 'woo-bundles'), $product_data->get_title(), $product_data->get_stock_quantity()));
}
return false;
}
// Stock check - this time accounting for whats already in-cart.
if ($exclude_cart) {
return true;
}
$product_qty_in_cart = $woocommerce->cart->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)) {
if (!$silent) {
$woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a>You cannot add that amount to the cart since there is not enough stock of "%s" — we have %s in stock and you already have %s in your cart.', 'woo-bundles'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_title(), $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)) {
if (!$silent) {
$woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a>You cannot add that amount to the cart since there is not enough stock of "%s" — we have %s in stock and you already have %s in your cart.', 'woo-bundles'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_title(), $product_data->get_stock_quantity(), $product_qty_in_cart[$product_id]));
}
return false;
}
}
}
return true;
}
示例8: quantity_change_handler
public function quantity_change_handler($order_id)
{
if (!version_compare(WOOCOMMERCE_VERSION, '2.1', '<')) {
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product = new WC_Product($product_id);
if (!$product->managing_stock()) {
continue;
}
$quantity = $product->get_stock_quantity();
if ($item['qty'] == $quantity) {
// update out of stock
self::on_product_update($product_id, 'update');
}
}
}
}
示例9:
/**
* Reduce stock level of the product
*
* @param int $by Amount to reduce by
*/
function reduce_stock($by = 1)
{
global $woocommerce;
if ($this->variation_has_stock) {
if ($this->managing_stock()) {
$this->stock = $this->stock - $by;
$this->total_stock = $this->get_total_stock() - $by;
update_post_meta($this->variation_id, '_stock', $this->stock);
// Parents out of stock attribute
if (!$this->is_in_stock()) {
// Check parent
$parent_product = new WC_Product($this->id);
if ($parent_product->managing_stock()) {
if (!$parent_product->backorders_allowed()) {
if ($parent_product->get_total_stock() == 0 || $parent_product->get_total_stock() < 0) {
update_post_meta($this->id, '_stock_status', 'outofstock');
$woocommerce->clear_product_transients($this->id);
// Clear transient
}
}
} else {
if ($parent_product->get_total_stock() == 0 || $parent_product->get_total_stock() < 0) {
update_post_meta($this->id, '_stock_status', 'outofstock');
$woocommerce->clear_product_transients($this->id);
// Clear transient
}
}
}
return $this->stock;
}
} else {
return parent::reduce_stock($by);
}
}
示例10: array
/**
* 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;
}
示例11: get_product_data
/**
* Get standard product data that applies to every product type
*
* @since 2.1
* @param WC_Product $product
* @return WC_Product
*/
private function get_product_data($product, $fields = null)
{
if ($fields) {
$field_list = explode(',', $fields);
}
$product_data = array();
if (!$fields || $fields && in_array('title', $field_list)) {
$product_data['title'] = $product->get_title();
}
if (!$fields || $fields && in_array('id', $field_list)) {
$product_data['id'] = (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id;
}
if (!$fields || $fields && in_array('created_at', $field_list)) {
$product_data['created_at'] = $this->server->format_datetime($product->get_post_data()->post_date_gmt);
}
if (!$fields || $fields && in_array('updated_at', $field_list)) {
$product_data['updated_at'] = $this->server->format_datetime($product->get_post_data()->post_modified_gmt);
}
if (!$fields || $fields && in_array('type', $field_list)) {
$product_data['type'] = $product->product_type;
}
if (!$fields || $fields && in_array('status', $field_list)) {
$product_data['status'] = $product->get_post_data()->post_status;
}
if (!$fields || $fields && in_array('downloadable', $field_list)) {
$product_data['downloadable'] = $product->is_downloadable();
}
if (!$fields || $fields && in_array('virtual', $field_list)) {
$product_data['virtual'] = $product->is_virtual();
}
if (!$fields || $fields && in_array('permalink', $field_list)) {
$product_data['permalink'] = $product->get_permalink();
}
if (!$fields || $fields && in_array('sku', $field_list)) {
$product_data['sku'] = $product->get_sku();
}
if (!$fields || $fields && in_array('price', $field_list)) {
$product_data['price'] = $product->get_price();
}
if (!$fields || $fields && in_array('regular_price', $field_list)) {
$product_data['regular_price'] = $product->get_regular_price();
}
if (!$fields || $fields && in_array('sale_price', $field_list)) {
$product_data['sale_price'] = $product->get_sale_price() ? $product->get_sale_price() : null;
}
if (!$fields || $fields && in_array('price_html', $field_list)) {
$product_data['price_html'] = $product->get_price_html();
}
if (!$fields || $fields && in_array('taxable', $field_list)) {
$product_data['taxable'] = $product->is_taxable();
}
if (!$fields || $fields && in_array('tax_status', $field_list)) {
$product_data['tax_status'] = $product->get_tax_status();
}
if (!$fields || $fields && in_array('tax_class', $field_list)) {
$product_data['tax_class'] = $product->get_tax_class();
}
if (!$fields || $fields && in_array('managing_stock', $field_list)) {
$product_data['managing_stock'] = $product->managing_stock();
}
if (!$fields || $fields && in_array('stock_quantity', $field_list)) {
$product_data['stock_quantity'] = $product->get_stock_quantity();
}
if (!$fields || $fields && in_array('in_stock', $field_list)) {
$product_data['in_stock'] = $product->is_in_stock();
}
if (!$fields || $fields && in_array('backorders_allowed', $field_list)) {
$product_data['backorders_allowed'] = $product->backorders_allowed();
}
if (!$fields || $fields && in_array('backordered', $field_list)) {
$product_data['backordered'] = $product->is_on_backorder();
}
if (!$fields || $fields && in_array('sold_individually', $field_list)) {
$product_data['sold_individually'] = $product->is_sold_individually();
}
if (!$fields || $fields && in_array('purchaseable', $field_list)) {
$product_data['purchaseable'] = $product->is_purchasable();
}
if (!$fields || $fields && in_array('featured', $field_list)) {
$product_data['featured'] = $product->is_featured();
}
if (!$fields || $fields && in_array('visible', $field_list)) {
$product_data['visible'] = $product->is_visible();
}
if (!$fields || $fields && in_array('catalog_visibility', $field_list)) {
$product_data['catalog_visibility'] = $product->visibility;
}
if (!$fields || $fields && in_array('on_sale', $field_list)) {
$product_data['on_sale'] = $product->is_on_sale();
}
if (!$fields || $fields && in_array('product_url', $field_list)) {
$product_data['product_url'] = $product->is_type('external') ? $product->get_product_url() : '';
}
//.........这里部分代码省略.........
示例12: get_ticket
/**
* Gets an individual ticket
*
* @param $event_id
* @param $ticket_id
*
* @return null|Tribe__Tickets__Ticket_Object
*/
public function get_ticket($event_id, $ticket_id)
{
if (class_exists('WC_Product_Simple')) {
$product = new WC_Product_Simple($ticket_id);
} else {
$product = new WC_Product($ticket_id);
}
if (!$product) {
return null;
}
$return = new Tribe__Tickets__Ticket_Object();
$product_data = $product->get_post_data();
$qty = get_post_meta($ticket_id, 'total_sales', true);
$return->description = $product_data->post_excerpt;
$return->frontend_link = get_permalink($ticket_id);
$return->ID = $ticket_id;
$return->name = $product->get_title();
$return->price = $product->get_price();
$return->regular_price = $product->get_regular_price();
$return->on_sale = (bool) $product->is_on_sale();
$return->provider_class = get_class($this);
$return->admin_link = admin_url(sprintf(get_post_type_object($product_data->post_type)->_edit_link . '&action=edit', $ticket_id));
$return->start_date = get_post_meta($ticket_id, '_ticket_start_date', true);
$return->end_date = get_post_meta($ticket_id, '_ticket_end_date', true);
$return->purchase_limit = get_post_meta($ticket_id, '_ticket_purchase_limit', true);
$complete_totals = $this->count_order_items_by_status($ticket_id, 'complete');
$pending_totals = $this->count_order_items_by_status($ticket_id, 'incomplete');
$qty = $qty ? $qty : 0;
$pending = $pending_totals['total'] ? $pending_totals['total'] : 0;
// Ticket stock is a simple reflection of remaining inventory for this item...
$stock = $product->get_stock_quantity();
// ...With some exceptions for global stock tickets
$stock = $this->set_stock_level_for_global_stock_tickets($stock, $event_id, $ticket_id);
$return->manage_stock($product->managing_stock());
$return->stock($stock);
$return->global_stock_mode(get_post_meta($ticket_id, '_global_stock_mode', true));
$return->global_stock_cap(get_post_meta($ticket_id, '_global_stock_cap', true));
$return->qty_sold($qty);
$return->qty_pending($pending);
$return->qty_cancelled($this->get_cancelled($ticket_id));
if (empty($return->purchase_limit) && 0 !== (int) $return->purchase_limit) {
/**
* Filter the default purchase limit for the ticket
*
* @var int
*
* @return int
*/
$return->purchase_limit = apply_filters('tribe_tickets_default_purchase_limit', 0);
}
return apply_filters('wootickets_get_ticket', $return, $event_id, $ticket_id);
}
示例13: get_stock_managed_wc_variation_ids
/**
* Retrieve WC Variation IDs for a given WC Product, that we're managing stock for.
*
* @param WC_Product $wc_product
* @return array
*/
public static function get_stock_managed_wc_variation_ids(WC_Product $wc_product)
{
$wc_variation_ids = array();
if ('simple' === $wc_product->product_type) {
if ($wc_product->managing_stock()) {
$wc_variation_ids = array($wc_product->id);
}
} elseif ('variable' === $wc_product->product_type) {
$variations = self::get_wc_product_variations($wc_product);
foreach ((array) $variations as $variation) {
if ($variation->managing_stock()) {
$wc_variation_ids[] = $variation->variation_id;
}
}
}
return $wc_variation_ids;
}