因为我试图添加一个额外的数据,以便我可以获取和过滤数据,根据元后,成功全额支付。让我用代码来解释。
$cart_data = array(
'product_id' => $product_id,
'payment_type' => 'subscription',
'**what_is_being_promoted**' => $promoting,
);
WC()->cart->add_to_cart($product_id, 1, null, null, $cart_item_data);现在我想读一下classifier_update_user_data_upon_payment钩子中的键classifier_update_user_data_upon_payment。如何实现这一目标。
发布于 2021-05-04 03:25:44
您可以使用下面的筛选器/操作向cart项添加其他数据:
//Add custom cart item data
function zwtadd_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if( isset( $_POST['what_is_being_promoted'] ) ) {
$cart_item_data['zwt_field'] = sanitize_text_field( $_POST['what_is_being_promoted'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'zwtadd_cart_item_data', 10, 3 );
//Display custom item data in the cart
function zwt_get_item_data( $item_data, $cart_item_data ) {
if( isset( $cart_item_data['zwt_field'] ) ) {
$item_data[] = array(
'key' => __( 'Is promoted', 'text-domain' ),
'value' => wc_clean( $cart_item_data['zwt_field'] )
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'zwt_get_item_data', 10, 2 );
//Add custom meta to order
function zwt_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['zwt_field'] ) ) {
$item->add_meta_data(__( 'Is promoted', 'text-domain' ), $values['zwt_field'], true);
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'zwt_checkout_create_order_line_item', 10, 4 );以上代码将向购物车项目添加数据,将数据显示到购物车中,并按顺序显示数据。
https://stackoverflow.com/questions/67366019
复制相似问题