有些商品用郵局寄送比用宅配便宜,所以較大的商品希望可以限制客人只能用郵局郵寄。修改後的畫面如下:
其他無大面積商品時:
設定方式,目前是「純手工」,在商品的編輯畫面裡增加metadata:
shipping_post_only = 1
純手工去加入欄位太不人性化,增加 3個副程式,就可以在 WooCommerce 裡增加新的Tab.
WordPress教學 – 增加自己的Tab在WooCommerce商品裡
https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/
我的 functions.php
function dynamically_show_shipping_rates( $rates ) { $free = array(); $is_shipping_post_only = 0; foreach (WC()->cart->get_cart() as $item_key => $item) { if ($item['data']->needs_shipping()) { $val = get_post_meta($item['product_id'], 'shipping_post_only', true); if ($val==1) { $is_shipping_post_only=1; break; } } } if ($is_shipping_post_only==1) { unset($rates['flat_rate:5']); unset($rates['flat_rate:3']); } return $rates; } add_filter( 'woocommerce_package_rates', 'dynamically_show_shipping_rates', 100 );
有沒有方法可以讓每一訂單金額大於指定數目,只能選用指定付款方式,如over$500 只能銀行轉帳,under 500 ,可以paypal/銀行轉帳.
解法:
add_filter('woocommerce_available_payment_gateways', 'woocs_filter_gateways', 1); function woocs_filter_gateways($gateway_list) { if (WC()->cart->subtotal > 1000) { unset($gateway_list['cod']); } return $gateway_list; }
動態增加貨運方式的方法:
/** * Developers can add additional flat rates based on this one via this action since @version 2.4. * * Previously there were (overly complex) options to add additional rates however this was not user. * friendly and goes against what Flat Rate Shipping was originally intended for. * * This example shows how you can add an extra rate based on this flat rate via custom function: * * add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 ); * * function add_another_custom_flat_rate( $method, $rate ) { * $new_rate = $rate; * $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID. * $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'. * $new_rate['cost'] += 2; // Add $2 to the cost. * * // Add it to WC. * $method->add_rate( $new_rate ); * }. */
作法二號:
/** * Hide paid shipping rates when free shipping is available. Also remove the * 'Free Shipping with Signature' option when the order total < 500. * * @param array $rates Array of rates found for the package. * @return array */ function dynamically_show_shipping_rates( $rates ) { $free = array(); $cart_subtotal = WC()->cart->subtotal; // Only display the free option and pseudo-free option when free shipping is eligible foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id || $rate_id === 'flat_rate:7' ) { $free[ $rate_id ] = $rate; } } // Always remove the pseudo-free rate from $rates; it will always be in $free array if // if free shipping is enabled, and that will always be returned below when appropriate unset($rates['flat_rate:7']); return $cart_subtotal >= 500 ? $free : $rates; } add_filter( 'woocommerce_package_rates', 'dynamically_show_shipping_rates', 100 );
官方文章:
https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/
相關文章:
Create A WooCommerce Custom Shipping Method Plugin
https://www.cloudways.com/blog/create-woocommerce-custom-shipping-method-plugin/