I'm trying to add some HTML code via echo inside this PHP code:
if( $quantity == 0 )
return __("ZERO ITEMS");
elseif( $quantity > 0 && $quantity <= 5 )
return $quantity;
elseif( $quantity > 5 )
return __("OVER 5 ITEMS");
When the $quantity is 3, for example, I want to return the quantity and also have the word "ITEMS" echo'd after it. So "3 ITEMS" instead of just "3".
If there are 3 items, your control will go to else if.
You are returning only $quantity that is number.
You need to concatenate the string ITEMS after $quantity
...
elseif( $quantity > 0 && $quantity <= 5 )
return $quantity . ' ITEMS';
...
You can do this:
if ( $quantity == 0 )
return __("ZERO ITEMS");
else if( $quantity > 0 && $quantity <= 5 ) {
$itemText = ($quantity == 1) ? 'ITEM' : 'ITEMS';
echo $quantity . ' ' . $itemText;
return $quantity . ' ' . $itemText;
} else if( $quantity > 5 )
return __("OVER 5 ITEMS");
You can have 1, so it's interesting to show ITEM or ITEMS
You can do this like below.
$post_str = 'ITEMS';
if( $quantity == 0 )
return __("ZERO ".$post_str);
elseif( $quantity > 0 && $quantity <= 5 )
return $quantity.' '.$post_str;
elseif( $quantity > 5 )
return __("OVER 5 ".$post_str);
Related
I have written the following script to automatically apply a $350 credit to the shipping rates if the shipping rate is over $350. When removing this script from my functions.php there are no issues at Checkout, the shipping methods will populate. When leaving this script in functions.php, the Checkout page requires multiple refreshes to populate and does not end the spinning loading loop.
Is there something I am missing here?
add_filter('woocommerce_package_rates', 'custom_shipping_rate_label_based_on_cost', 100, 2);
function custom_shipping_rate_label_based_on_cost( $rates, $package ){
// Declare Variables
$subtotal = $package['cart_subtotal'];
$counter = 0;
$first = 0;
$category = 0;
$discount = 350;
// Set Category True if Walk In Tub
foreach ($package['contents'] as $product) {
$product_cats = wp_get_post_terms( $product['product_id'], 'product_cat', array('fields' => 'names') );
if( in_array('Walk In Tubs', $product_cats) ) {
$category = 1;
}
}
// Set First Shipping Rate Cost and Counter
foreach ( $rates as $key => $rate ) {
$rate_cost = $rate->cost;
if( $counter == 0 ) {
$firstcost = $rate_cost;
}
if( $counter == 1 ) {
$secondcost = $rate_cost;
}
$counter = $counter + 1;
}
if ($firstcost == 0) {
$firstcost = $secondcost;
}
// Over or Equal to 350
if ( $firstcost >= 350 && $subtotal >= 4500 && $category = 1 && is_checkout()) {
$rates[$key]->cost = $rates[$key]->cost - ( $discount );
wc_print_notice( __( '<div class="shippingdiscount">$350 Discount Applied to Total Shipping Cost</div>', 'woocommerce' ), 'notice' );
}
// Under 350
else if ( $firstcost < 350 && $firstcost != 0 && $subtotal >= 4500 && $category = 1 && is_checkout()) {
foreach ( $rates as $key => $rate ) {
$rates[$key]->cost = $rates[$key]->cost - ( $firstcost );
}
wc_print_notice( __( '<div class="shippingdiscount">Your order is eligible for Free Shipping</div>', 'woocommerce' ), 'notice' );
}
else {
return;
}
return $rates;
}
It seems the issue is stemming from: wc_print_notice
I've tried to multiply shipping cost depending on how many pallets are in order. I think filter is wrong or something. It just doesnt change shipping price.
$calc = ceil($a+$b / 8); $a > is quantity of pallets, $b > is quantity of units what calculates how many pallets.
My code (function.php):
add_filter( 'woocommerce_cart_shipping_total', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $total, $cart )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( 0 < $cart->get_shipping_total() ) {
if ( $cart->display_prices_including_tax() ) {
$a = 0;
$b = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
if (!empty(get_post_meta($cart_item['variation_id'], '_number_field', true))) {
$a += $cart_item['quantity'];
} else {
$atk = strstr(get_post_meta($cart_item['variation_id'], '_alus_al', true), 'tk', true);
$b += ceil($cart_item['quantity'] / $atk);
}
}
$calc = ceil($a+$b / 8); // 8 pallets maximum
$total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $calc );
if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$a = 0;
$b = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
if (!empty(get_post_meta($cart_item['variation_id'], '_number_field', true))) {
$a += $cart_item['quantity'];
} else {
$atk = strstr(get_post_meta($cart_item['variation_id'], '_alus_al', true), 'tk', true);
$b += ceil($cart_item['quantity'] / $atk);
}
}
$calc = ceil($a+$b / 8); // 8 pallets maximum
$total = wc_price( $cart->shipping_total * $calc );
if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return $total;
}
My problem is that it doesnt change shipping cost. So I excpect it doesnt work.
Like user #LoicTheAztec mentioned I should use woocommerce_package_rates to override custom prices for shipping plugin.
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {
$a = 0;
$b = 0;
foreach( WC()->cart->get_cart() as $cart_item ){
if (!empty(get_post_meta($cart_item['variation_id'], '_number_field', true))) {
$a += $cart_item['quantity'];
} else {
$atk = strstr(get_post_meta($cart_item['variation_id'], '_alus_al', true), 'tk', true);
$atb = $atk / 2;
if ($cart_item['quantity'] < $atb) {
$b += 0;
} else {
$b += ceil($cart_item['quantity'] / $atk);
}
}
}
$tt = $a+$b;
$calc = ceil($tt / 8);
foreach( $rates as $rate_key => $rate ){
// Excluding free shipping methods
if( $rate->method_id != 'free_shipping'){
// Set rate cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * $calc;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 )
$taxes[$key] = $new_cost * $tax_rate;
}
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
I need to change shipping price programmatically:
<?php
$percentage = 50;
$current_shipping_cost = WC()->cart->get_cart_shipping_total();
echo $current_shipping_cost * $percentage / 100;
?>
Unfortunately it's not working and I always get 0 (zero).
How to change the displayed shipping total with a total based on a calculated discount percentage?
The following will displayed shipping total based on a percentage. There is 2 ways:
1) First way with a custom function.
In the function.php file of your active child theme (or active theme):
function wc_display_cart_shipping_total( $percentage = 100 )
{
$cart = WC()->cart;
$total = __( 'Free!', 'woocommerce' );
if ( 0 < $cart->get_shipping_total() ) {
if ( $cart->display_prices_including_tax() ) {
$total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$total = wc_price( $cart->shipping_total * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return $totals;
}
Usage:
<?php echo wc_display_cart_shipping_total(50); ?>
2) Second way with a filter hook.
In the function.php file of your active child theme (or active theme):
add_filter( 'woocommerce_cart_shipping_total', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $total, $cart )
{
// HERE set the percentage
$percentage = 50;
if ( 0 < $cart->get_shipping_total() ) {
if ( $cart->display_prices_including_tax() ) {
$total = wc_price( ( $cart->shipping_total + $cart->shipping_tax_total ) * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && ! wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$total = wc_price( $cart->shipping_total * $percentage / 100 );
if ( $cart->shipping_tax_total > 0 && wc_prices_include_tax() ) {
$total .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return $totals;
}
Usage:
<?php echo WC()->cart->get_cart_shipping_total(); ?>
I trying to develop a function allowing to calculate a specific price in function the quantity discount
Example : 10% = $discount_customer[] in function the database
$qty is add by an user
if $qty < 5 then $$discount_customer[] = 0 : price does'nt change
if $qty > 5 et qty < 10 then $$discount_customer[] = 10% : price with -10%
...
if $qty > 10 then $$discount_customer[] = 15% : price with -15%
$id : id of the product
$qty : order quantity inserted by a customer
$products_price : price of the product
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
// quantity discount
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
// Customer discount
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
I suppose to create a foreach but how ?
Thie element doesn't take the between condition.
foreach ($discount_quantity as $k => $quantity) {
print_r('<pre>');
var_dump($quantity );
print_r('</pre>');
foreach ($discount_customer as $c => $discount) {
if ($qty > $quantity) {
$news_price = $products_price -(($products_price * $discount) /100);
}
print_r('<pre>');
print_r($news_price);
print_r('</pre>');
}
return $newprice
}
thank you
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
return $this->getPrctDiscount($qty, $products_price);
}
public function getPrctDiscount($qty, $products_price)
{
$discount = 0;
if ($qty > 5 && $qty < 10) {
$discount = 10;
}elseif ($qty > 10) {
$discount = 15;
}
$newPrice = $discount > 0
? $products_price - ($products_price * ($discount/100))
: $newPrice;
return $newPrice;
}
I have a function to calculate price as below. Im just interested to learn any other way to shorten my code.
PHP
function calculate_price( $quantity, $target_quantity, $price, $rate )
{
$total = 0;
if( $quantity > $target_quantity )
{
$total = $target_quantity * $price;
$quantity -= $target_quantity;
$tmp_price = $price;
do {
$tmp_price = $tmp_price * $rate;
$total += $tmp_price;
$quantity--;
} while( $quantity > 0 );
}
else
{
$total = $quantity*$price;
}
return $total;
}
echo calculate_price( 8, 5, 3, 0.5 );
You could combine the decrement in the while statement.
while( --$quantity > 0 );