Last active
February 2, 2018 17:12
-
-
Save grola/358e45b448f1dbcd9d02846b2ba73848 to your computer and use it in GitHub Desktop.
Add information about the price in installments after price on product page
This file contains 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 | |
new studiowp_single_product_price_installments( $parts_count = 4, $factor = 1.2 ); | |
class studiowp_single_product_price_installments { | |
private $add_installment_price = false; | |
private $parts_count; | |
private $factor; | |
public function __construct( $parts_count = 12, $factor = 1.1 ) { | |
$this->parts_count = $parts_count; | |
$this->factor = $factor; | |
add_filter( 'woocommerce_get_price_html', array( $this, 'woocommerce_get_price_html' ), 10, 2 ); | |
add_action( 'woocommerce_before_single_product_summary', array( $this, 'woocommerce_before_single_product_summary' ) ); | |
add_action( 'woocommerce_after_single_product_summary', array( $this, 'woocommerce_after_single_product_summary' ) ); | |
} | |
public function woocommerce_before_single_product_summary() { | |
$this->add_installment_price = true; | |
} | |
public function woocommerce_after_single_product_summary() { | |
$this->add_installment_price = false; | |
} | |
public function woocommerce_get_price_html( $price, $product ) { | |
if ( $this->add_installment_price ) { | |
$single_part = $product->get_price() * $this->factor / $this->parts_count; | |
$single_part = ceil( $single_part ); | |
$price .= sprintf(' lub <strong>%s</strong> w %d ratach', wc_price( $single_part ), $this->parts_count ); | |
} | |
return $price; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment