2014-11-19 5 views
0

모든 배송지에 기본 배송 클래스를 추가 할 수 있습니까? 배송 클래스가 하나 뿐이므로 클래스를 생성 할 때 모든 제품에 해당 클래스를 추가하는 대신 자동으로 배송 클래스를 추가해야하는 자동화 된 방법이 필요합니다. 당신은 기능을 추가 할 수Woocommerce가 모든 제품에 기본 배송 클래스 추가

add_action('woocommerce_before_calculate_totals' , 'add_shipping_terms_before_totals' , 10, 1); 

function add_shipping_terms_before_totals(WC_Cart $wc_cart){ 

    if(count($wc_cart->get_cart()) == 0){ 
     return; 
    } 

    // Here you need to edit the slug_to_edit with your custom slug 
    $shipping_terms = get_term_by('slug', 'slug_to_edit', 'product_shipping_class'); 

    // If can't find the terms, return 
    if(empty($shipping_terms)){ 
     return; 
    } 


    foreach($wc_cart->get_cart() as $item){ 
     $product = new WC_Product($item['product_id']); 

     $product_shipping_class = $product->get_shipping_class(); 

     if(!empty($product_shipping_class)){ 
      continue; 
     } 

     wp_set_post_terms($product->id, array($shipping_terms->term_id), 'product_shipping_class'); 
    } 

} 

을 (당신의 functions.php에 추가) 또는

먼저 동적 카트 합계 전에 용어를 추가

답변

1

많은 솔루션

이 두 솔루션입니다 제품을 추가 할 때 수동으로 트리거 할 수 있습니다.

그런 다음 방아쇠를 당겨 야합니다.

http://yoururl?update_products 

admin.

+0

내 슬러그는 '배송'입니다 배송 클래스 슬러그와 slug_to_edit을 편집하는 것을 잊지 마십시오 솔루션 # 1에 대해'slug_to_edit'을 편집했지만 아무 일도 없었습니다. 솔루션 # 2에서 내 사이트는 제공된 코드로로드되지 않으며 단지 비어 있습니다. 두 솔루션 모두에서 해당 URL을 트리거해야합니까? 내가했을 때'http : // myurl /? update_products'로 리디렉션되었습니다. – vytfla

관련 문제