Last active
October 25, 2019 22:39
-
-
Save EricBusch/b8e3309550e54a4130b108595980989c to your computer and use it in GitHub Desktop.
This code updates a WooCommerce product's price (including sale price) with data from its Comparison Set when the Comparison Set is created or its cached updated. [datafeedr][dfrcs]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This code changes a WooCommerce product's price (including sale price) with data | |
* from its Comparison Set when the Comparison Set is created or its cache is updated and | |
* the Comparison Set has a highest and lowest price. | |
* | |
* This code modifies all of a WooCommerce Product's pricing fields. | |
* | |
* @param Dfrcs $compset | |
*/ | |
function mycode_add_high_low_price_to_woocommerce_product_data( Dfrcs $compset ) { | |
$product_id = isset( $compset->args['post_id'] ) ? $compset->args['post_id'] : 0; | |
$product = wc_get_product( $product_id ); | |
if ( ! $product ) { | |
return; | |
} | |
$main_active_price = $product->get_price(); | |
$compset_highest_price = dfrcs_get_highest_priced_product_field( $compset, 'price' ); | |
$compset_lowest_price = dfrcs_get_lowest_priced_product_field( $compset, 'finalprice' ); | |
/** | |
* If neither the highest price or the lowest price is found, return. | |
*/ | |
if ( ! $compset_highest_price || ! $compset_lowest_price ) { | |
return; | |
} | |
/** | |
* Convert the Comparison Set prices to floats so we can compare them | |
* to the main product's prices. | |
*/ | |
$compset_highest_price = floatval( $compset_highest_price / 100 ); | |
$compset_lowest_price = floatval( $compset_lowest_price / 100 ); | |
/** | |
* If the active price of the main product is less than or equal to the | |
* lowest price found in the Comparison Set, do nothing. No need to update the | |
* price for the main product as its price is already the best (or at least | |
* equal to the best price found in the Comparison Set). | |
* | |
* NOTE: | |
* | |
* The problem with this is, once you modify the main product's price to a lower price | |
* if a future Comparison Set never has a lower price, the main product's price will | |
* only get updated to a correct higher price when the product is updated via a | |
* Product Set update. | |
* | |
* Remove this conditional if you always want the main product's price to be | |
* generated from the Comparison Set's low and high prices. | |
*/ | |
if ( $main_active_price <= $compset_lowest_price ) { | |
return; | |
} | |
/** | |
* If we made it this far, we need to update the prices of the main | |
* WooCommerce product. | |
*/ | |
if ( $compset_lowest_price < $compset_highest_price ) { | |
update_post_meta( $product->get_id(), '_regular_price', (float) $compset_highest_price ); | |
update_post_meta( $product->get_id(), '_sale_price', (float) $compset_lowest_price ); | |
update_post_meta( $product->get_id(), '_price', (float) $compset_lowest_price ); | |
} else { | |
update_post_meta( $product->get_id(), '_regular_price', (float) $compset_highest_price ); | |
update_post_meta( $product->get_id(), '_sale_price', '' ); | |
update_post_meta( $product->get_id(), '_price', (float) $compset_lowest_price ); | |
} | |
} | |
add_action( 'dfrcs_compset_creation_complete', 'mycode_add_high_low_price_to_woocommerce_product_data', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment