Advertisement
Fany_VanDaal

Odečítání variant jako jeden produkt

Mar 30th, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package   Toret Product Variation Table
  4.  * @author    Vladislav Musílek
  5.  * @license   GPL-2.0+
  6.  * @link      http://toret.cz
  7.  * @copyright 2017 Toret.cz
  8.  *
  9.  * Plugin Name:       Toret Reduce All Variations Stock
  10.  * Plugin URI:        
  11.  * Description:       Plugin change stock quantity for all variations, when one is sell
  12.  * Version:           1.0
  13.  * Author:            Vladislav Musílek
  14.  * Author URI:        toret.cz
  15.  * License:           GPL-2.0+
  16.  * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
  17.  * Domain Path:       /languages
  18.  */
  19.  
  20. // If this file is called directly, abort.
  21. if ( ! defined( 'WPINC' ) ) {
  22.     die;
  23. }
  24.  
  25.  
  26. add_action( 'woocommerce_variation_set_stock', 'toret_reduce_all_variations', 10, 1 );
  27.  
  28. /**
  29.  * Change stock
  30.  *
  31.  * @since    1.0
  32.  */
  33. function toret_reduce_all_variations( $product ){
  34.  
  35.     $qty = $product->get_stock_quantity();
  36.  
  37.     $parent = wp_get_post_parent_id( $product->get_id() );
  38.     $parent_product = wc_get_product( $parent );
  39.        
  40.     $variations = $parent_product->get_available_variations();
  41.  
  42.     foreach( $variations as $variation ){
  43.  
  44.         //Must set directly to custom field, $product->set_stock_quantity() call this loop again for each variation and stock is set by last variation qty
  45.         $variation_product = update_post_meta( $variation['variation_id'], '_stock', $qty );
  46.  
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement