Advertisement
Guest User

WooCommerce WC Vendors Shipping Splitter divided

a guest
Nov 3rd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: WC Vendors Package Shipping Splitter
  4. Plugin URI: mailto:goteamscotch@gmail.com
  5. Description: Gives vendors their externally calculated shipping dues. TEST CODE
  6. Version: 0.1b
  7. Author: GoTeamScotch
  8. */
  9.  
  10. /*
  11. Notes
  12. - This requires the plugin Packages Configuration for WooCommerce by Jason Judge
  13. */
  14.  
  15. if (!defined('ABSPATH')) die; // Don't allow direct loading
  16. add_action( 'plugins_loaded', array( 'GTS_WCV_Ship', 'init' ), 20 );
  17.  
  18. if (!class_exists('GTS_WCV_Ship')) {
  19.     class GTS_WCV_Ship {
  20.         public static $instance = null;
  21.        
  22.         public static function init() {
  23.             null === self::$instance AND self::$instance = new self;
  24.             return self::$instance;
  25.         }
  26.        
  27.         public function __construct() {
  28.             include_once ABSPATH . 'wp-admin/includes/plugin.php';
  29.             $req = array('woocommerce/woocommerce.php', 'wc-vendors/class-wc-vendors.php', 'packages-configuration-for-woocommerce/multiple-packages-shipping.php');
  30.             foreach ($req as $plugin)
  31.                 if (!is_plugin_active($plugin)) {
  32.                     deactivate_plugins( plugin_basename( __FILE__ ) );
  33.                     die(__CLASS__ . ": Missing a required plugin: {$plugin}");
  34.                 }
  35.            
  36.             $give_shipping = WC_Vendors::$pv_options->get_option( 'give_shipping' );
  37.             if ($give_shipping == false)
  38.                 add_action('admin_notices', array($this, 'wrong_settings'));
  39.            
  40.             add_action('woocommerce_add_shipping_order_item', array($this, 'add_shipping_line_vendor'), 10, 3);
  41.             add_filter('wcvendors_shipping_due', array($this, 'get_vendors_shipping_dues_product'), 10, 3);
  42.         }
  43.        
  44.         public function wrong_settings() {
  45.             ?>
  46.             <div class="updated">
  47.                 <p>Attention: You may not have WC Vendors configured correctly! Please enable the setting "Give vendors shipping" for this plugin to work properly.<br><em>Note: If you override this option on a per-vendor basis, then you may ignore this message.</em></p>
  48.                 <p class="description">Generated by WC Vendors Package Shipping Splitter</p>
  49.             </div>
  50.             <?php
  51.         }
  52.        
  53.         // adds extra meta data to the shipping order items so we can tell which vendor the shipping line is destined for
  54.         public function add_shipping_line_vendor( $order_id, $item_id, $package_key ) {
  55.             $packages = WC()->shipping->get_packages();
  56.    
  57.             if (!isset($packages[$package_key])) return;
  58.    
  59.             $package = $packages[$package_key];
  60.            
  61.             $sample_product = array_pop($package['contents']);
  62.            
  63.             $vendor_id = WCV_Vendors::get_vendor_from_product($sample_product['product_id']);
  64.             if (WCV_Vendors::is_vendor($vendor_id))
  65.                 wc_add_order_item_meta($item_id, '_vendor_id', $vendor_id, true);
  66.         }
  67.        
  68.         // gets fired by WC Vendors when finding out how much shipping fees to give to the vendor for a given product
  69.         public function get_vendors_shipping_dues_product( $shipping_dues, $order_id, $product ) {
  70.             static $order = null;
  71.             if (is_null($order) || ($order instanceof WC_Order && $order->ID != $order_id))
  72.                 $order = new WC_Order($order_id);
  73.            
  74.             $vendor_id = WCV_Vendors::get_vendor_from_product($product['product_id']);
  75.             if (WCV_Vendors::is_vendor($vendor_id)) {
  76.                 $order = new WC_Order($order_id);
  77.                
  78.                 $shipping_methods = $order->get_shipping_methods();
  79.                 foreach ($shipping_methods as $key => $data) {
  80.                     if (!isset($data['vendor_id']) || (isset($data['vendor_id']) && $data['vendor_id'] != $vendor_id)) continue;
  81.                    
  82.                     $vendor_owed_shipping = $data['cost']; // this is their total shipping due for the entire order
  83.                     $number_items_pkg = count(explode('|', $data['product_ids']));
  84.                     $shipping_dues['amount'] = (($vendor_owed_shipping / $number_items_pkg) * $product['qty']);
  85.                     $shipping_dues['tax'] = $data['taxes'];
  86.                     break;
  87.                 }
  88.             }
  89.             return $shipping_dues;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement