Php 电子商务中基于自定义域的动态价格计算
在Woocommerce中,每个产品页面上都有一个文本框,允许用户输入自己的自定义文本。然后将此文本应用于产品,并按每个字母向客户收费 除了数学逻辑之外,我设法使一切都运转起来了。当访问者输入x个字母数量时,当前的数学逻辑会正确地计算产品价格+自定义字母的成本,并将此总和输出到Basket小部件 我遇到的问题是,当访问者进入购物篮页面时,定制字体的成本增加了一倍 我只是想不通为什么会这样 在产品仪表板中创建文本字段:Php 电子商务中基于自定义域的动态价格计算,php,wordpress,woocommerce,cart,price,Php,Wordpress,Woocommerce,Cart,Price,在Woocommerce中,每个产品页面上都有一个文本框,允许用户输入自己的自定义文本。然后将此文本应用于产品,并按每个字母向客户收费 除了数学逻辑之外,我设法使一切都运转起来了。当访问者输入x个字母数量时,当前的数学逻辑会正确地计算产品价格+自定义字母的成本,并将此总和输出到Basket小部件 我遇到的问题是,当访问者进入购物篮页面时,定制字体的成本增加了一倍 我只是想不通为什么会这样 在产品仪表板中创建文本字段: <?php /*Create Text Field in Produc
<?php
/*Create Text Field in Product Dashboard*/
function add_text_field_product_dashboard(){
global $post;
$input_checkbox = get_post_meta( $post->ID, '_custom_text_option', true );
if( empty( $input_checkbox ) || $input_checkbox == 'no' ) $input_checkbox = '';
echo '<div class="product_custom_field">';
/*Product Checkbox Field*/
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_option',
'desc' => __('set custom custom text field', 'woocommerce'),
'label' => __('Display custom custom text field', 'woocommerce'),
'desc_tip' => 'true',
'value' => $input_checkbox
)
);
/*Minimum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_minimum_custom_text_option',
'name' => '_minimum_custom_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Maximum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_maximum_custom_text_option',
'desc' => __('set custom maximum Lettering text field', 'woocommerce'),
'label' => __('Maximum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Cost Per Letter Pricing*/
woocommerce_wp_text_input(
array(
'id' => '_pricing_custom_text_option',
'desc' => __('set custom pricing Lettering text field', 'woocommerce'),
'label' => __('Cost Per Letter', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'add_text_field_product_dashboard');
?>
<?php
/*Save Inputted Entries, in the Product Dashboard Text Fields.*/
/*Checkbox Field*/
function woocommerce_product_custom_fields_save($post_id){
$_custom_text_option = isset( $_POST['_custom_text_option'] ) ? 'yes' : '';
update_post_meta( $post_id, '_custom_text_option', $_custom_text_option );
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
/*Save Minimum Letters*/
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_custom_text_option'] ) )
update_post_meta($post_id, '_minimum_custom_text_option', esc_attr( $_POST['_minimum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
/*Save Maximum Letters*/
function woocommerce_product_custom_fields_save2($post_id){
if ( isset( $_POST['_maximum_custom_text_option'] ) )
update_post_meta($post_id, '_maximum_custom_text_option', esc_attr( $_POST['_maximum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save2' );
/*Save Cost Per Letter*/
function woocommerce_product_custom_fields_save3($post_id){
if ( isset( $_POST['_pricing_custom_text_option'] ) )
update_post_meta($post_id, '_pricing_custom_text_option', esc_attr( $_POST['_pricing_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save3' );
?>
<?php
/*Output Custom Text Field to Product Page*/
function add_custom_text_field() {
global $post;
// Get the checkbox value
$custom_option = get_post_meta( $post->ID, '_custom_text_option', true );
// If is single product page and have the "custom text option" enabled we display the field
if ( is_product() && ! empty($custom_option) ) {
?>
<div>
<label class="product-custom-text-label" for="custom_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="custom_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_custom_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_custom_text_option',true);?>" />
</label>
</div><br>
<?php
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_text_field', 0 );
?>
?>
<?php
/*Append to Cart once Shoper adds to Cart*/
function save_custom_text( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_text'] ) && !empty( $_POST['custom_text'] ) ) {
$cart_item_data[ "custom_text" ] = esc_attr( $_POST['custom_text'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_text', 99, 2 );
?>
<?php
/*Sum of Product Price plus Custom Text*/
function calculate_custom_text_fee( $cart_object ) {
foreach ( $cart_object->get_cart() as $cart_item ) {
// Checking that we got the custom text in cart object
if( ! empty( $cart_item["custom_text"] ) ) {
// Quantity of characters entered into Custom Text Field:
$lenght = strlen( $cart_item["custom_text"] );
// get the custom pricing for this product
$pricing_custom = get_post_meta( $cart_item['product_id'], '_pricing_custom_text_option', true );
// Characters Entered Multiplied by Cost of Each Letter:
$custom_text_fee = $lenght * $pricing_custom;
// get product price
$price = floatval( $cart_item['data']->get_price() );
// set new price
$cart_item['data']->set_price( $price + $custom_text_fee );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_custom_text_fee', 99, 1 );
?>
<?php
/*Output to Cart Description*/
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$meta_items = array();
if( !empty( $cart_data ) ) {
$meta_items = $cart_data;
}
if( isset( $cart_item["custom_text"] ) ) {
$meta_items[] = array( "name" => "Your Custom Text", "value" => $cart_item["custom_text"] );
}
return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );
?>
<?php
/*Ensure Product is in Email Notifications*/
function custom_text_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values["custom_text"] ) ) {
wc_add_order_item_meta( $item_id, "Custom Text", $values["custom_text"] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'custom_text_order_meta_handler', 99, 3 );
?>
保存文本字段条目:
<?php
/*Create Text Field in Product Dashboard*/
function add_text_field_product_dashboard(){
global $post;
$input_checkbox = get_post_meta( $post->ID, '_custom_text_option', true );
if( empty( $input_checkbox ) || $input_checkbox == 'no' ) $input_checkbox = '';
echo '<div class="product_custom_field">';
/*Product Checkbox Field*/
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_option',
'desc' => __('set custom custom text field', 'woocommerce'),
'label' => __('Display custom custom text field', 'woocommerce'),
'desc_tip' => 'true',
'value' => $input_checkbox
)
);
/*Minimum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_minimum_custom_text_option',
'name' => '_minimum_custom_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Maximum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_maximum_custom_text_option',
'desc' => __('set custom maximum Lettering text field', 'woocommerce'),
'label' => __('Maximum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Cost Per Letter Pricing*/
woocommerce_wp_text_input(
array(
'id' => '_pricing_custom_text_option',
'desc' => __('set custom pricing Lettering text field', 'woocommerce'),
'label' => __('Cost Per Letter', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'add_text_field_product_dashboard');
?>
<?php
/*Save Inputted Entries, in the Product Dashboard Text Fields.*/
/*Checkbox Field*/
function woocommerce_product_custom_fields_save($post_id){
$_custom_text_option = isset( $_POST['_custom_text_option'] ) ? 'yes' : '';
update_post_meta( $post_id, '_custom_text_option', $_custom_text_option );
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
/*Save Minimum Letters*/
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_custom_text_option'] ) )
update_post_meta($post_id, '_minimum_custom_text_option', esc_attr( $_POST['_minimum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
/*Save Maximum Letters*/
function woocommerce_product_custom_fields_save2($post_id){
if ( isset( $_POST['_maximum_custom_text_option'] ) )
update_post_meta($post_id, '_maximum_custom_text_option', esc_attr( $_POST['_maximum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save2' );
/*Save Cost Per Letter*/
function woocommerce_product_custom_fields_save3($post_id){
if ( isset( $_POST['_pricing_custom_text_option'] ) )
update_post_meta($post_id, '_pricing_custom_text_option', esc_attr( $_POST['_pricing_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save3' );
?>
<?php
/*Output Custom Text Field to Product Page*/
function add_custom_text_field() {
global $post;
// Get the checkbox value
$custom_option = get_post_meta( $post->ID, '_custom_text_option', true );
// If is single product page and have the "custom text option" enabled we display the field
if ( is_product() && ! empty($custom_option) ) {
?>
<div>
<label class="product-custom-text-label" for="custom_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="custom_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_custom_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_custom_text_option',true);?>" />
</label>
</div><br>
<?php
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_text_field', 0 );
?>
?>
<?php
/*Append to Cart once Shoper adds to Cart*/
function save_custom_text( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_text'] ) && !empty( $_POST['custom_text'] ) ) {
$cart_item_data[ "custom_text" ] = esc_attr( $_POST['custom_text'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_text', 99, 2 );
?>
<?php
/*Sum of Product Price plus Custom Text*/
function calculate_custom_text_fee( $cart_object ) {
foreach ( $cart_object->get_cart() as $cart_item ) {
// Checking that we got the custom text in cart object
if( ! empty( $cart_item["custom_text"] ) ) {
// Quantity of characters entered into Custom Text Field:
$lenght = strlen( $cart_item["custom_text"] );
// get the custom pricing for this product
$pricing_custom = get_post_meta( $cart_item['product_id'], '_pricing_custom_text_option', true );
// Characters Entered Multiplied by Cost of Each Letter:
$custom_text_fee = $lenght * $pricing_custom;
// get product price
$price = floatval( $cart_item['data']->get_price() );
// set new price
$cart_item['data']->set_price( $price + $custom_text_fee );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_custom_text_fee', 99, 1 );
?>
<?php
/*Output to Cart Description*/
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$meta_items = array();
if( !empty( $cart_data ) ) {
$meta_items = $cart_data;
}
if( isset( $cart_item["custom_text"] ) ) {
$meta_items[] = array( "name" => "Your Custom Text", "value" => $cart_item["custom_text"] );
}
return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );
?>
<?php
/*Ensure Product is in Email Notifications*/
function custom_text_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values["custom_text"] ) ) {
wc_add_order_item_meta( $item_id, "Custom Text", $values["custom_text"] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'custom_text_order_meta_handler', 99, 3 );
?>
将自定义文本输出到产品页面:
<?php
/*Create Text Field in Product Dashboard*/
function add_text_field_product_dashboard(){
global $post;
$input_checkbox = get_post_meta( $post->ID, '_custom_text_option', true );
if( empty( $input_checkbox ) || $input_checkbox == 'no' ) $input_checkbox = '';
echo '<div class="product_custom_field">';
/*Product Checkbox Field*/
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_option',
'desc' => __('set custom custom text field', 'woocommerce'),
'label' => __('Display custom custom text field', 'woocommerce'),
'desc_tip' => 'true',
'value' => $input_checkbox
)
);
/*Minimum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_minimum_custom_text_option',
'name' => '_minimum_custom_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Maximum Letter Text Box*/
woocommerce_wp_text_input(
array(
'id' => '_maximum_custom_text_option',
'desc' => __('set custom maximum Lettering text field', 'woocommerce'),
'label' => __('Maximum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
/*Cost Per Letter Pricing*/
woocommerce_wp_text_input(
array(
'id' => '_pricing_custom_text_option',
'desc' => __('set custom pricing Lettering text field', 'woocommerce'),
'label' => __('Cost Per Letter', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'add_text_field_product_dashboard');
?>
<?php
/*Save Inputted Entries, in the Product Dashboard Text Fields.*/
/*Checkbox Field*/
function woocommerce_product_custom_fields_save($post_id){
$_custom_text_option = isset( $_POST['_custom_text_option'] ) ? 'yes' : '';
update_post_meta( $post_id, '_custom_text_option', $_custom_text_option );
}
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
/*Save Minimum Letters*/
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_custom_text_option'] ) )
update_post_meta($post_id, '_minimum_custom_text_option', esc_attr( $_POST['_minimum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
/*Save Maximum Letters*/
function woocommerce_product_custom_fields_save2($post_id){
if ( isset( $_POST['_maximum_custom_text_option'] ) )
update_post_meta($post_id, '_maximum_custom_text_option', esc_attr( $_POST['_maximum_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save2' );
/*Save Cost Per Letter*/
function woocommerce_product_custom_fields_save3($post_id){
if ( isset( $_POST['_pricing_custom_text_option'] ) )
update_post_meta($post_id, '_pricing_custom_text_option', esc_attr( $_POST['_pricing_custom_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save3' );
?>
<?php
/*Output Custom Text Field to Product Page*/
function add_custom_text_field() {
global $post;
// Get the checkbox value
$custom_option = get_post_meta( $post->ID, '_custom_text_option', true );
// If is single product page and have the "custom text option" enabled we display the field
if ( is_product() && ! empty($custom_option) ) {
?>
<div>
<label class="product-custom-text-label" for="custom_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="custom_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_custom_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_custom_text_option',true);?>" />
</label>
</div><br>
<?php
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_text_field', 0 );
?>
?>
<?php
/*Append to Cart once Shoper adds to Cart*/
function save_custom_text( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_text'] ) && !empty( $_POST['custom_text'] ) ) {
$cart_item_data[ "custom_text" ] = esc_attr( $_POST['custom_text'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_text', 99, 2 );
?>
<?php
/*Sum of Product Price plus Custom Text*/
function calculate_custom_text_fee( $cart_object ) {
foreach ( $cart_object->get_cart() as $cart_item ) {
// Checking that we got the custom text in cart object
if( ! empty( $cart_item["custom_text"] ) ) {
// Quantity of characters entered into Custom Text Field:
$lenght = strlen( $cart_item["custom_text"] );
// get the custom pricing for this product
$pricing_custom = get_post_meta( $cart_item['product_id'], '_pricing_custom_text_option', true );
// Characters Entered Multiplied by Cost of Each Letter:
$custom_text_fee = $lenght * $pricing_custom;
// get product price
$price = floatval( $cart_item['data']->get_price() );
// set new price
$cart_item['data']->set_price( $price + $custom_text_fee );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_custom_text_fee', 99, 1 );
?>
<?php
/*Output to Cart Description*/
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$meta_items = array();
if( !empty( $cart_data ) ) {
$meta_items = $cart_data;
}
if( isset( $cart_item["custom_text"] ) ) {
$meta_items[] = array( "name" => "Your Custom Text", "value" => $cart_item["custom_text"] );
}
return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );
?>
<?php
/*Ensure Product is in Email Notifications*/
function custom_text_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values["custom_text"] ) ) {
wc_add_order_item_meta( $item_id, "Custom Text", $values["custom_text"] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'custom_text_order_meta_handler', 99, 3 );
?>
要按id获取产品价格,请执行以下操作:
$\u product=wc\u get\u product($product\u id)
$\u产品->获取正常价格()
$\产品->获取\销售\价格()
$\产品->获取\价格()
或者,
获得固定价格:
$price=get_post_meta(get_ID(),'u regular_price',true);
//$price将返回正常价格
获取销售价格:
$sale=get_post_meta(get_ID(),'u sale_price',true);
//$sale将返回销售价格更新
为了避免这个问题,我在进行新的价格计算之前,使用woocommerce\u add\u cart\u item\u data
hook(用于在cart对象中保存自定义文本的钩子)将其保存在cart对象中
注意:对于您的文本,我使用trim()
php函数删除长度字母计数中的空格…如果您不需要,可以删除它
我重新访问了您的代码,并做了一些更改:
// Add custom fields in "product data" settings metabox ("Advanced" tab)
add_action('woocommerce_product_options_advanced', 'add_text_field_product_dashboard');
function add_text_field_product_dashboard(){
global $post;
echo '<div class="product_custom_field">';
// Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_custom_text_option',
'description' => __('set custom custom text field', 'woocommerce'),
'label' => __('Display custom custom text field', 'woocommerce'),
'desc_tip' => 'true',
) );
// Minimum Letter Text Box
woocommerce_wp_text_input( array(
'id' => '_minimum_custom_text_option',
'label' => __('Minimum Letters', 'woocommerce'),
'description' => __('set custom minimum Lettering text field', 'woocommerce'),
'desc_tip' => 'true',
) );
// Maximum Letter Text Box
woocommerce_wp_text_input( array(
'id' => '_maximum_custom_text_option',
'label' => __('Maximum Letters', 'woocommerce'),
'description' => __('set custom maximum Lettering text field', 'woocommerce'),
'desc_tip' => 'true'
) );
// Cost Per Letter Pricing
woocommerce_wp_text_input( array(
'id' => '_pricing_custom_text_option',
'label' => __('Cost Per Letter', 'woocommerce'),
'description' => __('set custom pricing Lettering text field', 'woocommerce'),
'desc_tip' => 'true'
) );
echo '</div>';
}
// Save Inputted Entries, in the Product Dashboard Text Fields.
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id){
// Checkbox Field
$checkbox = isset( $_POST['_custom_text_option'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_custom_text_option', $checkbox );
// Save Minimum Letters
if ( isset( $_POST['_minimum_custom_text_option'] ) )
update_post_meta($post_id, '_minimum_custom_text_option', sanitize_text_field( $_POST['_minimum_custom_text_option'] ) );
// Save Maximum Letters
if ( isset( $_POST['_maximum_custom_text_option'] ) )
update_post_meta($post_id, '_maximum_custom_text_option', sanitize_text_field( $_POST['_maximum_custom_text_option'] ) );
// Save Cost Per Letter
if ( isset( $_POST['_pricing_custom_text_option'] ) )
update_post_meta($post_id, '_pricing_custom_text_option', sanitize_text_field( $_POST['_pricing_custom_text_option'] ) );
}
// Output Custom Text Field to Product Page
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_text_field', 0 );
function add_custom_text_field() {
global $post;
// Get the checkbox value
$custom_option = get_post_meta( $post->ID, '_custom_text_option', true );
// If is single product page and have the "custom text option" enabled we display the field
if ( is_product() && ! empty($custom_option) ) {
?>
<div>
<label class="product-custom-text-label" for="custom_text"><?php _e( 'Custom Letters:', 'woocommerce'); ?><br>
<input style="min-width:220px" type="text" class="product-counter" name="custom_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" minlength="<?php global $post; echo get_post_meta($post->ID,'_minimum_custom_text_option',true);?>" maxlength="<?php global $post; echo get_post_meta($post->ID,'_maximum_custom_text_option',true);?>" />
</label>
</div><br>
<?php
}
}
// Set custom text and calculated price as custom cart data in the cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_in_cart_object', 30, 3 );
function save_custom_data_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
if( ! isset( $_POST['custom_text'] ) || empty( $_POST['custom_text'] ) )
return $cart_item_data;
// Get the custom text cost by letter
$pricing_custom = (float) get_post_meta( $product_id, '_pricing_custom_text_option', true );
// Get an instance of the WC_Product object
$product = $variation_id > 0 ? wc_get_product($variation_id) : wc_get_product($product_id);
$product_price = (float) $product->get_price(); // Get the product price
// Get the text
$custom_text = sanitize_text_field ( $_POST['custom_text'] );
// Get lenght (trimming white spaces)
$lenght = (float) strlen ( trim( $custom_text ) );
// Set the text and the calculated price as custom cart data in the cart item
$cart_item_data['custom_data']['price'] = $product_price + ( $lenght * $pricing_custom );
$cart_item_data['custom_data']['text'] = $custom_text;
return $cart_item_data;
}
// Display Custom text in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
if( isset( $cart_item['custom_data']['text'] ) )
$cart_data[] = array( "name" => "Your Custom Text", "value" => $cart_item["custom_data"]["text"] );
return $cart_data;
}
// Set the new calculated price of the cart item
add_action( 'woocommerce_before_calculate_totals', 'calculate_custom_text_fee', 99, 1 );
function calculate_custom_text_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset( $cart_item['custom_data']['price'] ) ) {
// Get the new calculated price
$new_price = (float) $cart_item['custom_data']['price'];
// Set the new calculated price
$cart_item['data']->set_price( $new_price );
}
}
}
// Save the custom text as order item data (displaying it in order and notifications)
add_action( 'woocommerce_add_order_item_meta', 'custom_text_order_meta_handler', 99, 3 );
function custom_text_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data']['text'] ) )
wc_add_order_item_meta( $item_id, "Custom Text", $values["custom_data"]["text"] );
}
//在“产品数据”设置元框(“高级”选项卡)中添加自定义字段
添加操作(“商业、产品、选项、高级”、“添加文本、字段、产品、仪表板”);
函数添加\文本\字段\产品\仪表板(){
全球$员额;
回声';
//复选框字段
woocommerce\u wp\u复选框(数组(
'id'=>''自定义文本选项',
“description'=>”(设置自定义文本字段)、“woocommerce”),
“label'=>”(显示自定义文本字段)、“woocommerce”),
'desc_tip'=>'true',
) );
//最小字母文本框
woocommerce_wp_text_输入(数组(
'id'=>''最小值\自定义\文本\选项',
'label'=>\('Minimum Letters','woocommerce'),
“description'=>”(设置自定义最小字母文本字段)、“woocommerce”),
'desc_tip'=>'true',
) );
//最大字母文本框
woocommerce_wp_text_输入(数组(
'id'=>''最大值\自定义\文本\选项',
“label'=>”(最大字母数),“woocommerce”),
“description'=>”(设置自定义最大字母文本字段)、“woocommerce”),
“描述提示”=>“正确”
) );
//每封信成本定价
woocommerce_wp_text_输入(数组(
'id'=>''定价\自定义\文本\选项',
“标签”=>“‘每字母成本’、‘woocommerce’”,
“description'=>”(设置自定义定价文字文本字段)、“woocommerce”),
“描述提示”=>“正确”
) );
回声';
}
//在产品仪表板文本字段中保存输入的条目。
添加操作('woocommerce\u process\u product\u meta'、'woocommerce\u product\u custom\u fields\u save');
功能商业\产品\自定义\字段\保存($post\u id){
//复选框字段
$checkbox=isset($\u POST[“自定义文本”选项])?“是”:“否”;
更新发布元($post\u id,“\u自定义文本选项,$checkbox”);
//保存最小字母数
如果(isset($\u POST[''最小值\u自定义\u文本\u选项]))
更新发布元数据($发布id,''最小值''自定义文本选项',清理文本字段($发布[''最小值''自定义文本选项]);
//保存最大字母数
如果(isset($\u POST[''最大值\u自定义\u文本\u选项]))
更新发布元数据($发布id、$最大值\自定义文本\选项、$清理文本\字段($发布[''最大值\自定义文本\选项]);
//节省每封信的费用
如果(isset($\u POST[''定价\自定义\文本\选项]))
更新发布元数据($发布id,''定价''自定义文本选项',清理文本字段($发布[''定价''自定义文本选项]);
}
//将自定义文本字段输出到产品页面
添加操作(“添加到购物车按钮之前的woocommerce”和“添加自定义文本”字段”,0);
函数添加\自定义\文本\字段(){
全球$员额;
//获取复选框值
$custom\u option=get\u post\u meta($post->ID,'u custom\u text\u option',true);
//如果是单个产品页面并启用了“自定义文本选项”,则显示该字段
if(is_product()&&&!empty($custom_选项)){
?>
感谢您抽出时间提供您的见解@LoiceTheAztec。我不知道其他编码会是一个因素,也不想不必要地发布一个冗长的问题。我已经看过您的编码,但它与我的不同,并且在尝试应用建议时有点困难。因此,我修改了我的搜索ion,以显示整个编码。我只能假设我在某个地方复制了数学逻辑,但无法确定这可能在哪里。@Craig使用您更新的答案代码更新了我的答案…试试看,它可以工作。没有更多问题。感谢您花时间提供如此全面的编辑。不幸的是,错误仍然存在。W编辑后,自定义文本框将从产品页面中消失,不会在购物篮中输出。因此,只输出产品价格,而不是输入自定义文本框中的每个字母的产品价格和成本。我将看看是否可以找出原因。最终使代码正常工作。感谢您的帮助。