问题
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???
回答1:
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);
回答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
来源:https://stackoverflow.com/questions/27245789/how-to-add-shipping-cost-with-product-actual-price