Skip to content

Commit

Permalink
Dynamic Bid Pricing (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-viget authored Dec 19, 2023
1 parent 8ae1471 commit bb6d1f3
Show file tree
Hide file tree
Showing 9 changed files with 1,107 additions and 19 deletions.
11 changes: 11 additions & 0 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Returns the Auction's Goal value. If `$auction_id` is not provided, the current
`goodbids()->auctions->get_expected_high_bid( int $auction_id )`
Returns the Auction's Expected High Bid value. If `$auction_id` is not provided, the current post ID will be used.

### Bids Functions

`goodbids()->auctions->bids->get_auction_id( int $bid_product_id )`
Returns the Auction ID for the given Bid Product ID.

### ACF Block Functions

`goodbids()->acf->blocks()->get_all_blocks()`
Expand All @@ -86,3 +91,9 @@ Get the location of a block. Return values can be: "directory" (Default) or "jso

`goodbids()->acf->blocks()->get_block_locations()`
Get all directories where blocks can be found.


### WooCommerce Functions

`goodbids()->woocommerce->get_order_auction_id( int $order_id)`
Get the Auction ID for the given Order ID. If `$order_id` is not provided, the current order ID will be used.
108 changes: 108 additions & 0 deletions client-mu-plugins/goodbids/blocks/bid-now/block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Bid Now Block
*
* @since 1.0.0
*
* @package GoodBids
*/

namespace GoodBids\Blocks;

/**
* Class for Bid Now Block
*
* @since 1.0.0
*/
class BidNow {

/**
* @since 1.0.0
* @var array
*/
private array $block = [];

/**
* @since 1.0.0
* @var ?int
*/
private ?int $auction_id = null;

/**
* @since 1.0.0
* @var ?int
*/
private ?int $bid_product_id = null;

/**
* Initialize the block.
*
* @since 1.0.0
*
* @param array $block
*/
public function __construct( array $block ) {
$this->block = $block;
$this->auction_id = goodbids()->auctions->get_auction_id();
$this->bid_product_id = goodbids()->auctions->get_bid_product_id( $this->auction_id );
}

/**
* Determines if the block should be displayed
*
* @since 1.0.0
*
* @return bool
*/
public function display(): bool {
if ( ! is_admin() && goodbids()->auctions->get_post_type() !== get_post_type() ) {
return false;
}

return true;
}

/**
* Returns the text for the Bid Now button
*
* @since 1.0.0
*
* @return string
*/
public function get_button_text(): string {
$button_text = __( 'Bid Now', 'goodbids' );

if ( $this->bid_product_id && ! is_admin() ) {
$bid_product = wc_get_product( $this->bid_product_id );
$button_text = sprintf(
/* translators: %s: Bid Price */
__( 'Bid %s Now', 'goodbids' ),
wc_price( $bid_product->get_regular_price() )
);
}

return $button_text;
}

/**
* Returns the URL for the Bid Now button
*
* @since 1.0.0
*
* @return string
*/
public function get_button_url(): string {
return $this->bid_product_id && ! is_admin() ? add_query_arg( 'add-to-cart', $this->bid_product_id, wc_get_checkout_url() ) : '#';
}

/**
* Returns the classes for the block
*
* @since 1.0.0
*
* @return string
*/
public function get_block_classes(): string {
return 'wp-block-buttons is-vertical is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex';
}
}
18 changes: 7 additions & 11 deletions client-mu-plugins/goodbids/blocks/bid-now/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@
* @package GoodBids
*/

if ( ! is_admin() && goodbids()->auctions->get_post_type() !== get_post_type() ) :
// Bail early if we're not inside an Auction post type.
$bid_now = new GoodBids\Blocks\BidNow( $block );

// Bail early if we're not inside an Auction post type.
if ( ! $bid_now->display() ) :
return;
endif;

$auction_id = goodbids()->auctions->get_auction_id();
$bid_product_id = goodbids()->auctions->get_bid_product_id( $auction_id );

$classes = 'wp-block-buttons is-vertical is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex';
$button_url = $bid_product_id && ! is_admin() ? add_query_arg( 'add-to-cart', $bid_product_id, wc_get_checkout_url() ) : '#';
?>
<div <?php block_attr( $block, $classes ); ?>>
<div <?php block_attr( $block, $bid_now->get_block_classes() ); ?>>
<div class="wp-block-button has-custom-width wp-block-button__width-100">
<a href="<?php echo esc_url( $button_url ); ?>" class="wp-block-button__link wp-element-button">
<?php esc_html_e( 'Bid Now', 'goodbids' ); ?>
<a href="<?php echo esc_url( $bid_now->get_button_url() ); ?>" class="wp-block-button__link wp-element-button">
<?php echo wp_kses_post( $bid_now->get_button_text() ); ?>
</a>
</div>
</div>
1 change: 1 addition & 0 deletions client-mu-plugins/goodbids/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ext-mbstring": "*",
"composer/installers": "^1 || ^2",
"cweagans/composer-patches": "^1.7",
"illuminate/collections": "^10.38",
"oomphinc/composer-installers-extender": "^2.0",
"vlucas/phpdotenv": "^5.5",
"wpackagist-plugin/accessibility-checker": "^1.6",
Expand Down
Loading

0 comments on commit bb6d1f3

Please sign in to comment.