first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,435 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Shipping_Shipment' ) ) {
/**
* Class WPDesk_Flexible_Shipping_Shipment
*/
abstract class WPDesk_Flexible_Shipping_Shipment {
const STATUS_FS_NEW = 'fs-new';
const STATUS_FS_CREATED = 'fs-created';
const STATUS_FS_CONFIRMED = 'fs-confirmed';
const STATUS_FS_FAILED = 'fs-failed';
const STATUS_FS_MANIFEST = 'fs-manifest';
const CREATED_VIA = 'created_via';
const SENT_VIA_METABOX = 'metabox';
const SENT_VIA_BULK = 'bulk';
const SENT_VIA_AUTO = 'auto';
const SENT_VIA = '_sent_via';
const NOT_SET = 'not_set';
/**
* @var int
*/
private $id;
/**
* @var WP_Post
* Post assigned to shipment
*/
private $post;
/**
* @var null|WC_Order
* WC_Order assigned to shipment
*/
private $order = null;
/**
* @var bool
* True if assigned post ich changed. Used when saving post
*/
private $save_post = false;
/**
* @var null
* Holds old status when shipment status is changed
*/
private $old_status = null;
/**
* @var bool
* True when status changing
*/
private $status_changed = false;
/**
* @var array
* Shipment metadata (from postmeta table)
*/
private $meta_data = array();
/**
* @var bool
* True when shipment metadata loaded
*/
private $meta_data_loaded = false;
/**
* @var array
* Holds changed metadata keys. Used when saving shipment
*/
private $meta_data_save_keys = array();
/**
* @var string
* Context for order metabox
*/
private $order_metabox_context = 'side';
/**
* WPDesk_Flexible_Shipping_Shipment constructor.
*
* @param int|WP_Post|WPDesk_Flexible_Shipping_Shipment $shipment Shipment or shipment ID.
* @param WC_Order|null $order Order.
*/
public function __construct( $shipment, $order = null ) {
if ( is_numeric( $shipment ) ) {
$this->id = absint( $shipment );
$this->post = get_post( $this->id );
} elseif ( $shipment instanceof WPDesk_Flexible_Shipping_Shipment ) {
$this->id = absint( $shipment->get_id() );
$this->post = $shipment->get_post();
} elseif ( isset( $shipment->ID ) ) {
$this->id = absint( $shipment->ID );
$this->post = $shipment;
}
$this->order = $order;
}
/**
* @return mixed
*/
public function get_id() {
return $this->id;
}
/**
* @return mixed
*/
public function get_post() {
return $this->post;
}
/**
* @return WC_Order
*/
public function get_order() {
if ( $this->order == null ) {
$this->order = wc_get_order( $this->post->post_parent );
}
return $this->order;
}
/**
* @return int
*/
public function get_order_id() {
return $this->post->post_parent;
}
/**
* @return string
*/
public function get_order_metabox_context() {
return $this->order_metabox_context;
}
/**
* @param string $order_metabox_context
*/
public function set_order_metabox_context( $order_metabox_context ) {
$this->order_metabox_context = $order_metabox_context;
}
/**
* @param string $meta_key
* @param null|sting $default
* @return array|string|null
*/
public function get_meta( $meta_key = '', $default = null ) {
$this->load_meta_data();
if ( $meta_key == '' ) {
return $this->meta_data;
}
if ( isset( $this->meta_data[$meta_key] ) ) {
return maybe_unserialize( $this->meta_data[$meta_key][0] );
}
else {
return $default;
}
return null;
}
/**
* @param string $meta_key
* @param int|string|array|object|null $value
*/
public function set_meta( $meta_key, $value ) {
$this->load_meta_data();
if ( !isset( $this->meta_data[$meta_key] ) ) {
$this->meta_data[$meta_key] = array();
}
$this->meta_data[$meta_key][0] = $value;
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* @param string $meta_key
*/
public function delete_meta( $meta_key ) {
unset( $this->meta_data[$meta_key] );
$this->meta_data_save_keys[$meta_key] = $meta_key;
}
/**
* Saves shipment data to database.
*/
public function save() {
if ( $this->save_post ) {
wp_update_post($this->post);
$this->save_post = false;
}
foreach ( $this->meta_data_save_keys as $key ) {
if ( isset( $this->meta_data[$key] ) ) {
update_post_meta( $this->id, $key, $this->meta_data[$key][0] );
}
else {
delete_post_meta( $this->id, $key );
}
unset( $this->meta_data_save_keys[$key] );
}
if ( $this->status_changed ) {
do_action( 'flexible_shipping_shipment_status_updated', $this->old_status, $this->post->post_status, $this );
$this->status_changed = false;
$this->old_status = null;
}
}
/**
* Loads all meta data from postmeta
*/
public function load_meta_data() {
if ( !$this->meta_data_loaded ) {
$this->meta_data = get_post_meta( $this->id );
$this->meta_data_loaded = true;
}
}
/**
* @return string|null
* Returns integration assigned to shipment
*/
public function get_integration() {
return $this->get_meta( '_integration' );
}
/**
* @return string
* Returns URL for admin metabox for this shipment
*/
public function get_order_metabox_url() {
return admin_url( 'post.php?post=' . $this->get_order_id() . '&action=edit#shipment_meta_box_' . $this->get_id() );
}
/**
* @return string
*/
public function get_status() {
return $this->post->post_status;
}
/**
* @return string
*/
public function get_status_for_shipping_column() {
$statuses = array(
self::STATUS_FS_NEW => 'new',
self::STATUS_FS_CREATED => 'created',
self::STATUS_FS_CONFIRMED => 'confirmed',
self::STATUS_FS_FAILED => 'error',
self::STATUS_FS_MANIFEST => 'manifest',
);
return $statuses[$this->get_status()];
}
/**
* @return null|string
* Returns URL for label
*/
public function get_label_url() {
if ( in_array( $this->get_status(), array( self::STATUS_FS_NEW, self::STATUS_FS_CREATED, self::STATUS_FS_FAILED ) ) ) {
return null;
}
$label_url = '?flexible_shipping_get_label=' . $this->get_id() . '&nonce=' . wp_create_nonce( 'flexible_shipping_get_label' ) ;
return site_url( $label_url );
}
/**
* @param string $new_status
*/
public function update_status( $new_status ) {
$this->old_status = $this->post->post_status;
$this->post->post_status = $new_status;
$this->save_post = true;
$this->status_changed = true;
}
public function add_to_manifest( WPDesk_Flexible_Shipping_Manifest $manifest ) {
$this->set_meta( '_manifest', $manifest->get_id() );
}
public function label_avaliable() {
if ( in_array( $this->get_status(), array( self::STATUS_FS_CONFIRMED, self::STATUS_FS_MANIFEST ) ) ) {
return true;
}
return false;
}
/**
* Displays shipping column in orders list view.
* Must be overwritten!
*/
public function shipping_column() {
echo _( 'Please override shipping_column method!', 'flexible-shipping' );
echo '<pre>';
print_r( $this->post );
echo '</pre>';
echo '<pre>';
print_r( $this->meta_data );
echo '</pre>';
}
/**
* Is status fs-new?
*
* @return bool
*/
public function is_status_fs_new() {
return self::STATUS_FS_NEW === $this->get_status();
}
/**
* Is status fs-created?
*
* @return bool
*/
public function is_status_fs_created() {
return self::STATUS_FS_CREATED === $this->get_status();
}
/**
* Is status fs-confirmed?
*
* @return bool
*/
public function is_status_fs_confirmed() {
return self::STATUS_FS_CONFIRMED === $this->get_status();
}
/**
* Is status fs-failed?
*
* @return bool
*/
public function is_status_fs_failed() {
return self::STATUS_FS_FAILED === $this->get_status();
}
/**
* Is status fs-manifest?
*
* @return bool
*/
public function is_status_fs_manifest() {
return self::STATUS_FS_MANIFEST === $this->get_status();
}
/**
* Set created via.
*
* @param string $created_via Created via.
*/
public function set_created_via( $created_via ) {
$this->set_meta( self::CREATED_VIA, $created_via );
}
/**
* Set created via checkout.
*/
public function set_created_via_checkout() {
$this->set_created_via( 'checkout' );
}
/**
* Set created via add shipment.
*/
public function set_created_via_add_shipment() {
$this->set_created_via( 'add_shipment' );
}
/**
* Get created via.
*
* @return string
*/
public function get_created_via() {
return $this->get_meta( self::CREATED_VIA, self::NOT_SET );
}
/**
* Set sent via.
*
* @param string $sent_via
*/
public function set_sent_via( $sent_via ) {
$this->set_meta( self::SENT_VIA, $sent_via );
}
/**
* Set sent via bulk.
*/
public function set_sent_via_bulk() {
$this->set_sent_via( self::SENT_VIA_BULK );
}
/**
* Set sent via auto.
*/
public function set_sent_via_auto() {
$this->set_sent_via( self::SENT_VIA_AUTO );
}
/**
* Set sent via metabox.
*/
public function set_sent_via_metabox() {
$this->set_sent_via( self::SENT_VIA_METABOX );
}
/**
* Get sent via.
*
* @return string
*/
public function get_sent_via() {
return $this->get_meta( self::SENT_VIA, self::NOT_SET );
}
/**
* Get meta shipping method.
*
* @return WC_Order_Item_Shipping
*/
protected function get_meta_shipping_method() {
return $this->get_meta( '_shipping_method' );
}
}
}

View File

@@ -0,0 +1,344 @@
<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPDesk_Flexible_Shipping_Shipment_CPT {
const POST_TYPE_SHIPMENT = 'shipment';
private $plugin = null;
/**
* Is order processed on checkout?
*
* @var bool
*/
private $is_order_processed_on_checkout = false;
public function __construct( Flexible_Shipping_Plugin $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
public function hooks() {
add_action( 'init', array( $this, 'register_post_types' ), 20 );
add_action( 'init', array( $this, 'flexible_shipping_get_label' ), 9999999 );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 2 );
$last_priority = PHP_INT_MAX;
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'create_shipping_for_order' ), $last_priority );
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'woocommerce_order_details_after_order_table' ) );
add_action( 'woocommerce_email_after_order_table', array( $this, 'woocommerce_email_after_order_table' ), 10, 2 );
}
/**
* Register post types.
*/
public function register_post_types() {
if ( post_type_exists( self::POST_TYPE_SHIPMENT ) ) {
return;
}
register_post_type( self::POST_TYPE_SHIPMENT,
array(
'labels' => array(
'name' => __( 'Shipments', 'flexible-shipping' ),
'singular_name' => __( 'Shipment', 'flexible-shipping' ),
'menu_name' => __( 'Shipments', 'flexible-shipping' ),
'parent_item_colon' => '',
'all_items' => __( 'Shipments', 'flexible-shipping' ),
'view_item' => __( 'View Shipments', 'flexible-shipping' ),
'add_new_item' => __( 'Add new Shipment', 'flexible-shipping' ),
'add_new' => __( 'Add new Shipment', 'flexible-shipping' ),
'edit_item' => __( 'Edit Shipment', 'flexible-shipping' ),
'update_item' => __( 'Save Shipment', 'flexible-shipping' ),
'search_items' => __( 'Search Shipments', 'flexible-shipping' ),
'not_found' => __( 'Shipment not found', 'flexible-shipping' ),
'not_found_in_trash' => __( 'Shipment not found in trash', 'flexible-shipping' )
),
'description' => __( 'Shipments.', 'flexible-shipping' ),
'public' => false,
'show_ui' => false,
'capability_type' => 'post',
'capabilities' => array(),
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'query_var' => true,
'supports' => array( 'title' ),
'has_archive' => false,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-upload',
)
);
$shipment_statuses = apply_filters( 'flexible_shipping_register_shipment_statuses',
array(
'fs-new' => array(
'label' => _x( 'New', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'New <span class="count">(%s)</span>', 'New <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-created' => array(
'label' => _x( 'Created', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Created <span class="count">(%s)</span>', 'Created <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-confirmed' => array(
'label' => _x( 'Confirmed', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-manifest' => array(
'label' => _x( 'Manifest created', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
'fs-failed' => array(
'label' => _x( 'Failed', 'Shipment status', 'flexible-shipping' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>', 'flexible-shipping' ),
),
)
);
foreach ( $shipment_statuses as $shipment_status => $values ) {
register_post_status( $shipment_status, $values );
}
}
public function add_meta_boxes( $post_type, $post ) {
if ( $post_type == self::POST_TYPE_SHIPMENT ) {
add_meta_box(
'shipment_meta_box',
__('Shipment data', 'flexible-shipping'),
array( $this, 'metabox' ),
'shipment',
'normal',
'high'
);
}
if ( $post_type == 'shop_order' ) {
$shipments = fs_get_order_shipments( $post->ID );
foreach ( $shipments as $shipment ) {
$args = array( 'shipment' => $shipment );
add_meta_box(
'shipment_meta_box_' . $shipment->get_id(),
$shipment->get_order_metabox_title(),
array( $this, 'order_metabox' ),
'shop_order',
$shipment->get_order_metabox_context(),
'default',
$args
);
}
}
}
public function order_metabox( $post, $args ) {
/** @var WPDesk_Flexible_Shipping_Shipment $shipment */
$shipment = $args['args']['shipment'];
$shipment_id = $shipment->get_id();
$message = $shipment->get_error_message();
$message_heading = $shipment->get_order_metabox_title();
$message_css_style = '';
include( 'views/order-metabox.php' );
}
public function metabox() {
global $post;
echo '<pre>';
print_r( $post );
echo '</pre>';
$meta_data = get_post_meta( $post->ID );
foreach ( $meta_data as $key => $val ) {
echo '<pre>';
echo $key;
echo ' = ';
print_r( maybe_unserialize( $val[0] ) );
echo '</pre>';
}
}
public function flexible_shipping_get_label() {
if ( !empty( $_GET['flexible_shipping_get_label'] ) && !empty( $_GET['nonce'] ) ) {
$nonce = $_GET['nonce'];
if ( !wp_verify_nonce( $nonce, 'flexible_shipping_get_label' ) ) {
echo __( 'Invalid nonce!', 'flexible-shipping' );
exit;
}
try {
$shipment_id = $_GET['flexible_shipping_get_label'];
$shipment = fs_get_shipment( $shipment_id );
$label_data = $shipment->get_label();
header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=" . $label_data['file_name'] );
echo $label_data['content'];
}
catch ( Exception $e ) {
echo $e->getMessage();
}
exit();
}
}
/**
* Get Flexible Shipping method from order shipping method meta data.
*
* @param WC_Order_Item_Shipping $shipping_method
*
* @return array
*/
private function get_fs_method_from_order_shipping_method( $shipping_method ) {
$fs_method = array();
if ( version_compare( WC_VERSION, '2.6', '<' ) ) {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['method_id'] )
&& isset( $shipping_method['item_meta']['method_id'][0] )
) {
$all_shipping_methods = flexible_shipping_get_all_shipping_methods();
$flexible_shipping = $all_shipping_methods['flexible_shipping'];
$flexible_shipping_rates = $flexible_shipping->get_all_rates();
$fs_method = $flexible_shipping_rates[ $shipping_method['item_meta']['method_id'][0] ];
}
}
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['_fs_method'] )
&& isset( $shipping_method['item_meta']['_fs_method'][0] )
) {
$fs_method = unserialize( $shipping_method['item_meta']['_fs_method'][0] );
}
} else {
if ( isset( $shipping_method['item_meta'] )
&& isset( $shipping_method['item_meta']['_fs_method'] )
) {
$fs_method = $shipping_method['item_meta']['_fs_method'];
}
}
return $fs_method;
}
/**
* Create shipment for order and shipping method.
*
* @param WC_Order $order Order.
* @param array $fs_method Flexible Shipping shipping method.
* @param string $shipping_id Shipping Id.
* @param WC_Order_Item_Shipping $shipping_method Shipping method.
* @param array $packages Packages.
* @param int $package_id Package Id.
*
* @return WPDesk_Flexible_Shipping_Shipment
*/
private function create_shipment_for_order_and_fs_shipping_method(
WC_Order $order,
array $fs_method,
$shipping_id,
WC_Order_Item_Shipping $shipping_method,
array $packages,
$package_id
) {
$shipment = fs_create_shipment( $order, $fs_method );
$shipment->set_meta( '_fs_method', $fs_method );
$shipment->set_meta( '_shipping_id', $shipping_id );
$shipment->set_meta( '_shipping_method', $shipping_method );
$shipment->set_meta( '_package', $packages[ $package_id ] );
$shipment->set_meta( '_packages', $packages );
$shipment->set_created_via_checkout();
$shipment->checkout( $fs_method, $packages[ $package_id ] );
$shipment->save();
return $shipment;
}
/**
* Create shipping for order.
*
* @param $order_id
*/
public function create_shipping_for_order( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order && ! $this->is_order_processed_on_checkout ) {
$mutex = \FSVendor\WPDesk\Mutex\WordpressPostMutex::fromOrder( $order );
$mutex->acquireLock();
$shipments = fs_get_order_shipments( $order_id );
if ( 0 === count( $shipments ) ) {
$this->is_order_processed_on_checkout = true;
$order_shipping_methods = $order->get_shipping_methods();
$packages = WC()->shipping->get_packages();
$package_id = - 1;
global $fs_package_id;
foreach ( $order_shipping_methods as $shipping_id => $shipping_method ) {
$package_id ++;
$fs_package_id = $package_id;
$fs_method = $this->get_fs_method_from_order_shipping_method( $shipping_method );
if ( ! empty( $fs_method['method_integration'] ) ) {
if ( fs_shipment_integration_exists( $fs_method['method_integration'] ) ) {
$shipment = $this->create_shipment_for_order_and_fs_shipping_method(
$order, $fs_method, $shipping_id, $shipping_method, $packages, $package_id
);
}
}
}
}
$mutex->releaseLock();
}
}
/**
* Hook woocommerce_order_details_after_order_table.
*
* @param WC_Abstract_Order $order Order.
*/
public function woocommerce_order_details_after_order_table( $order ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$shipments = fs_get_order_shipments( $order_id );
foreach ( $shipments as $shipment ) {
echo $shipment->get_after_order_table();
}
}
public function woocommerce_email_after_order_table( $order, $sent_to_admin ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$shipments = fs_get_order_shipments( $order_id );
foreach ( $shipments as $shipment ) {
echo $shipment->get_email_after_order_table();
}
}
}

View File

@@ -0,0 +1,126 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function fs_shipment_integration_exists( $integration ) {
$class_name = apply_filters( 'flexible_shipping_shipment_class', 'WPDesk_Flexible_Shipping_Shipment' . '_' . $integration, $integration );
if ( class_exists( $class_name ) ) {
return true;
}
return false;
}
/**
* @param WC_Abstract_Order $order Order.
* @param array $fs_method Flexible shipping method.
*
* @return WPDesk_Flexible_Shipping_Shipment
*/
function fs_create_shipment( $order, $fs_method ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$order_id = $order->id;
}
else {
$order_id = $order->get_id();
}
$integration = $fs_method['method_integration'];
$post_title = sprintf( __( 'Shipment for order %s, %s', 'flexible-shipping' ), $order_id, $integration );
$post_title = apply_filters( 'flexible_shipping_shipment_post_title_'. $integration, $post_title, $fs_method );
$shipment_post = array(
'post_title' => $post_title,
'post_type' => 'shipment',
'post_status' => 'fs-new',
'post_parent' => $order_id
);
$shipment_id = wp_insert_post( $shipment_post );
update_post_meta( $shipment_id, '_integration', $integration );
return fs_get_shipment( $shipment_id, $order );
}
/**
* Returns shipments for order.
* Shipments are ordered from oldest to newest.
*
* @param $order_id
* @param string|null $integration
* @return WPDesk_Flexible_Shipping_Shipment_Interface[]
*/
function fs_get_order_shipments( $order_id, $integration = null ) {
$shipments_posts_query = array(
'posts_per_page' => -1,
'post_parent' => $order_id,
'post_type' => 'shipment',
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'ASC',
);
if ( !empty( $integration ) ) {
$shipments_posts_query['meta_key'] = '_integration';
$shipments_posts_query['meta_value'] = $integration;
}
$shipments_posts = get_posts( $shipments_posts_query );
$shipments = array();
if ( count( $shipments_posts ) ) {
$order = wc_get_order($order_id);
foreach ($shipments_posts as $shipment_post) {
$integration = get_post_meta($shipment_post->ID, '_integration', true);
if ( fs_shipment_integration_exists( $integration ) ) {
$shipments[] = fs_get_shipment($shipment_post->ID, $order);
}
}
}
return $shipments;
}
/**
* Get shipment.
*
* @param int $shipment_id Shipment id.
* @param WC_Order|null $order Order.
* @return WPDesk_Flexible_Shipping_Shipment
*/
function fs_get_shipment( $shipment_id, $order = null ) {
$integration = get_post_meta( $shipment_id, '_integration', true );
$class_name = 'WPDesk_Flexible_Shipping_Shipment';
$integration_class_name = 'WPDesk_Flexible_Shipping_Shipment' . '_' . $integration;
$integration_class_name = apply_filters( 'flexible_shipping_shipment_class', $integration_class_name, $integration );
if ( class_exists( $integration_class_name ) ) {
$class_name = $integration_class_name;
}
return new $class_name( $shipment_id, $order );
}
/**
* @param WC_Abstract_Order $order Order.
*
* @return float
*/
function fs_calculate_order_weight( $order ) {
$weight = 0;
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
$product = $order->get_product_from_item( $item );
}
else {
$product = $item->get_product();
}
$product_weight = $product->get_weight();
if ( ! $product->is_virtual() && is_numeric( $product_weight ) ) {
$weight += floatval( $product->get_weight() ) * floatval( $item['qty'] );
}
}
}
}
return $weight;
}
function fs_calculate_package_weight( $package ) {
$weight = 0;
foreach( $package['contents'] as $item ) {
$weight += floatval( $item['data']->get_weight() ) * floatval( $item['quantity'] );
}
return $weight;
}

View File

@@ -0,0 +1,76 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! interface_exists( 'WPDesk_Flexible_Shipping_Shipment_Interface' ) ) {
interface WPDesk_Flexible_Shipping_Shipment_Interface {
/**
* @param array $fs_method
* @param array $package
* @return void
* Executes on woocommerce checkout when order is created
*/
public function checkout( array $fs_method, $package );
/**
* @return void
* Displays metabox in woocommerce order
*/
public function order_metabox();
/**
* @return string
* Returns woocommerce metabox title
*/
public function get_order_metabox_title();
/**
* @param string $action
* @param array $data
* @return void
* Executes on ajax request. $data contains all woocommerce order metabox fields values from metabox generated in order_metabox() method.
*/
public function ajax_request( $action, $data );
/**
* @return string
* Returns error message
*/
public function get_error_message();
/**
* @return string
* Returns tracking number for shipment
*/
public function get_tracking_number();
/**
* @return string
* Returns tracking URL for shipping
*/
public function get_tracking_url();
/**
* @return array
* Return label data foe shipping in array:
* 'label_format' => 'pdf'
* 'content' => pdf content,
* 'file_name' => file name for label
*/
public function get_label();
/**
* @return mixed
*/
public function get_after_order_table();
/**
* @return mixed
*/
public function get_email_after_order_table();
}
}

View File

@@ -0,0 +1,9 @@
<div class="flexible_shipping_shipment" id="flexible_shipping_shipment_<?php echo $shipment->get_id(); ?>" data-id="<?php echo $shipment_id; ?>">
<?php wp_nonce_field( 'flexible_shipping_shipment_nonce', 'flexible_shipping_shipment_nonce_' . $shipment_id, false ); ?>
<div class="flexible_shipping_shipment_content">
<?php $shipment->order_metabox(); ?>
</div>
<div class="flexible_shipping_shipment_message flexible_shipping_shipment_message_error" style="<?php echo $message_css_style; ?>">
<?php echo $message; ?>
</div>
</div>