From 835a6372934a692ffccd205a632c6914922f6b53 Mon Sep 17 00:00:00 2001 From: Andrew Lima Date: Wed, 1 Dec 2021 10:09:43 +0200 Subject: [PATCH] Final commit before launch. Hopefully. --- .gitignore | 1 + classes/admin.php | 79 +++++++++++++------- classes/class-frontend.php | 28 ++----- templates/name-your-price-single-product.php | 2 +- yh-name-your-price.php | 6 +- 5 files changed, 64 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index 550ce9c..b34b13c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .DS_Store assets/.DS_Store assets/.DS_Store +assets/.DS_Store diff --git a/classes/admin.php b/classes/admin.php index 635bff7..d4f2bf5 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -14,7 +14,7 @@ public function __construct() { // Product meta setup and saving. add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'woocommerce_product_write_panel_tabs' ) ); add_action( 'woocommerce_product_data_panels', array( $this, 'woocommerce_product_data_panels' ) ); - add_action( 'woocommerce_process_product_meta', array( $this, 'woocommerce_process_product_meta' ) ); + add_action( 'woocommerce_process_product_meta', array( $this, 'woocommerce_process_product_meta' ), 50 ); // JS for admin add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); @@ -122,35 +122,58 @@ public function woocommerce_product_data_panels() { public function woocommerce_process_product_meta() { global $post_id; - if ( isset( $_POST['_yh_is_nyp_product'] ) - && !empty( $_POST['_yh_is_nyp_product'] ) - && $_POST['_yh_is_nyp_product'] !== 'no' ) { - $is_nyp = 1; - } else { - $is_nyp = 0; - } - - if ( isset ( $is_nyp ) ) { - update_post_meta( $post_id, '_yh_is_nyp_product', $is_nyp ); - } - - // Update label value. - if ( isset( $_POST['_yh_nyp_label'] ) ) { - $label = sanitize_text_field( $_POST['_yh_nyp_label'] ); - update_post_meta( $post_id, '_yh_nyp_label', $label ); - } + if ( isset( $_POST['_yh_is_nyp_product'] ) + && !empty( $_POST['_yh_is_nyp_product'] ) + && $_POST['_yh_is_nyp_product'] !== 'no' ) { + $is_nyp = 1; + } else { + $is_nyp = 0; + } + + //Check post meta now + if ( get_post_meta( $post_id, '_yh_is_nyp_product', true ) ) { + $is_nyp = get_post_meta( $post_id, '_yh_is_nyp_product', true ); + } - // Update min value - if ( isset( $_POST['_yh_min_value'] ) ) { - $min_value = (float) $_POST['_yh_min_value']; - update_post_meta( $post_id, '_yh_min_value', $min_value ); - } + // Let's not try to update things if it's a Name Your Price product. + if ( ! $is_nyp ) { + return; + } - // Update max value - if ( isset( $_POST['_yh_max_value'] ) ) { - $max_value = (float) $_POST['_yh_max_value']; - update_post_meta( $post_id, '_yh_max_value', $max_value ); - } + if ( isset ( $is_nyp ) ) { + update_post_meta( $post_id, '_yh_is_nyp_product', $is_nyp ); + } + + // Update label value. + if ( isset( $_POST['_yh_nyp_label'] ) ) { + $label = sanitize_text_field( $_POST['_yh_nyp_label'] ); + update_post_meta( $post_id, '_yh_nyp_label', $label ); + } + + // Update min value + if ( isset( $_POST['_yh_min_value'] ) ) { + $min_value = (float) $_POST['_yh_min_value']; + update_post_meta( $post_id, '_yh_min_value', $min_value ); + } + + // Update max value + if ( isset( $_POST['_yh_max_value'] ) ) { + $max_value = (float) $_POST['_yh_max_value']; + update_post_meta( $post_id, '_yh_max_value', $max_value ); + } + + if ( empty( $_POST['_regular_price'] ) || get_post_meta( $post_id, '_regular_price', true ) == '' ) { + if ( ! empty( $max_value ) ) { + update_post_meta( $post_id, '_regular_price', $max_value ); + update_post_meta( $post_id, '_price', $max_value ); + } elseif ( ! empty( $min_value ) ) { + update_post_meta( $post_id, '_regular_price', $max_value ); + update_post_meta( $post_id, '_price', $max_value ); + } else { + update_post_meta( $post_id, '_regular_price', '999' ); //Default value. + update_post_meta( $post_id, '_price', '999' ); //Default value. + } + } } diff --git a/classes/class-frontend.php b/classes/class-frontend.php index 0682030..439ea32 100644 --- a/classes/class-frontend.php +++ b/classes/class-frontend.php @@ -7,12 +7,9 @@ class YH_Name_Your_Price_Frontend { * @since 1.0.0 */ public function __construct() { - add_action( 'woocommerce_before_add_to_cart_form', array( $this, 'yh_nyp_load_custom_single_product_template' ) ); + add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'yh_nyp_load_custom_single_product_template' ) ); add_filter( 'woocommerce_get_price_html', array( $this, 'hide_default_price_html' ), 20, 2 ); - //Force is_purchasable if no price is set but is a NYP product. - add_filter( 'woocommerce_is_purchasable', array( $this, 'set_is_purchasable' ) ); - //Cart Filters add_filter( 'woocommerce_add_cart_item_data', array( $this, 'yh_nyp_add_cart_item_data' ), 20, 3 ); add_filter( 'woocommerce_add_cart_item', array( $this, 'yh_nyp_add_cart_item' ), 20, 1 ); @@ -24,22 +21,7 @@ public function __construct() { } - /** - * Handle products where there is no regular or sale price but is a "Name Your Price" product. - * @since 1.0.0 - */ - public function set_is_purchasable( $is_purchasable ) { - global $product; - - $product_id = YH_Name_Your_Price::get_product_id( $product ); - - if ( YH_Name_Your_Price::is_nyp_product( $product_id ) ) { - $is_purchasable = true; - } - - return $is_purchasable; - } /** * Load custom product for input fields. @@ -103,7 +85,8 @@ public function hide_default_price_html( $price, $product ) { */ public function yh_nyp_add_cart_item( $cart_item_data ) { - $product_id = $cart_item_data['variation_id'] ? $cart_item_data['variation_id'] : $cart_item_data['product_id']; + $product_id = ! empty( $cart_item_data['variation_id'] ) ? $cart_item_data['variation_id'] : $cart_item_data['product_id']; + // Check to see if the product is a Name Your Price product, if so let's figure out how much to set it to. if ( YH_Name_Your_Price::is_nyp_product( $product_id ) ) { @@ -111,10 +94,9 @@ public function yh_nyp_add_cart_item( $cart_item_data ) { $product = wc_get_product( $product_id ); if ( isset( $cart_item_data['yh_nyp_amount'] ) ) { - + $product = $cart_item_data['data']; $product->set_price( $cart_item_data['yh_nyp_amount'] ); - } } @@ -233,6 +215,8 @@ public function get_cart_item_from_session( $cart_item, $values ) { * @since 1.0.0 */ public function loop_add_to_cart_link( $link, $product ) { + + $product_id = YH_Name_Your_Price::get_product_id( $product ); if ( YH_Name_Your_Price::is_nyp_product( $product_id ) ) { diff --git a/templates/name-your-price-single-product.php b/templates/name-your-price-single-product.php index 540ccf3..36cec83 100644 --- a/templates/name-your-price-single-product.php +++ b/templates/name-your-price-single-product.php @@ -18,7 +18,7 @@ $max_value = apply_filters( 'yh_nyp_max_value_allowed', get_post_meta( $product_id, '_yh_max_value', true ) ?: 0, $product_id ); ?> - /> + />

\ No newline at end of file diff --git a/yh-name-your-price.php b/yh-name-your-price.php index 8ef5ff2..83eb6ac 100644 --- a/yh-name-your-price.php +++ b/yh-name-your-price.php @@ -91,7 +91,11 @@ public static function is_nyp_product( $product_id = null ) { * @return int The Product ID of the parent or simple product. */ public static function get_product_id( $product ) { - $product_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); + + if ( ! $product ) { + return $product; + } + $product_id = $product->get_parent_id() ? $product->get_parent_id() :$product->get_id(); return $product_id; }