Skip to content

Commit

Permalink
Improve cart/checkout block usage check. Added update database tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Nov 11, 2024
1 parent f325eef commit ae0c326
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
5 changes: 5 additions & 0 deletions includes/admin/class-wc-gzd-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function tool_actions() {
'disable_food_options',
'install_oss',
'install_ts',
'update_database',
);

if ( current_user_can( 'manage_woocommerce' ) ) {
Expand Down Expand Up @@ -1136,6 +1137,10 @@ protected function check_disable_notices() {
}
}

protected function check_update_database() {
WC_GZD_Install::update();
}

public function disable_small_business_options() {
// Update woocommerce options to show tax
update_option( 'woocommerce_calc_taxes', 'yes' );
Expand Down
7 changes: 7 additions & 0 deletions includes/admin/views/html-page-status-germanized.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'wc-gzd-check-disable_notices' => true ) ), 'wc-gzd-check-disable_notices' ) ); ?>" class="button button-secondary"><?php echo ( 'yes' === get_option( 'woocommerce_gzd_disable_notices' ) ? esc_html__( 'Enable notices', 'woocommerce-germanized' ) : esc_html__( 'Disable notices', 'woocommerce-germanized' ) ); ?></a>
</td>
</tr>
<tr>
<td><?php esc_html_e( 'Update database', 'woocommerce-germanized' ); ?></td>
<td class="help"><?php echo wc_help_tip( esc_attr( __( 'Force (re-) running database updates, necessary, e.g. in case your database version differs from the current plugin version.', 'woocommerce-germanized' ) ) ); ?></td>
<td>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'wc-gzd-check-update_database' => true ) ), 'wc-gzd-check-update_database' ) ); ?>" class="button button-secondary"><?php echo esc_html__( 'Update database', 'woocommerce-germanized' ); ?></a>
</td>
</tr>
<tr>
<td><?php esc_html_e( 'Extended debug mode', 'woocommerce-germanized' ); ?></td>
<td class="help"><?php echo wc_help_tip( esc_attr( __( 'Enable/disable extended debug mode via log files. Check your logs via WooCommerce > Status > Logs.', 'woocommerce-germanized' ) ) ); ?></td>
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-gzd-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private static function needs_db_update() {
/**
* Handle updates
*/
private static function update() {
public static function update() {
$current_db_version = get_option( 'woocommerce_gzd_db_version', null );

if ( ! is_null( $current_db_version ) && ! empty( $current_db_version ) ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-gzd-payment-gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function set_fields( $fields ) {

public function get_current_gateway() {
$current_gateway = WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '';
$has_block_checkout = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || WC()->is_rest_api_request();
$has_block_checkout = \Vendidero\Germanized\Utilities\CartCheckout::uses_checkout_block() || WC()->is_rest_api_request();

if ( $has_block_checkout ) {
$current_gateway = WC()->session ? WC()->session->get( 'wc_gzd_blocks_chosen_payment_method', '' ) : '';
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Vendidero\Germanized\Blocks\PaymentGateways\DirectDebit;
use Vendidero\Germanized\Blocks\PaymentGateways\Invoice;
use Vendidero\Germanized\Package;
use Vendidero\Germanized\Utilities\CartCheckout;

final class Checkout {

Expand Down Expand Up @@ -57,7 +58,7 @@ function ( $is_visible, $checkbox_id ) {
add_filter(
'woocommerce_get_item_data',
function ( $item_data, $item ) {
$needs_price_labels = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || WC()->is_rest_api_request();
$needs_price_labels = CartCheckout::uses_checkout_block() || CartCheckout::uses_cart_block() || WC()->is_rest_api_request();

if ( apply_filters( 'woocommerce_gzd_cart_checkout_needs_block_price_labels', $needs_price_labels ) ) {
$labels = wc_gzd_get_checkout_shopmarks();
Expand Down
43 changes: 43 additions & 0 deletions src/Utilities/CartCheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Vendidero\Germanized\Utilities;

class CartCheckout {

/**
* Checks if the default cart page is using the Cart block.
*
* @return bool true if the WC cart page is using the Cart block.
*/
public static function uses_cart_block() {
if ( function_exists( 'wc_current_theme_is_fse_theme' ) && wc_current_theme_is_fse_theme() && is_callable( array( '\Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils', 'get_block_templates_from_db' ) ) ) {
$templates_from_db = \Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils::get_block_templates_from_db( array( 'cart', 'page-cart' ), 'wp_template' );
foreach ( $templates_from_db as $template ) {
if ( has_block( 'woocommerce/cart', $template->content ) ) {
return true;
}
}
}
$cart_page_id = wc_get_page_id( 'cart' );

return $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
}

/**
* Checks if the default checkout page is using the Checkout block.
*
* @return bool true if the WC checkout page is using the Checkout block.
*/
public static function uses_checkout_block() {
if ( function_exists( 'wc_current_theme_is_fse_theme' ) && wc_current_theme_is_fse_theme() && is_callable( array( '\Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils', 'get_block_templates_from_db' ) ) ) {
$templates_from_db = \Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils::get_block_templates_from_db( array( 'checkout', 'page-checkout' ), 'wp_template' );
foreach ( $templates_from_db as $template ) {
if ( has_block( 'woocommerce/checkout', $template->content ) ) {
return true;
}
}
}
$checkout_page_id = wc_get_page_id( 'checkout' );

return $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
}
}

0 comments on commit ae0c326

Please sign in to comment.