特定类别的产品不影响免费送货
Products from a particular category does not affect the free shipping
我需要从特定产品类别中过滤产品,这些产品(数量)不影响免费送货的最低数量。
所以我想我增加了特殊产品数量的最小数量。
示例:
免费送货是从 40 欧元。
客户购买 35 欧元的产品和 6 欧元的空间产品。总计 = 41 欧元
通常他可以免费送货。但不是他卡里的特产。
这可能吗?
add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 );
function cardNo_freeShipping($rates, $package){
global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items
foreach($items as $item => $values) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->slug == "The-Special-Category"){ //If is the special category
if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //If variation exist get price
else $price = get_post_meta($values['product_id'] , '_price', true); //else get singel price
$PriceArray[] = $price * $values['quantity'];
}
}
}
//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;
if($PriceArray) {
$Sum = array_sum($PriceArray);
//The reason it's not exactly 0 lies in the definition of floating point numbers.
$MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
$MinAmount = $MinAmount/100;
//The reason it's not exactly 0 lies in the definition of floating point numbers.
}
if(!$MinAmount){ //No Special products in cart
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}
if($MinAmount==0){ //Only special products in cart
// Only modify rates if flat_rate is present
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}else{ //Normal and special products in cart
if($free_shipping_settings['min_amount'] >= $MinAmount){
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}else{
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
else if( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}
}
return $rates;
}
也许你有更好的主意?谢谢 ;)
也许有人又需要它...
这里是正确的代码:
add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 );
function cardNo_freeShipping($rates, $package){
global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items
foreach($items as $item => $values) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->slug == "xartotainies"){ //If is the special category
if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //If variation exist get price
else $price = get_post_meta($values['product_id'] , '_price', true); //else get singel price
$PriceArray[] = $price * $values['quantity'];
}
}
}
//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;
if($PriceArray) {
$Sum = array_sum($PriceArray);
//The reason it's not exactly 0 lies in the definition of floating point numbers.
$MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
$MinAmount = $MinAmount/100;
//The reason it's not exactly 0 lies in the definition of floating point numbers.
if($MinAmount==0)$MinAmount=0.1; //fix zero problem
}
if(!$MinAmount){ //No Special products in cart
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}else{
if($MinAmount >= $free_shipping_settings['min_amount']){
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}else{
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}
}
return $rates;
}
我需要从特定产品类别中过滤产品,这些产品(数量)不影响免费送货的最低数量。
所以我想我增加了特殊产品数量的最小数量。
示例: 免费送货是从 40 欧元。 客户购买 35 欧元的产品和 6 欧元的空间产品。总计 = 41 欧元
通常他可以免费送货。但不是他卡里的特产。
这可能吗?
add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 );
function cardNo_freeShipping($rates, $package){
global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items
foreach($items as $item => $values) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->slug == "The-Special-Category"){ //If is the special category
if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //If variation exist get price
else $price = get_post_meta($values['product_id'] , '_price', true); //else get singel price
$PriceArray[] = $price * $values['quantity'];
}
}
}
//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;
if($PriceArray) {
$Sum = array_sum($PriceArray);
//The reason it's not exactly 0 lies in the definition of floating point numbers.
$MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
$MinAmount = $MinAmount/100;
//The reason it's not exactly 0 lies in the definition of floating point numbers.
}
if(!$MinAmount){ //No Special products in cart
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}
if($MinAmount==0){ //Only special products in cart
// Only modify rates if flat_rate is present
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}else{ //Normal and special products in cart
if($free_shipping_settings['min_amount'] >= $MinAmount){
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}else{
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
else if( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}
}
return $rates;
}
也许你有更好的主意?谢谢 ;)
也许有人又需要它...
这里是正确的代码:
add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 );
function cardNo_freeShipping($rates, $package){
global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items
foreach($items as $item => $values) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->slug == "xartotainies"){ //If is the special category
if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //If variation exist get price
else $price = get_post_meta($values['product_id'] , '_price', true); //else get singel price
$PriceArray[] = $price * $values['quantity'];
}
}
}
//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;
if($PriceArray) {
$Sum = array_sum($PriceArray);
//The reason it's not exactly 0 lies in the definition of floating point numbers.
$MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
$MinAmount = $MinAmount/100;
//The reason it's not exactly 0 lies in the definition of floating point numbers.
if($MinAmount==0)$MinAmount=0.1; //fix zero problem
}
if(!$MinAmount){ //No Special products in cart
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}else{
if($MinAmount >= $free_shipping_settings['min_amount']){
if ( isset( $rates['free_shipping'] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates['flat_rate'] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
}else{
if ( isset( $rates['flat_rate'] ) ) {
unset( $rates['free_shipping'] );
// To unset all methods except for flat rate, do the following
$flat_rate = $rates['flat_rate'];
$rates = array();
$rates['flat_rate'] = $flat_rate;
}
}
}
return $rates;
}