在产品页面Woocommerce上显示折扣

时间:2018-07-31 01:24:50

标签: php code-snippets

我在主题的function.php中有此代码,以在产品页面Woocommerce上显示折扣-它不再起作用.....!

此代码段在WooCommerce 3.0+版本上不再起作用。

我该如何解决.......?

这是代码:

// for Bulk discounts
add_filter( 'woocommerce_variable_price_html', 'webexpert_apply_to_productpage', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'webexpert_apply_to_productpage', 10, 2 );

function webexpert_apply_to_productpage( $price, $product ) {
$original_product=$product;
$q = array( 0.0 );
$d = array( 0.0 );
$quantity=1;

$configurer = get_page_by_title( 'wc_bulk_discount_configurer', OBJECT, 'product' );
if ( $configurer && $configurer->ID && $configurer->post_status == 'private' ) {
    $product_id = $configurer->ID;
}
if ($product instanceof WC_Product_Variation) {
    $product_id = $product->get_parent_id();
}else {
    $product->get_id();
}

/* Find the appropriate discount coefficient by looping through up to the five discount settings */
for ( $i = 1; $i <= 5; $i++ ) {
    array_push( $q, get_post_meta( $product_id, "_bulkdiscount_quantity_$i", true ) );
    if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) {
        array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_flat_$i", true ) : 0.0 );
    } else if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'fixed' ) {
        array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_fixed_$i", true ) : 0.0 );
    } else {
        array_push( $d, get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) ? get_post_meta( $product_id, "_bulkdiscount_discount_$i", true ) : 0.0 );
    }
    if ( $quantity >= $q[$i] && $q[$i] > $q[0] ) {
        $q[0] = $q[$i];
        $d[0] = $d[$i];
    }
}
// for percentage discount convert the resulting discount from % to the multiplying coefficient
if ( get_option( 'woocommerce_t4m_discount_type', '' ) == 'fixed' ) {
    $coeff= max( 0, $d[0] * $quantity );
}
$coeff=( get_option( 'woocommerce_t4m_discount_type', '' ) == 'flat' ) ? max( 0, $d[0] ) : min( 1.0, max( 0, ( 100.0 - round( $d[0], 2 ) ) / 100.0 ) );

if ($product instanceof WC_Product_Variation) {
    $min_var_reg_price = $original_product->get_variation_sale_price( 'min', true );
}else {
    $min_var_reg_price=$product->get_price();
}

$min_var_sale_price = $min_var_reg_price * $coeff ;

if ( $min_var_sale_price!=$min_var_reg_price ) {
$price = sprintf( __( '<del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price( $min_var_sale_price ) );
} else {
$price = sprintf( __( '%1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) );
}

return $price;
}

0 个答案:

没有答案