Der in den Partner-Shops installierte WooCommerce-Connector, der Bestellungen per REST an /api/wp/* uebertraegt (Kunde anlegen, Warenkorb, Status-Abgleich). Grundlage fuer AP-13 (Shop-Bestandsabzug beim Versand). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
200 lines
5.5 KiB
PHP
200 lines
5.5 KiB
PHP
<?php
|
|
if (! defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class PARTNERCENTER_API_Order_Forwarding_Button_MetaBox
|
|
*/
|
|
class PARTNERCENTER_API_Order_Forwarding_Button_MetaBox
|
|
{
|
|
/**
|
|
* PARTNERCENTER_API_Order_Forwarding_Button_MetaBox constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
|
|
if (is_admin()) {
|
|
add_action('load-post.php', [$this, 'init_metabox']);
|
|
add_action('load-post-new.php', [$this, 'init_metabox']);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Add methods to hooks.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function init_metabox()
|
|
{
|
|
|
|
add_action('add_meta_boxes', [$this, 'add_metabox']);
|
|
add_action('save_post', [$this, 'save_metabox'], 10, 2);
|
|
|
|
}
|
|
|
|
/**
|
|
* Register the meta box.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function add_metabox()
|
|
{
|
|
|
|
add_meta_box(
|
|
'partnercenter-order-forwarding-button',
|
|
__('Fulfillment by PARTNERCENTER', 'partnercenter-api'),
|
|
[$this, 'render_metabox'],
|
|
'shop_order',
|
|
'side',
|
|
'default'
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Callback to render the meta box content.
|
|
*
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function render_metabox($post)
|
|
{
|
|
|
|
// Add nonce for security and authentication.
|
|
wp_nonce_field('partnercenter_api_order_forwarding_nonce_action', 'partnercenter_api_order_forwarding_nonce');
|
|
|
|
// Retrieve an existing value from the database.
|
|
$order_send_status = intval(get_post_meta($post->ID, '_partnercenter_api_order_fulfillment_forwarded', true));
|
|
|
|
if ($order_send_status !== 0) { ?>
|
|
<p>
|
|
<?php
|
|
printf(__('Order send at %s', 'partnercenter-api'), date(get_option('date_format'), $order_send_status));
|
|
?>
|
|
</p>
|
|
<p>
|
|
<?php
|
|
// Get status into var
|
|
$order_send_status = get_post_meta($post->ID, '_partnercenter_api_order_status', true);
|
|
// Check status
|
|
switch ($order_send_status) {
|
|
case 'free':
|
|
_e('Self-shipping.', 'partnercenter-api');
|
|
break;
|
|
case 'open':
|
|
case 'process':
|
|
_e('Order waiting.', 'partnercenter-api');
|
|
break;
|
|
case 'sent':
|
|
case 'close':
|
|
_e('Order sent.', 'partnercenter-api');
|
|
break;
|
|
default:
|
|
_e('Unclear status.', 'partnercenter-api');
|
|
break;
|
|
}
|
|
?>
|
|
</p>
|
|
|
|
<?php
|
|
// Check if order is open
|
|
if ($order_send_status === 'open') {
|
|
// Show button to allow canceling order
|
|
?>
|
|
<p>
|
|
<button id="order_fulfillment_internal_retrieve" name="order_fulfillment_internal_retrieve"
|
|
class="button button-primary" value="yes">
|
|
<?php esc_html_e('Retrieve', 'aloe-vera-shipment-interface'); ?>
|
|
</button>
|
|
</p>
|
|
<?php } ?>
|
|
|
|
<?php } elseif (empty(get_post_meta($post->ID, '_partnercenter_api_order_canceled', true))) { ?>
|
|
|
|
<p>
|
|
<button id="partnercenter_api_order_fulfillment_forwarded" name="partnercenter_api_order_fulfillment_forwarded"
|
|
class="button button-primary" value="yes">
|
|
<?php _e('Send to PARTNERCENTER for fulfillment', 'partnercenter-api'); ?>
|
|
</button>
|
|
</p>
|
|
|
|
<?php } else { ?>
|
|
|
|
<p>
|
|
<?php
|
|
printf(__('Order canceled at PARTNERCENTER on %s', 'partnercenter-api'), date(get_option('date_format'), (int) get_post_meta($post->ID, '_partnercenter_api_order_canceled', true)));
|
|
?>
|
|
</p>
|
|
|
|
<?php }
|
|
|
|
}
|
|
|
|
/**
|
|
* Callback to handle metabox submission.
|
|
*
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function save_metabox($post_id, $post)
|
|
{
|
|
|
|
// Add nonce for security and authentication.
|
|
$nonce_name = isset($_POST['partnercenter_api_order_forwarding_nonce']) ? $_POST['partnercenter_api_order_forwarding_nonce'] : '';
|
|
$nonce_action = 'partnercenter_api_order_forwarding_nonce_action';
|
|
|
|
// Check if a nonce is set.
|
|
if (! isset($nonce_name)) {
|
|
return;
|
|
}
|
|
|
|
// Check if a nonce is valid.
|
|
if (! wp_verify_nonce($nonce_name, $nonce_action)) {
|
|
return;
|
|
}
|
|
|
|
// Check if the user has permissions to save data.
|
|
if (! current_user_can('edit_shop_orders', $post_id)) {
|
|
return;
|
|
}
|
|
|
|
// Check if it's not an autosave.
|
|
if (wp_is_post_autosave($post_id)) {
|
|
return;
|
|
}
|
|
|
|
// Check if it's not a revision.
|
|
if (wp_is_post_revision($post_id)) {
|
|
return;
|
|
}
|
|
|
|
// Sanitize and check if user wants to forward item
|
|
if (isset($_POST['partnercenter_api_order_fulfillment_forwarded']) && sanitize_title($_POST['partnercenter_api_order_fulfillment_forwarded']) === 'yes') {
|
|
|
|
/**
|
|
* Call action to allow plugins to react.
|
|
*
|
|
* @param int $order_id
|
|
*/
|
|
do_action('partnercenter_api_send_order_items', $post_id);
|
|
|
|
}
|
|
|
|
// Sanitize and check if user wants to retrieve item
|
|
if (isset($_POST['order_fulfillment_internal_retrieve']) && sanitize_title($_POST['order_fulfillment_internal_retrieve']) === 'yes') {
|
|
|
|
/**
|
|
* Call action to allow plugins to react.
|
|
*
|
|
* @param int $order_id
|
|
*/
|
|
do_action('partnercenter_api_retrieve_order', $post_id);
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
new PARTNERCENTER_API_Order_Forwarding_Button_MetaBox;
|