Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: WC Vendors Package Shipping Splitter
- Plugin URI: mailto:goteamscotch@gmail.com
- Description: Gives vendors their externally calculated shipping dues. TEST CODE
- Version: 0.1b
- Author: GoTeamScotch
- */
- /*
- Notes
- - This requires the plugin Packages Configuration for WooCommerce by Jason Judge
- */
- if (!defined('ABSPATH')) die; // Don't allow direct loading
- add_action( 'plugins_loaded', array( 'GTS_WCV_Ship', 'init' ), 20 );
- if (!class_exists('GTS_WCV_Ship')) {
- class GTS_WCV_Ship {
- public static $instance = null;
- public static function init() {
- null === self::$instance AND self::$instance = new self;
- return self::$instance;
- }
- public function __construct() {
- include_once ABSPATH . 'wp-admin/includes/plugin.php';
- $req = array('woocommerce/woocommerce.php', 'wc-vendors/class-wc-vendors.php', 'packages-configuration-for-woocommerce/multiple-packages-shipping.php');
- foreach ($req as $plugin)
- if (!is_plugin_active($plugin)) {
- deactivate_plugins( plugin_basename( __FILE__ ) );
- die(__CLASS__ . ": Missing a required plugin: {$plugin}");
- }
- $give_shipping = WC_Vendors::$pv_options->get_option( 'give_shipping' );
- if ($give_shipping == false)
- add_action('admin_notices', array($this, 'wrong_settings'));
- add_action('woocommerce_add_shipping_order_item', array($this, 'add_shipping_line_vendor'), 10, 3);
- add_filter('wcvendors_shipping_due', array($this, 'get_vendors_shipping_dues_product'), 10, 3);
- }
- public function wrong_settings() {
- ?>
- <div class="updated">
- <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>
- <p class="description">Generated by WC Vendors Package Shipping Splitter</p>
- </div>
- <?php
- }
- // adds extra meta data to the shipping order items so we can tell which vendor the shipping line is destined for
- public function add_shipping_line_vendor( $order_id, $item_id, $package_key ) {
- $packages = WC()->shipping->get_packages();
- if (!isset($packages[$package_key])) return;
- $package = $packages[$package_key];
- $sample_product = array_pop($package['contents']);
- $vendor_id = WCV_Vendors::get_vendor_from_product($sample_product['product_id']);
- if (WCV_Vendors::is_vendor($vendor_id))
- wc_add_order_item_meta($item_id, '_vendor_id', $vendor_id, true);
- }
- // gets fired by WC Vendors when finding out how much shipping fees to give to the vendor for a given product
- public function get_vendors_shipping_dues_product( $shipping_dues, $order_id, $product ) {
- static $order = null;
- if (is_null($order) || ($order instanceof WC_Order && $order->ID != $order_id))
- $order = new WC_Order($order_id);
- $vendor_id = WCV_Vendors::get_vendor_from_product($product['product_id']);
- if (WCV_Vendors::is_vendor($vendor_id)) {
- $order = new WC_Order($order_id);
- $shipping_dues = array('amount' => 0, 'tax' => 0);
- $shipping_methods = $order->get_shipping_methods();
- foreach ($shipping_methods as $key => $data) {
- if (!isset($data['vendor_id']) || (isset($data['vendor_id']) && $data['vendor_id'] != $vendor_id)) continue;
- $vendor_owed_shipping = $data['cost']; // this is their total shipping due for the entire order
- $number_items_pkg = count(explode('|', $data['product_ids']));
- $shipping_dues['amount'] = (($data['cost'] / $number_items_pkg) * $product['qty']);
- $shipping_dues['tax'] = $data['taxes'];
- break;
- }
- }
- return $shipping_dues;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement