WooCommerce Package Rates (Looping 2 Times) Creates Multiplied Rate Costs - woocommerce

Using the filter, woocommerce_package_rates, it works at first, but upon loading the checkout page, it seems to load this filter multiple times.
Example.
If the cost of shipping is, $20. I need to multiply this by 2, which should be $40.
But it will loop again, and it will become $80.
When I first load the checkout, it shows the right price, but after 2 seconds, it adjusts it again to $80.
Not sure what to do here, or how to handle this.
add_filter( 'woocommerce_package_rates', 'woocommerce_shipping_rates', 10, 2);
function woocommerce_shipping_rates($rates, $package) {
$multiplier = 2;
foreach ( $rates as $rate_key => $rate ){
if( 'canada_post' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
//echo "CALC:". $rate->cost ." * ". $multiplier ;
$rates[$rate_key]->cost = $rate->cost * $multiplier ;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $multiplier ;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}

Related

WooCommerce free delivery over certain amount and only for certain products

I need some kind of a WordPress plugin that allows me to have 2 different product classes on WooCommerce, and for each one, I put a limit and when the customers cart went over the limit for that certain product class, the shipping cost gets free.
For example, I have books and pens in my shop
when a customer adds pens to his/her cart, the limit for free shipping will be 20$
and if he/she adds books, the limit gets to 50$
and in case they add both items the limit will be the higher one (50$)
add_filter('woocommerce_package_rates', 'shipping_class_based_cost', 15, 2);
function shipping_class_based_cost($rates, $package) {
if (is_admin() && !defined('DOING_AJAX'))
return $rates;
$shipping_class_books = 'books';
$shipping_class_pen = 'pen';
$my_shipping_classes = array($shipping_class_books, $shipping_class_pen);
$has_shipping_class_books = $has_shipping_class_pen = false;
$shipping_class_found = false;
// loop through cart items and checking for the specific product class
foreach ($package['contents'] as $item) {
$item_shipping_class = $item['data']->get_shipping_class();
if (in_array($item_shipping_class, $my_shipping_classes)) {
if ($shipping_class_books == $item_shipping_class)
$has_shipping_class_books = true;
if ($shipping_class_pen == $item_shipping_class)
$has_shipping_class_pen = true;
$shipping_class_found = true;
}
}
if ($shipping_class_found) {
// loop through shipping rates
foreach ($rates as $rate_key => $rate) {
if ($rate->method_id == 'flat_rate') {
$cart_total = $package['cart_subtotal'];
if ($has_shipping_class_pen && $cart_total > 20)
$rates[$rate_key]->cost = 0.00;
if ($has_shipping_class_books && $cart_total > 50)
$rates[$rate_key]->cost = 0.00;
$has_taxes = false;
$taxes = [];
// loop through the shipping taxes array
foreach ($rates[$rate_key]->taxes as $key => $tax) {
if ($rates[$rate_key]->taxes[$key] > 0) {
$taxes[$key] = 0;
$has_taxes = true;
}
}
if ($has_taxes)
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}

How to check type of order notes in woocommerce

How to check the type of order notes in woocommerce?
I want to check the note is private_note or customer_note.
Here is my code:
//add_action('wp_insert_comment','hook_insert_func',100,2);
add_filter( 'comments_clauses','hook_insert_func',100,2);
function hook_insert_func($id, $comment){
if($comment->comment_type!='order_note')
return;
if($comment->comment_type == 'order_note'){
$mobile = PWooSMS()->buyerMobile($comment->comment_post_ID);
$data = array();
$data['type'] = 1;
$data['mobile'] = "$mobile";
$data['message'] = $comment->comment_content;
$response = PWooSMS()->SendSMS( $data );
}
}

Get total price after ajax update in woocommerce

I am trying to take the price in the filter woocommerce_update_order_review_fragments, but failing to understand where it resides
add_filter('woocommerce_update_order_review_fragments', 'price_bottom_checkout');
function price_bottom_checkout($arr) {
$price = ? // how to get it over here?
$price_txt = '<span class="total-pay">'.$price.'</span>';
$arr['.total-pay'] = $price;
return $arr;
}
Managed to find out how. Needed to see the session cart which is updated on every cart refresh via ajax.
add_filter('woocommerce_update_order_review_fragments', 'price_bottom_checkout');
function price_bottom_checkout($arr) {
global $woocommerce;
$the_total = '';
foreach ($woocommerce->cart->cart_contents as $cart_item) {
$the_total = $cart_item['line_total'];
break;
}
$price_txt = '<span class="total-pay">'.wc_price($the_total).'</span>';
$arr['.total-pay'] = $price_txt;
return $arr;
}

How to Overriding the existing quantity of products already in cart?

I am using the latest version of wordpress and Woocommerce.
My Functionality is " Overriding the existing Quantity of Products already in cart". I already try this idea but its failure. See the Code and its errors.
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);
function update_product_in_cart($p, $q) {
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
$wCart = $woocommerce->cart->get_cart();
// If cart already exists, and product exists, than remove product, and add the new product to it.
if ($wCart)
{
$cart_item_keys = array_keys($wCart);
foreach($cart_item_keys as $key)
{
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
if ($productItemId == $currentProductId)
{
// If you want to empty the entire cart...
// $woocommerce->cart->empty_cart();
// If you want to remove just the product from the cart...
$woocommerce->cart->set_quantity($key, 0);
}
}
}
}
// This adds the product to the cart...
return $q;
}
Its Error is:
Catchable fatal error: Object of class WC_Product_Grouped could not be converted to string in plugins\woocommerce\includes\class-wc-form-handler.php on line 715
This code is the answer, Previously I added on comment boxes....
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);function update_product_in_cart($p, $q) { global $woocommerce; $cartItem = $woocommerce->cart->cart_contents; $currentProductId = $q->id; $wCart = $woocommerce->cart->get_cart(); if ($wCart){ $cart_item_keys = array_keys($wCart); foreach($cart_item_keys as $key) {foreach($cartItem as $item) {$productItemId = $item['product_id']; if ($productItemId == $currentProductId) {$woocommerce->cart->set_quantity($key, 0); }}}}return $q; }

How to apply <span> tag to My Cart top link in magento?

I have changed texts My cart to My Shopping Bag in top links through below code.
public function addCartLink()
{
$parentBlock = $this->getParentBlock();
if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
$count = $this->getSummaryQty() ? $this->getSummaryQty()
: $this->helper('checkout/cart')->getSummaryCount();
if ($count == 1) {
$text = $this->__('My Shopping Bag (%s)', $count);
} elseif ($count > 0) {
$text = $this->__('My Shopping Bag (%s)', $count);
} else {
$text = $this->__('My Shopping Bag');
}
$parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
}
return $this;
}
Now, I want to apply tag to shopping cart quantity.
So, It should look like My Shopping Bag 0. 0 (quantity) should be in red color. So what should I do?
your code is right but in else part its want tricky way.
you can show like this
$text = $this->__('My Shopping Bag (0)');
may its help you
Thanks
Anand
<span> can be added directly.
public function addCartLink()
{
$parentBlock = $this->getParentBlock();
if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
$count = $this->getSummaryQty() ? $this->getSummaryQty()
: $this->helper('checkout/cart')->getSummaryCount();
if ($count == 1) {
$text = $this->__('My Shopping Bag <span>(%s)</span>', $count);
} elseif ($count > 0) {
$text = $this->__('My Shopping Bag <span>(%s)</span>', $count);
} else {
$text = $this->__('My Shopping Bag');
}
$parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
}
return $this;
}

Resources