2017-09-21 1 views

답변

1

업데이트(댓글에 관련된) : 이제

제품, 카트 항목에 이름 + 속성 값 중독이 사용자 정의 기능을 사용하는 것도 가능합니다 것처럼 브랜드 이름 (들)을 추가하는 방법 woocommerce_get_item_data 필터 걸이.

코드는 조금 다른 (그러나 브랜드의 데이터를 얻을 동일)입니다 :

add_filter('woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2); 
function customizing_cart_item_data($cart_data, $cart_item) { 

    $custom_items = array(); 

    if(! empty($cart_data)) $custom_items = $cart_data; 

    $product = $cart_item['data']; // The product 
    $product_id = $cart_item['product_id']; // The product id 

    // Loop through the product brand names (the taxonomy for WC Brands is 'product_brand') 
    foreach(wp_get_post_terms($product_id, 'product_brand') as $wp_term) 
     $brand_names[] = $wp_term->name; // Set the brand names in an array 

    $brand_names_str = implode(', ', $brand_names); // Set the brand names in a coma separated string array 

    if(count($brand_names) > 0) 
     $custom_items[] = array(
      'name'  => __('Brand', 'woocommerce'), 
      'value'  => $brand_names_str, 
      'display' => $brand_names_str, 
     ); 

    return $custom_items; 
} 

코드가 어떤 또한 활성 자식 테마 (또는 테마)의 function.php 파일에 간다거나 플러그인 파일.

모든 코드는 Woocommerce 3 이상에서 테스트되었으며 작동합니다. 여기


woocommerce_cart_item_name 필터 훅이 걸어 맞춤 기능을 사용하여 카트 항목에서 제품 이름 상품명 (들)을 추가하는 방식이다.

1 개 제품에 대해 여러 브랜드를 설정할 수 있으므로 문자가 인 경우 ( 이상인 경우) 문자를 쉼표로 구분하여 표시합니다. 어떤 플러그인 파일도

add_filter('woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3); 
function customizing_cart_item_name($product_name, $cart_item, $cart_item_key) { 
    $product = $cart_item['data']; // The product 
    $product_id = $cart_item['product_id']; // The product id 

    // Loop through the product brand names (the taxonomy for WC Brands is 'product_brand') 
    foreach(wp_get_post_terms($product_id, 'product_brand') as $wp_term) 
     $brand_names[] = $wp_term->name; // Set the brand names in an array 

    $brand_names_str = implode(', ', $brand_names); // Set the brand names in a coma separated string array 

    $brand = 'Brand: ' . $brand_names_str; 
    $product_permalink = $product->get_permalink($cart_item); 

    if (is_cart() && count($brand_names) > 0) 
     return sprintf('<a href="%s">%s | %s</a>', esc_url($product_permalink), $product->get_name(), $brand); 
    elseif (count($brand_names) > 0) 
     return $product_name . ' | ' . $brand; 
    else return $product_name; 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 : 여기

는 코드입니다.

모든 코드는 Woocommerce 3 이상에서 테스트되었으며 작동합니다.

+0

감사합니다. @LoicTheAztec, 훌륭합니다. 브랜드를 제목 대신 변형으로 보이게 할 수 있습니까? http://prntscr.com/go7wep –

+0

@JamesDeadman이 코드는 그 코드와 다릅니다 ... 내 대답을 업데이트해야합니다. 테스트하고 변경하는 데 몇 분이 필요합니다. – LoicTheAztec

+0

정말 대단합니다. 감사합니다. –

관련 문제