Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making WooCommerce meta boxes and registering post meta for products #44

Merged
merged 18 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions NineteenEightyWoo.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

/**
* Run the PSR-4 autoloader
*
* This will replace any other loading mechanism in the codebase once it has
* been refactored.
*/
declare(strict_types = 1);

require plugin_dir_path( __FILE__ ) . './vendor/autoload.php';

$new_admin = new NineteenEightyFour\NineteenEightyWoo\Admin();

$new_woo_metaboxes = new NineteenEightyFour\NineteenEightyWoo\RegisterPostMeta();
$new_rest_settings = new NineteenEightyFour\NineteenEightyWoo\Rest\Settings();
$new_woo_metaboxes = new NineteenEightyFour\NineteenEightyWoo\WooMetaboxes();
296 changes: 148 additions & 148 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class NineteenEightyWoo {
static settingsErrorIndicator() {
return document.querySelector( '#nineteen-eighty-woo-settings-error' );
}
static settingsErrorIndicator() {
return document.querySelector( '#nineteen-eighty-woo-settings-error' );
}
static settingsLoader() {
return document.querySelector( '#nineteen-eighty-woo-settings-loader' );
}
Expand Down
4 changes: 2 additions & 2 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function __construct() {
public static function add_menu_page(): void {
add_submenu_page(
'woocommerce',
__( '1984 dkPlus Connection', 'NineteenEightyWoo' ),
__( 'dkPlus Connection', 'NineteenEightyWoo' ),
__( '1984 DK Connection', 'NineteenEightyWoo' ),
__( 'DK Connection', 'NineteenEightyWoo' ),
'manage_options',
'NineteenEightyWoo',
array( __CLASS__, 'render_admin_page' )
Expand Down
106 changes: 106 additions & 0 deletions src/RegisterPostMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types = 1);

namespace NineteenEightyFour\NineteenEightyWoo;

use WP_Post;
use WP_REST_Request;
use WC_Product;
use WC_Data;

/**
* The RegisterPostMeta class
*
* Registers the post_meta values and Product props needed for the plugin to work.
*/
class RegisterPostMeta {
/**
* The RegisterPostMeta constructor
*
* Registers the post_meta values.
*/
public function __construct() {
register_post_meta(
'product',
'1984_woo_dk_price_sync',
array(
'type' => 'boolean',
'description' => 'Wether to enable price sync with DK',
'single' => true,
'default' => true,
),
);

register_post_meta(
'product',
'1984_woo_dk_stock_sync',
array(
'type' => 'boolean',
'description' => 'Wether to enable stock sync with DK',
'single' => true,
'default' => true,
),
);

add_action(
'save_post_product',
array( __CLASS__, 'set_default_product_props' ),
10,
3
);

add_action(
'woocommerce_rest_insert_product_object',
array( __CLASS__, 'set_default_product_props_in_rest' ),
10,
3
);
}

/**
* Set default product pops on creation
*
* Products are created and receive a database ID as soon as the user opens
* the traditional "add new" sidebar menu item. As that happens, we set the
* `manage_stock`
*
* @param int $id The Post ID.
* @param WP_Post $post The post object (unused).
* @param bool $update False if the post is being created, true if updating.
*/
public function set_default_product_props(
int $id,
WP_Post $post,
bool $update
): void {
if ( false === $update ) {
$product = new WC_Product( $id );
$product->set_manage_stock( true );
$product->save();
}
}

/**
* Set default product props when a product is created via the JSON API
*
* For some reason, we need to call a separate hook from save_post_product
* when the new block-ish WooCommerce Product Form (still in beta) is opened
* as a POST call to the WC Product REST endpoint is made.
*
* @param WC_Data $object the WC_Data object, representing the product.
* @param WP_REST_Request $request The REST request (unused).
* @param bool $creating True if the post is being created, false if not.
*/
public function set_default_product_props_in_rest(
WC_Data $object,
WP_REST_Request $request,
bool $creating
): void {
if ( true === $creating ) {
$product = new WC_Product( $object->get_id() );
$product->set_manage_stock( true );
$product->save();
}
}
}
18 changes: 18 additions & 0 deletions src/Rest/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public static function rest_api_callback(
$rest_body = $request->get_body();
$rest_json = json_decode( $rest_body );

$validator = new Validator();
$validation = $validator->validate( $rest_json, self::JSON_SCHEMA );

if ( true === $validation->hasError() ) {
return new WP_Error(
'bad_request',
'Bad Request',
array( 'status' => '400' ),
);
}

update_option( '1984_woo_dk_api_key', $rest_json->api_key );

foreach ( $rest_json->payment_methods as $p ) {
Expand All @@ -89,6 +100,13 @@ public static function rest_api_callback(
);
}

foreach ( $rest_json->payment_methods as $p ) {
update_option(
'1984_woo_dk_payment_method_' . $p->woo_id,
$p
);
}

return new WP_REST_Response( array( 'status' => 200 ) );
}

Expand Down
122 changes: 122 additions & 0 deletions src/WooMetaboxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types = 1);

namespace NineteenEightyFour\NineteenEightyWoo;

use WP_Post;
use WC_Product;

/**
* The WooMetaboxes class
*
* The WooMetaboxes class adds fields to the WooCommerce editor metaboxes that
* handle properties facilitating price and inventory sync with DK.
*/
class WooMetaboxes {
/**
* The constructor for the WooMetaboxes class
*/
public function __construct() {
add_action(
'woocommerce_product_options_pricing',
array( $this, 'render_product_options_pricing_partial' )
);

add_action(
'woocommerce_product_options_sku',
array( $this, 'render_product_options_sku_partial' )
);

add_action(
'save_post_product',
array( $this, 'save_product_meta' ),
10,
3
);
}

/**
* Render the pricing metabox partial
*/
public static function render_product_options_pricing_partial(): void {
require __DIR__ . '/../views/product_options_pricing_partial.php';
}

/**
* Render the SKU metabox partial
*/
public static function render_product_options_sku_partial(): void {
require __DIR__ . '/../views/product_options_sku_partial.php';
}

/**
* Save the NineteenEightyWoo related meta tags for a product using superglobals
*
* Fired during the `save_post_product` hook.
*
* @param int $id The post ID for the product.
* @param WP_Post $post The post object (unused).
* @param bool $update Wether the action is an update.
*/
public function save_product_meta( int $id, WP_Post $post, bool $update ): void {
if ( true === $update ) {
$this->save_price_sync_meta( $id );
$this->save_stock_sync_meta( $id );
}
}

/**
* Save the 1984_woo_dk_price_sync post meta from superglobals
*
* @param int $id The post ID for the product.
*/
public function save_price_sync_meta( int $id ): void {
if ( false === isset( $_POST['set_1984_woo_dk_price_sync_nonce'] ) ) {
return;
}
if (
false === wp_verify_nonce(
sanitize_text_field(
wp_unslash( $_POST['set_1984_woo_dk_price_sync_nonce'] )
),
'set_1984_woo_dk_price_sync'
)
) {
return;
}

if ( isset( $_POST['1984_woo_dk_price_sync'] ) ) {
update_post_meta( $id, '1984_woo_dk_price_sync', true );
} else {
update_post_meta( $id, '1984_woo_dk_price_sync', false );
}
}

/**
* Save the 1984_woo_dk_stock_sync post meta from superglobal
*
* @param int $id The post ID for the product.
*/
public function save_stock_sync_meta( int $id ): void {
if ( false === isset( $_POST['set_1984_woo_dk_stock_sync_nonce'] ) ) {
return;
}
if (
false === wp_verify_nonce(
sanitize_text_field(
wp_unslash( $_POST['set_1984_woo_dk_stock_sync_nonce'] )
),
'set_1984_woo_dk_stock_sync'
)
) {
return;
}

if ( isset( $_POST['1984_woo_dk_stock_sync'] ) ) {
update_post_meta( $id, '1984_woo_dk_stock_sync', true );
} else {
update_post_meta( $id, '1984_woo_dk_stock_sync', false );
}
}
}
9 changes: 9 additions & 0 deletions style/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ body.woocommerce_page_NineteenEightyWoo .wrap {
font-size: 1.17em;
}

#nineteen-eighty-woo-wrap #nineteen-eighty-woo-settings-error {
flex: 1;
margin-right: 5em;
}

#nineteen-eighty-woo-wrap #nineteen-eighty-woo-settings-error p {
font-size: 1.17em;
}

#nineteen-eighty-woo-wrap .loader {
margin-right: 1rem;
}
Expand Down
6 changes: 3 additions & 3 deletions views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class="wrap nineteen-eighty-woo-wrap"
>
<form id="nineteen-eighty-woo-settings-form" class="type-form" novalidate>
<h1 class="wp-heading-inline">
<?php esc_html_e( '1984 dkPlus Connection', 'NineteenEightyWoo' ); ?>
<?php esc_html_e( '1984 DK Connection', 'NineteenEightyWoo' ); ?>
</h1>
<section class="section">
<h2><?php esc_html_e( 'Authentication', 'NineteenEightyWoo' ); ?></h2>
Expand Down Expand Up @@ -100,7 +100,7 @@ class="regular-text payment-name"
required
/>
<p class="validity valid"><?php esc_html_e( 'Valid', 'NineteenEightyWoo' ); ?><span class="dashicons dashicons-yes"></span></p>
<p class="validity invalid"><?php esc_html_e( 'This is a required ', 'NineteenEightyWoo' ); ?></p>
<p class="validity invalid"><?php esc_html_e( 'This is a required field', 'NineteenEightyWoo' ); ?></p>
</td>
</tr>
<?php endforeach ?>
Expand Down Expand Up @@ -152,7 +152,7 @@ class="button button-primary button-hero"
<p>
<?php
esc_html_e(
'The 1984 dkPlus Connection Plugin for WooCommerce is developed, maintained and supported on goodwill basis by 1984 Hosting as free software without any guarantees or obligations and is not affiliated with or supported by DK hugbúnaður ehf.',
'The 1984 DK Connection Plugin for WooCommerce is developed, maintained and supported on goodwill basis by 1984 Hosting as free software without any guarantees or obligations and is not affiliated with or supported by DK hugbúnaður ehf.',
'NineteenEightyWoo'
);
?>
Expand Down
25 changes: 25 additions & 0 deletions views/product_options_pricing_partial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

?>

<div class="options_group">
<?php
global $post;
wp_nonce_field( 'set_1984_woo_dk_price_sync', 'set_1984_woo_dk_price_sync_nonce' );
woocommerce_wp_checkbox(
array(
'id' => '1984_woo_dk_price_sync',
'value' => get_post_meta( $post->ID, '1984_woo_dk_price_sync', true ) ? 'true' : 'false',
'label' => 'Sync Price with DK',
'description' => 'Enables the 1984 DK Connection plugin to sync the products\'s price between WooCommerce and dkPlus.',
'desc_tip' => true,
'cbvalue' => 'true',
),
);
?>
<p class="form-field">
In order for price sync to work, the <abbr title="Stock Keeping Unit">SKU</abbr> needs to be set and must equal the Item Code in DK. It is set in the <strong>Inventory Panel</strong>.
</p>
</div>
29 changes: 29 additions & 0 deletions views/product_options_sku_partial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

?>

<div class="options_group">
<?php
global $post;
wp_nonce_field( 'set_1984_woo_dk_stock_sync', 'set_1984_woo_dk_stock_sync_nonce' );
woocommerce_wp_checkbox(
array(
'id' => '1984_woo_dk_stock_sync',
'value' => get_post_meta( $post->ID, '1984_woo_dk_stock_sync', true ) ? 'true' : 'false',
'label' => 'Sync Inventory with DK',
'description' => 'Enables the 1984 DK Connection plugin to sync inventory status and stock quanity between WooCommerce and dkPlus.',
'desc_tip' => true,
'cbvalue' => 'true',
),
);
?>
<p class="form-field">
The <abbr title="Stock Keeping Unit">SKU</abbr> needs to be set and must
equal the <strong>Item Code</strong> in DK for any 1984 DK Sync
functionality to work.<br />
<strong>Stock management</strong> needs to be enabled for Inventory Sync
to work as well.
</p>
</div>
Loading