在foreach内获取WooCommerce产品的价格,并使用百分比折扣
我尝试执行以下操作:
我在结帐处添加了一个文本字段,客户必须在其中输入代码,如果该代码是我想要的,我会根据类别对产品应用折扣。
到目前为止我遇到的问题是我只能申请固定费用,我想根据个别产品的价格(取决于每个产品)申请一定的百分比费用。
另外,现在文本字段尚未在前端为客户进行验证,我该如何添加呢?我希望客户在结帐前知道他是否输入了正确的代码。
谢谢你!
function custom_donation_form() { ?>
<input type="number" name="card_code" class="input-text" style="display: inline-block !important; width: auto !important;" placeholder="<?php echo esc_attr( 'Card Code:', 'woocommerce' ); ?>" value="" />
<?php
}
add_action( 'woocommerce_review_order_before_submit', 'custom_donation_form', 99 );
function donation_add_fee( $cart ) {
if( isset( $_REQUEST["card_code"] ) && !empty( $_REQUEST["card_code"] ) ) {
if( is_numeric( $_REQUEST["card_code"] ) && $_REQUEST["card_code"] == 1000 ) {
// You need to enter your fees here, in `category slug` => `fee amount` format
$fees = array(
'botas' => 10);
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product_categories = get_the_terms( $values['product_id'], 'product_cat' );
if ( $product_categories && ! is_wp_error( $product_categories ) ) {
$product_categories = wp_list_pluck( $product_categories, 'slug' );
foreach ( $product_categories as $product_category ) {
if ( ! empty( $fees[ $product_category ] ) ) {
$name = 'Category fee: ' . get_the_title( $values['product_id'] );
$amount = $fees[ $product_category ];
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
}
}
}
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'donation_add_fee', 1 );
Im trying to do the following:
I added a text field on the checkout where the customer has to enter a code, if the code is what I want, I apply a discount to the products depending on the category.
The problem I have until now is that I can only apply a fixed fee, I want to apply a percentage fee depending on the individual product price (depending on each product).
Also, right now the text field has not validation on the front end for the customer, how could I add that? I would like for the customer to know if he put the right code or not before checking out.
Thank you!
function custom_donation_form() { ?>
<input type="number" name="card_code" class="input-text" style="display: inline-block !important; width: auto !important;" placeholder="<?php echo esc_attr( 'Card Code:', 'woocommerce' ); ?>" value="" />
<?php
}
add_action( 'woocommerce_review_order_before_submit', 'custom_donation_form', 99 );
function donation_add_fee( $cart ) {
if( isset( $_REQUEST["card_code"] ) && !empty( $_REQUEST["card_code"] ) ) {
if( is_numeric( $_REQUEST["card_code"] ) && $_REQUEST["card_code"] == 1000 ) {
// You need to enter your fees here, in `category slug` => `fee amount` format
$fees = array(
'botas' => 10);
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product_categories = get_the_terms( $values['product_id'], 'product_cat' );
if ( $product_categories && ! is_wp_error( $product_categories ) ) {
$product_categories = wp_list_pluck( $product_categories, 'slug' );
foreach ( $product_categories as $product_category ) {
if ( ! empty( $fees[ $product_category ] ) ) {
$name = 'Category fee: ' . get_the_title( $values['product_id'] );
$amount = $fees[ $product_category ];
$taxable = true;
$tax_class = '';
$cart->add_fee( $name, $amount, $taxable, $tax_class );
}
}
}
}
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'donation_add_fee', 1 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论