how to add shipping cost with product actual price? - wordpress

I am using woocommerce plugin v2.2.8 for my eCommerce site. I am using weight based shipping method. Is there is any possibility to add shipping cost with products actual price which is displaying in product page?
For Eg.. Product1 = Rs 800/- & shipping cost of this product is Rs 50/-
Product1 price in shop page should be displayed as Rs 850/- (Actual price + shipping cost) Note: shipping cost calculated from weight based shipping method. Is this possible?
Any idea regarding this???

Shipping always needs a destination address, but when you are showing a product in shop page then no address is available there. But, as you can have a flat rate shipping setting for every zone then you can retrieve that value by your own and add that price to product. If you need product based shipping price, define shipping classes, assign desired shipping class to product and configure prices for each shipping class.
Now while showing price for product in front end you can use following woocommerce hook and put your logic to modify the price.
function return_custom_price($price, $product) {
//Apply your logic and modify the price
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

Here is your cart Data
global $woocommerce;<br>
$data = $woocommerce->cart->get_cart();<br><br>
<br>
$product_id = array();<br>
$product_weigth = array();<br>
$weight_total = 0;<br>
<br>
Break your cart Data
foreach($data as $value)<br>
{<br>
$product_id[] = $value['product_id'];<br>
}<br>
<br>
for($i=0;$i < count($product_id);$i++)<br>
{<br>
$product_weigth[] = get_post_meta($product_id[$i],'_weight',true);<br>
$weight_total += get_post_meta($product_id[$i],'_weight',true);<br>
}<br><br>
<br>
Print Your Cart Products ID
print_r($product_id);<br><br>
Print Your Cart Products Weight
print_r($product_weigth);<br><br>
Total Weight
echo $weight_total;<br><br>
Add Fee
$woocommerce->cart->add_fee('Shipping Charges(Weight)'.$weight_total, $your_fee, true, 'standard' );<br><br>
Now ou can set your fee by weight.. if - else conditions

Related

Woocommerce - Exclude Specific Country Tax from Product at checkout

I would like to exclude all tax rates from a specific country inside my checkout containing a specific product (which is a custom fee (tip)) - I tried to use another Tax rates like 'Zero Tax Rates' in woocommerce to set Canada to 0%, But I have an issue with all the other countries using this method as only Standard Rate seems to calculate the rates based on the total (including the tip).
I found this code to exclude a all tax rates based on a zip code :
/**
* #snippet Remove Tax if Field Value Exists - WooCommerce Checkout
* #how-to Get CustomizeWoo.com FREE
* #author Rodolfo Melogli, BusinessBloomer.com
* #testedwith WooCommerce 4.5
* #donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );
function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
WC()->customer->set_is_vat_exempt( false );
parse_str( $post_data, $output );
if ( $output['billing_postcode'] === '32444' ) WC()->customer->set_is_vat_exempt( true );
}
I'm able to edit it to choose 'Billing_country' === CA' instead but I would also need this function to target a specific product so that the rest of the website would not use this function. I need Canadians customers to get no taxes rates for this specific products, but not for the others countries.
Thanks for your help.

Changing shipping cost in Woocommerce

I am creating a plugin that connects to an 3rd API to request the shipping cost based on the address of the person and the dimensions of the product.
Everything is fine but now I need the values ​​returned by the API to be added to the prices of the shipping methods and I tried with this code but it still doesn't work.
add_filter('woocommerce_package_rates','test_overwrite_fedex',100,2);
function test_overwrite_($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = $phpVar1; <-//variable that receives the value of the shipment
}
return $rates;
But I don't find any way to change the shipping cost with functions..
Is it possible ? Thanks

How add product to cart woocomerce by hook?

I'm trying to implement the following solution:
add_action('woocommerce_calculate_totals' , 'buy3');
function buy3(WC_Cart $cart){
global $woocommerce;
$woocommerce->cart->add_to_cart(6373,1,7444);
}
but the problem after execute the code all quantity the product add To cart not one quantity
Can you Confirm that Sold individually is un-checked? Because WooCommere add_to_cart() Force the quantity to 1 if sold individually and it also check for existing item in cart
Also Parameters for add_to_cart() function are as below :
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id,$quantity,$variation_id, $variation,$cart_item_data);
According to the code you provided, you have supplied quantity = 1;

woocommerce update sale price

I currently have a form where the admin can bulk update sale prices of products within one screen.
When the form is submitted i simply use update_post_meta like this:
update_post_meta($id,'_sale_price', 'new sale price here');
This updates the _sale_price meta key. When i go into the admin to check this, the new sale price has been inserted. When i view the product on the front end, the item is not marked as on sale. I have to go back in and re-save the product.
My question is, does woocommerce add some other meta_key to mark the product as on sale? I have had a dig round in the database for all the custom fields inserted, but can only see _sale_price.
Any help would be greatly appreciated
Having a look at the Product abstract class in WooCommerce the following php code gets whether or not the product is on sale:
return ( $this->sale_price != $this->regular_price && $this->sale_price == $this->price );
Which seems to indicate that the _price has to be the same as the sale_price in order for it to be classed as on sale. So just
update_post_meta($id, '_price', 'new sale price here too');
as well as the code in your question and it should work.

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

Resources