WooCommerce - 카트에 추가할 때 선택하지 않은 변형 무시
목표: 선택하지 않은 변형에도 불구하고 제품을 카트에 추가합니다. 즉, 변형 필드의 필수 특성을 제거/비활성화합니다.
이슈: 카트에 추가하기 전에 모든 변형을 선택해야 하는 우커머스의 절대적인 요구 사항.
시도: 다양한 후크를 사용하여 카트에 추가하기 전에 선택되지 않은 변형을 필터링/제거/비활성화합니다.woocommerce_before_calculate_totals
,woocommerce_add_to_cart
,woocommerce_add_cart_item
,woocommerce_add_to_cart_validation
저는 이것이 WooCommerce가 어떻게 작동하는지 그리고 왜 이런 식으로 작동하는지 이해합니다. 그럼에도 불구하고 저는 여전히 해결책이 필요합니다.
모든 변형을 선택하지 않았더라도 제품을 카트에 추가할 수 있도록 WooCommerce의 "모든 변형 선택" 요구 사항을 해결하려면 어떻게 해야 합니까?
1 - "변동" 속성과 "변동이 아닌" 속성을 사용할 수 있습니다.테스트 후 업데이트됨
제품 가격을 처리할 속성:
가변 제품 생성
실제 Woocommerce 제품 속성(실제 분류법 및 용어) 생성(실제 분류법 및 용어는 생성되지 않으므로 "제품 페이지에 생성"된 속성과 함께 사용할 수 없음)
여기에 몇 가지 용어를 사용하여 3개의 속성을 만들었습니다.
귀사의 가변 제품에서 저는 3가지 속성을 모두 선택합니다.그러나 "옵션" 속성(옵션)이 아닌 변동에 대해서만 색상 및 크기를 사용하도록 지정됩니다(색상 및 변동이 가격 변동을 처리하도록 지정됨).
그런 다음 변형을 생성합니다.여기 보시면 색상과 크기 조합에 대한 변형만 있을 뿐 "옵션" 속성에 대한 내용은 아직 없습니다.
또한 변동 속성에 대한 "기본값"을 선택합니다.따라서 프론트엔드에서 HTML 입력 선택 속성에는 사전 선택된 옵션이 있습니다(사용자가 카트에 직접 추가할 수 있음).
이제 프론트 엔드에 사전 선택된 값을 가진 변동 속성이 있습니다.
하지만 우리는 여전히 "선택적" 특성을 놓치고 있습니다.
합니다.
function.php
또는 관련됨(영감 있음, 업데이트됨/새로 고침, 수정됨)(형식 지정에 대해 죄송합니다. 스니펫을 요지로도 사용할 수 있습니다.선택적 속성에 대한 선택 입력을 출력하여 카트와 주문에 저장합니다.기본값을 사용하거나 사용하지 않고 HTML 및 배치를 다른 후크로 편집하도록 조정할 수 있습니다.
/**
* List available attributes on the product page in a drop-down selection
*/
function list_attributes_on_product_page() {
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
//from original script, but here we want to use it for variable products
/*if ($product->is_type( 'variable' )) {
return;
}*/
echo '<div style="padding-bottom:15px;">';
foreach ( $attributes as $attribute ) {
//If product is variable, and attribute is used for variation: woocommerce already handle this input - so it can also be used with attributes of simple products (not variables)
if($product->is_type( 'variable' ) && $attribute['variation']) {
continue;
}
//get taxonomy for the attribute - eg: Size
$taxonomy = get_taxonomy($attribute['name']);
//get terms - eg: small
$options = wc_get_product_terms( $product->get_id(), $attribute['name'], array( 'fields' => 'all' ) );
$label = str_replace('Product ', '', $taxonomy->label);
//display select input
?>
<div style="padding-bottom:8px;">
<label for="attribute[<?php echo $attribute['id']; ?>]"><?php echo $label; ?></label>
<br />
<!-- add required attribute or not, handle default with "selected" attribute depending your needs -->
<select name="attribute[<?php echo $attribute['id']; ?>]" id="attribute[<?php echo $attribute['id']; ?>]">
<option value disabled selected>Choose an option</option>
<?php foreach ( $options as $pa ): ?>
<option value="<?php echo $pa->name; ?>"><?php echo $pa->name; ?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
echo '</div>';
}
add_action('woocommerce_before_add_to_cart_button', 'list_attributes_on_product_page');
/**
* Add selected attributes to cart items
*/
add_filter('woocommerce_add_cart_item_data', 'add_attributes_to_cart_item', 10, 3 );
function add_attributes_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$attributes = $_POST['attribute'] ?? null;
if (empty( $attributes ) ) {
return $cart_item_data;
}
$cart_item_data['attributes'] = serialize($attributes);
return $cart_item_data;
}
/**
* Display attributes in cart
*/
add_filter( 'woocommerce_get_item_data', 'display_attributes_in_cart', 10, 2 );
function display_attributes_in_cart( $item_data, $cart_item ) {
if ( empty( $cart_item['attributes'] ) ) {
return $item_data;
}
foreach (unserialize($cart_item['attributes']) as $attributeID => $value) {
$attribute = wc_get_attribute($attributeID);
$item_data[] = array(
'key' => $attribute->name,
'value' => $value,
'display' => '',
);
}
return $item_data;
}
/**
* Add attribute data to order items
*/
add_action( 'woocommerce_checkout_create_order_line_item', 'add_attributes_to_order_items', 10, 4 );
function add_attributes_to_order_items( $item, $cart_item_key, $values, $order ) {
if ( empty( $values['attributes'] ) ) {
return;
}
foreach (unserialize($values['attributes']) as $attributeID => $value) {
$attribute = wc_get_attribute($attributeID);
$item->add_meta_data( $attribute->name, $value );
}
}
결과:
2 - 플러그인
또한 WooCommerce용 제품 추가 기능 또는 추가 제품 옵션(제품 추가 기능)과 같은 플러그인을 사용하여 확인할 수 있습니다.속성 처리 가격에 따른 실제 Woocommerce 제품 변형 외에도 이러한 플러그인을 사용하면 카트에 추가할 때 제품 수준에서 옵션 필드를 추가할 수 있습니다.이러한 "옵션" 특성에 대한 실제 제품 변형이 필요하지 않고 "주문 라인 항목 제품에 저장될 옵션 필드"만 필요한 경우 충분합니다.
3 - 후크 포함(카막스)
이 문제를 처리하기 위해 후크를 사용해야 하는데 필수 및 선택적 특성에 대한 변형이 이미 있는 경우.따라서 모든 속성에 대해 모든 제품 변형을 생성했습니다.그러나 결국 하나의 속성만 가격에 영향을 미치므로 많은 변형이 동일한 가격을 갖습니다.)
먼저 "기본" 특성 값으로 처리할 수 있습니다.따라서 "필요한 가격 영향" 속성만 변경하면 사용자는 항상 속성 조합에서 기존 제품 변형을 갖게 됩니다.카트에 추가할 수 있습니다.
어떤 이유로 인해 기본값을 미리 선택할 수 없는 경우에도 기본 속성 용어를 만들고 카트에 추가하기 전에 후크를 연결할 수 있습니다.여기서 POST 변수를 사용하여 다음 작업을 수행할 수 있습니다.
- 필요한 속성 값을 찾고, 따라서 DB에서 해당 제품 변형을 다시 찾습니다.
- 올바른 "필수" 특성과 기본 "비필수" 특성을 사용하여 변형을 카트에 추가합니다.
문제는 다양한 제품에 있습니다. 속성은 체크아웃 프로세스에서 아무것도 아닙니다.사용자는 프런트 엔드에서 옵션(속성)을 선택할 때 해당 제품 변형(admin 또는 woocommerce helper로 생성됨)이 있거나 없는 속성 조합을 선택합니다.Woocommerce는 카트에 무언가를 추가하기 위해 제품 페이지에서 볼 수 있는 기존 제품 변형(속성 조합)이 필요합니다.이러한 변형은 DB에서 카트와 주문에 라인 항목으로 추가할 수 있는 사용자 지정 post_type "product_variation"입니다.특성 조합에 일치하는 제품 변형이 없는 경우: 카트에 추가할 내용이 없습니다.
시도해 보세요
add_filter('woocommerce_dropdown_variation_attribute_options_args', 'setSelectDefaultVariableOption', 10, 1);
function setSelectDefaultVariableOption($args)
{
$default = $args['product']->get_default_attributes();
if (count($args['options']) > 0 && empty($default)) {
$args['selected'] = $args['options'][0];
}
return $args;
}
언급URL : https://stackoverflow.com/questions/67001571/woocommerce-ignore-unselected-variations-when-adding-to-cart
'itsource' 카테고리의 다른 글
Ruby의 "continue"에 해당합니다. (0) | 2023.07.16 |
---|---|
SQL Server에서 적용되는 인덱스 및 적용되는 쿼리란 무엇입니까? (0) | 2023.07.16 |
Spring JDBC 지원 및 대규모 데이터 세트 (0) | 2023.07.16 |
두 셀을 분할한 결과의 SUM(SUM PRODUCT 대신 분할) (0) | 2023.07.16 |
시작 페이지 초기화 중 Oracle SQL Developer 문제 (0) | 2023.07.16 |