WooCommerce:将产品作为不同的项目多次添加到购物车,但按其元数据分组
WooCommerce: add product to cart multiple times as different items but grouped by its meta data
我只有一种产品 ('testare-psihologica')。当买家购买它时,他必须将此产品与影响价格的附加信息(特定 'test' 标题)相关联。我不想使用可变产品,因为那些 'test' 标题会很多,所以可变产品对我来说不是很方便。在这种情况下,我选择多次添加此产品作为单独的项目,但具有不同的 'test' 标题。
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_simple_product_custom_price', 20, 2 );
function add_cart_simple_product_custom_price( $cart_item_data, $product_id ) {
//add selected test ID to 'testare-psihologica' cart item
if( isset( $_GET['test'] ) ) {
$test_id = get_page_by_path( $_GET['test'], OBJECT, 'tests' )->ID;
$test_title = get_post_field( 'post_title', $test_id );
$cart_item_data['test_title'] = $test_title;
}
// add product to cart multiple times, but as different items
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
在此之后,'test' 标题将显示在产品名称下的购物车和结帐页面中,然后它们将作为元数据(使用其他代码)添加到订单中。
这种方法的唯一缺点是当产品被多次添加到购物车但具有相同(相同)'test' 标题时,它也显示为单独的项目,但我想要这些项目看起来像一个整体,只会增加它们的数量。所以,而不是这个:
我想要这个:
我想以编程方式执行此操作。如何实现?
看来下面几行代码是多余的:
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
所以要回答您的问题,您只需删除这些行
因为我没有你在答案中使用的相同获取数据,所以我做了一个概念验证,分为 3 个步骤:
1.在单品页面加入购物车按钮前添加输入框
function action_woocommerce_before_add_to_cart_button() {
// Add a new input field, allowing the customer to set "test"
echo '<div class="my-div"><input type="text" id="test" name="test"></div>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );
2. 将 'test' 值添加到购物车商品
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// Add test to cart item
if ( ! empty ( $_POST[ 'test' ] ) ) {
$cart_item_data['test_title'] = sanitize_text_field( $_POST['test'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 10, 3 );
3. 可选择在购物车和结帐页面中显示自定义数据
// Optionally display custom data in cart and checkout pages
function filter_woocommerce_get_item_data( $cart_data, $cart_item = null ) {
if ( isset( $cart_item['test_title'] ) ) {
$cart_data[] = array(
'name' => 'Test title',
'value' => $cart_item['test_title']
);
}
return $cart_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 99, 2 );
然后在第一步应用以下步骤:
- 插入
group1
作为值并按添加到购物车按钮
- 插入
group2
作为值并按添加到购物车按钮
- 插入
group1
作为值并按添加到购物车按钮
值为group1
的产品将根据对应的值自动分组:
我只有一种产品 ('testare-psihologica')。当买家购买它时,他必须将此产品与影响价格的附加信息(特定 'test' 标题)相关联。我不想使用可变产品,因为那些 'test' 标题会很多,所以可变产品对我来说不是很方便。在这种情况下,我选择多次添加此产品作为单独的项目,但具有不同的 'test' 标题。
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_simple_product_custom_price', 20, 2 );
function add_cart_simple_product_custom_price( $cart_item_data, $product_id ) {
//add selected test ID to 'testare-psihologica' cart item
if( isset( $_GET['test'] ) ) {
$test_id = get_page_by_path( $_GET['test'], OBJECT, 'tests' )->ID;
$test_title = get_post_field( 'post_title', $test_id );
$cart_item_data['test_title'] = $test_title;
}
// add product to cart multiple times, but as different items
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
在此之后,'test' 标题将显示在产品名称下的购物车和结帐页面中,然后它们将作为元数据(使用其他代码)添加到订单中。
这种方法的唯一缺点是当产品被多次添加到购物车但具有相同(相同)'test' 标题时,它也显示为单独的项目,但我想要这些项目看起来像一个整体,只会增加它们的数量。所以,而不是这个:
我想要这个:
我想以编程方式执行此操作。如何实现?
看来下面几行代码是多余的:
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
所以要回答您的问题,您只需删除这些行
因为我没有你在答案中使用的相同获取数据,所以我做了一个概念验证,分为 3 个步骤:
1.在单品页面加入购物车按钮前添加输入框
function action_woocommerce_before_add_to_cart_button() {
// Add a new input field, allowing the customer to set "test"
echo '<div class="my-div"><input type="text" id="test" name="test"></div>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'action_woocommerce_before_add_to_cart_button' );
2. 将 'test' 值添加到购物车商品
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// Add test to cart item
if ( ! empty ( $_POST[ 'test' ] ) ) {
$cart_item_data['test_title'] = sanitize_text_field( $_POST['test'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 10, 3 );
3. 可选择在购物车和结帐页面中显示自定义数据
// Optionally display custom data in cart and checkout pages
function filter_woocommerce_get_item_data( $cart_data, $cart_item = null ) {
if ( isset( $cart_item['test_title'] ) ) {
$cart_data[] = array(
'name' => 'Test title',
'value' => $cart_item['test_title']
);
}
return $cart_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 99, 2 );
然后在第一步应用以下步骤:
- 插入
group1
作为值并按添加到购物车按钮 - 插入
group2
作为值并按添加到购物车按钮 - 插入
group1
作为值并按添加到购物车按钮
值为group1
的产品将根据对应的值自动分组: