Skip to content

Commit

Permalink
Final commit before launch. Hopefully.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlimaza committed Dec 1, 2021
1 parent 7faae8a commit 835a637
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
assets/.DS_Store
assets/.DS_Store
assets/.DS_Store
79 changes: 51 additions & 28 deletions classes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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.
}
}

}

Expand Down
28 changes: 6 additions & 22 deletions classes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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.
Expand Down Expand Up @@ -103,18 +85,18 @@ 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 ) ) {

$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'] );

}

}
Expand Down Expand Up @@ -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 ) ) {
Expand Down
2 changes: 1 addition & 1 deletion templates/name-your-price-single-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
?>
<label for="yh_nyp_suggest_price_single"><?php echo esc_html( $label_text ); ?></label>
<input type="number" id="yh_nyp_suggest_price_single" class="yh_nyp_sugg_price short" name="yh_nyp_amount" <?php if ($min_value != 0){ echo 'min="' . $min_value . '"'; } if ($max_value != 0){ echo 'max="' . $max_value . '"'; }?> />
<input type="number" id="yh_nyp_suggest_price_single" class="yh_nyp_sugg_price short" name="yh_nyp_amount" <?php if ($min_value != 0){ echo 'min="' . $min_value . '"'; } if ($max_value != 0){ echo 'max="' . $max_value . '"'; }?> />
<br/><br/>
<?php do_action( 'yh_nyp_after_single_product_form' ); ?>
</div>
6 changes: 5 additions & 1 deletion yh-name-your-price.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 835a637

Please sign in to comment.