abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
wc-stock-functions.php
115 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Stock Functions |
| 4 | * |
| 5 | * Functions used to manage product stock levels. |
| 6 | * |
| 7 | * @author WooThemes |
| 8 | * @category Core |
| 9 | * @package WooCommerce/Functions |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Update a product's stock amount. |
| 18 | * |
| 19 | * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues). |
| 20 | * |
| 21 | * @since 3.0.0 this supports set, increase and decrease. |
| 22 | * @param int|WC_Product $product |
| 23 | * @param int|null $stock_quantity |
| 24 | * @param string $operation set, increase and decrease. |
| 25 | */ |
| 26 | function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set' ) { |
| 27 | if ( ! $product = wc_get_product( $product ) ) { |
| 28 | return false; |
| 29 | } |
| 30 | if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) { |
| 31 | // Some products (variations) can have their stock managed by their parent. Get the correct ID to reduce here. |
| 32 | $product_id_with_stock = $product->get_stock_managed_by_id(); |
| 33 | $data_store = WC_Data_Store::load( 'product' ); |
| 34 | $data_store->update_product_stock( $product_id_with_stock, $stock_quantity, $operation ); |
| 35 | delete_transient( 'wc_low_stock_count' ); |
| 36 | delete_transient( 'wc_outofstock_count' ); |
| 37 | delete_transient( 'wc_product_children_' . ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() ) ); |
| 38 | wp_cache_delete( 'product-' . $product_id_with_stock, 'products' ); |
| 39 | |
| 40 | // Re-read product data after updating stock, then have stock status calculated and saved. |
| 41 | $product_with_stock = wc_get_product( $product_id_with_stock ); |
| 42 | $product_with_stock->set_stock_status(); |
| 43 | $product_with_stock->save(); |
| 44 | |
| 45 | do_action( $product_with_stock->is_type( 'variation' ) ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock', $product_with_stock ); |
| 46 | |
| 47 | return $product_with_stock->get_stock_quantity(); |
| 48 | } |
| 49 | return $product->get_stock_quantity(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Update a product's stock status. |
| 54 | * |
| 55 | * @param int $product_id |
| 56 | * @param int $status |
| 57 | */ |
| 58 | function wc_update_product_stock_status( $product_id, $status ) { |
| 59 | $product = wc_get_product( $product_id ); |
| 60 | if ( $product ) { |
| 61 | $product->set_stock_status( $status ); |
| 62 | $product->save(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * When a payment is complete, we can reduce stock levels for items within an order. |
| 68 | * @since 3.0.0 |
| 69 | * @param int $order_id |
| 70 | */ |
| 71 | function wc_maybe_reduce_stock_levels( $order_id ) { |
| 72 | $data_store = WC_Data_Store::load( 'order' ); |
| 73 | if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! $data_store->get_stock_reduced( $order_id ), $order_id ) ) { |
| 74 | wc_reduce_stock_levels( $order_id ); |
| 75 | $data_store->set_stock_reduced( $order_id, true ); |
| 76 | } |
| 77 | } |
| 78 | add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' ); |
| 79 | |
| 80 | /** |
| 81 | * Reduce stock levels for items within an order. |
| 82 | * @since 3.0.0 |
| 83 | * @param int $order_id |
| 84 | */ |
| 85 | function wc_reduce_stock_levels( $order_id ) { |
| 86 | $order = wc_get_order( $order_id ); |
| 87 | |
| 88 | if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && sizeof( $order->get_items() ) > 0 ) { |
| 89 | foreach ( $order->get_items() as $item ) { |
| 90 | if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) { |
| 91 | $qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item ); |
| 92 | $item_name = $product->get_formatted_name(); |
| 93 | $new_stock = wc_update_product_stock( $product, $qty, 'decrease' ); |
| 94 | |
| 95 | if ( ! is_wp_error( $new_stock ) ) { |
| 96 | /* translators: 1: item name 2: old stock quantity 3: new stock quantity */ |
| 97 | $order->add_order_note( sprintf( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ), $item_name, $new_stock + $qty, $new_stock ) ); |
| 98 | |
| 99 | if ( '' !== get_option( 'woocommerce_notify_no_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { |
| 100 | do_action( 'woocommerce_no_stock', $product ); |
| 101 | } elseif ( '' !== get_option( 'woocommerce_notify_low_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_low_stock_amount' ) ) { |
| 102 | do_action( 'woocommerce_low_stock', $product ); |
| 103 | } |
| 104 | |
| 105 | if ( $new_stock < 0 ) { |
| 106 | do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $order_id, 'quantity' => $qty ) ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | do_action( 'woocommerce_reduce_order_stock', $order ); |
| 113 | } |
| 114 | } |
| 115 |