Checkbox WooCommerce插件,如何为多个产品向1个礼品盒收费
我的WooCommerce网站有问题 我目前拥有的是一个插件,它允许我为每个产品页面放置一个复选框,显示“将此产品添加到礼品盒?”。 这个篮子要花5欧元 问题是,如果客户希望在同一篮子中添加其他产品,他们必须单击该页面上的“将此产品添加到礼品盒?”复选框。 这将再增加5欧元。 这就是问题所在 我们的目标是保持1篮的价格,但同时 让人们可以选择添加更多产品,而不必为每种产品收取另一篮子费用,只需要一次 我正在使用的插件已经2年没有更新了,所以问这个问题几乎是死路一条 我使用的插件是“WooCommerce产品礼品包装”。 链接: 编码为:Checkbox WooCommerce插件,如何为多个产品向1个礼品盒收费,checkbox,plugins,woocommerce,Checkbox,Plugins,Woocommerce,我的WooCommerce网站有问题 我目前拥有的是一个插件,它允许我为每个产品页面放置一个复选框,显示“将此产品添加到礼品盒?”。 这个篮子要花5欧元 问题是,如果客户希望在同一篮子中添加其他产品,他们必须单击该页面上的“将此产品添加到礼品盒?”复选框。 这将再增加5欧元。 这就是问题所在 我们的目标是保持1篮的价格,但同时 让人们可以选择添加更多产品,而不必为每种产品收取另一篮子费用,只需要一次 我正在使用的插件已经2年没有更新了,所以问这个问题几乎是死路一条 我使用的插件是“WooComm
<?php
*/
/**
*本地化
*/
加载插件文本域('woocommerce product gift wrap',false,dirname(插件基本名称(文件)./languages/)
/**
*WC_产品_礼品_包装类。
*/
WC类\u产品\u礼品\u包装{
/**
* Hook us in :)
*
* @access public
* @return void
*/
public function __construct() {
$default_message = '{checkbox} '. sprintf( __( 'Gift wrap this item for %s?', 'woocommerce-product-gift-wrap' ), '{price}' );
$this->gift_wrap_enabled = get_option( 'product_gift_wrap_enabled' ) == 'yes' ? true : false;
$this->gift_wrap_cost = get_option( 'product_gift_wrap_cost', 0 );
$this->product_gift_wrap_message = get_option( 'product_gift_wrap_message' );
if ( ! $this->product_gift_wrap_message ) {
$this->product_gift_wrap_message = $default_message;
}
add_option( 'product_gift_wrap_enabled', 'no' );
add_option( 'product_gift_wrap_cost', '0' );
add_option( 'product_gift_wrap_message', $default_message );
// Init settings
$this->settings = array(
array(
'name' => __( 'Gift Wrapping Enabled by Default?', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'Enable this to allow gift wrapping for products by default.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_enabled',
'type' => 'checkbox',
),
array(
'name' => __( 'Default Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'The cost of gift wrap unless overridden per-product.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_cost',
'type' => 'text',
'desc_tip' => true
),
array(
'name' => __( 'Gift Wrap Message', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_message',
'desc' => __( 'Note: <code>{checkbox}</code> will be replaced with a checkbox and <code>{price}</code> will be replaced with the gift wrap cost.', 'woocommerce-product-gift-wrap' ),
'type' => 'text',
'desc_tip' => __( 'The checkbox and label shown to the user on the frontend.', 'woocommerce-product-gift-wrap' )
),
);
// Display on the front end
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'gift_option_html' ), 10 );
// Filters for cart actions
add_filter( 'woocommerce_add_cart_item_data', array( $this, 'add_cart_item_data' ), 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item', array( $this, 'add_cart_item' ), 10, 1 );
add_action( 'woocommerce_add_order_item_meta', array( $this, 'add_order_item_meta' ), 10, 2 );
// Write Panels
add_action( 'woocommerce_product_options_pricing', array( $this, 'write_panel' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'write_panel_save' ) );
// Admin
add_action( 'woocommerce_settings_general_options_end', array( $this, 'admin_settings' ) );
add_action( 'woocommerce_update_options_general', array( $this, 'save_admin_settings' ) );
}
/**
* Show the Gift Checkbox on the frontend
*
* @access public
* @return void
*/
public function gift_option_html() {
global $post;
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( $is_wrappable == 'yes' ) {
$current_value = ! empty( $_REQUEST['gift_wrap'] ) ? 1 : 0;
$cost = get_post_meta( $post->ID, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$price_text = $cost > 0 ? woocommerce_price( $cost ) : __( 'free', 'woocommerce-product-gift-wrap' );
$checkbox = '<input type="checkbox" name="gift_wrap" value="yes" ' . checked( $current_value, 1, false ) . ' />';
woocommerce_get_template( 'gift-wrap.php', array(
'product_gift_wrap_message' => $this->product_gift_wrap_message,
'checkbox' => $checkbox,
'price_text' => $price_text
), 'woocommerce-product-gift-wrap', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
}
}
/**
* When added to cart, save any gift data
*
* @access public
* @param mixed $cart_item_meta
* @param mixed $product_id
* @return void
*/
public function add_cart_item_data( $cart_item_meta, $product_id ) {
$is_wrappable = get_post_meta( $product_id, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( ! empty( $_POST['gift_wrap'] ) && $is_wrappable == 'yes' ) {
$cart_item_meta['gift_wrap'] = true;
}
return $cart_item_meta;
}
/**
* Get the gift data from the session on page load
*
* @access public
* @param mixed $cart_item
* @param mixed $values
* @return void
*/
public function get_cart_item_from_session( $cart_item, $values ) {
if ( ! empty( $values['gift_wrap'] ) ) {
$cart_item['gift_wrap'] = true;
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* Display gift data if present in the cart
*
* @access public
* @param mixed $other_data
* @param mixed $cart_item
* @return void
*/
public function get_item_data( $item_data, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) )
$item_data[] = array(
'name' => __( 'In geschenkmand', 'woocommerce-product-gift-wrap' ),
'value' => __( 'Yes', 'woocommerce-product-gift-wrap' ),
'display' => __( 'Ja', 'woocommerce-product-gift-wrap' )
);
return $item_data;
}
/**
* Adjust price after adding to cart
*
* @access public
* @param mixed $cart_item
* @return void
*/
public function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* After ordering, add the data to the order line items.
*
* @access public
* @param mixed $item_id
* @param mixed $values
* @return void
*/
public function add_order_item_meta( $item_id, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
woocommerce_add_order_item_meta( $item_id, __( 'In geschenkmand', 'woocommerce-product-gift-wrap' ), __( 'Ja', 'woocommerce-product-gift-wrap' ) );
}
}
/**
* write_panel function.
*
* @access public
* @return void
*/
public function write_panel() {
global $post;
echo '</div><div class="options_group show_if_simple show_if_variable">';
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
woocommerce_wp_checkbox( array(
'id' => '_is_gift_wrappable',
'wrapper_class' => '',
'value' => $is_wrappable,
'label' => __( 'Gift Wrappable', 'woocommerce-product-gift-wrap' ),
'description' => __( 'Enable this option if the customer can choose gift wrapping.', 'woocommerce-product-gift-wrap' ),
) );
woocommerce_wp_text_input( array(
'id' => '_gift_wrap_cost',
'label' => __( 'Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'placeholder' => $this->gift_wrap_cost,
'desc_tip' => true,
'description' => __( 'Override the default cost by inputting a cost here.', 'woocommerce-product-gift-wrap' ),
) );
wc_enqueue_js( "
jQuery('input#_is_gift_wrappable').change(function(){
jQuery('._gift_wrap_cost_field').hide();
if ( jQuery('#_is_gift_wrappable').is(':checked') ) {
jQuery('._gift_wrap_cost_field').show();
}
}).change();
" );
}
/**
* write_panel_save function.
*
* @access public
* @param mixed $post_id
* @return void
*/
public function write_panel_save( $post_id ) {
$_is_gift_wrappable = ! empty( $_POST['_is_gift_wrappable'] ) ? 'yes' : 'no';
$_gift_wrap_cost = ! empty( $_POST['_gift_wrap_cost'] ) ? woocommerce_clean( $_POST['_gift_wrap_cost'] ) : '';
update_post_meta( $post_id, '_is_gift_wrappable', $_is_gift_wrappable );
update_post_meta( $post_id, '_gift_wrap_cost', $_gift_wrap_cost );
}
/**
* admin_settings function.
*
* @access public
* @return void
*/
public function admin_settings() {
woocommerce_admin_fields( $this->settings );
}
/**
* save_admin_settings function.
*
* @access public
* @return void
*/
public function save_admin_settings() {
woocommerce_update_options( $this->settings );
}
}
新WC_产品_礼品_包装()
如果有人能帮我,我将非常感激
提前感谢,
林恩
/**
* Hook us in :)
*
* @access public
* @return void
*/
public function __construct() {
$default_message = '{checkbox} '. sprintf( __( 'Gift wrap this item for %s?', 'woocommerce-product-gift-wrap' ), '{price}' );
$this->gift_wrap_enabled = get_option( 'product_gift_wrap_enabled' ) == 'yes' ? true : false;
$this->gift_wrap_cost = get_option( 'product_gift_wrap_cost', 0 );
$this->product_gift_wrap_message = get_option( 'product_gift_wrap_message' );
if ( ! $this->product_gift_wrap_message ) {
$this->product_gift_wrap_message = $default_message;
}
add_option( 'product_gift_wrap_enabled', 'no' );
add_option( 'product_gift_wrap_cost', '0' );
add_option( 'product_gift_wrap_message', $default_message );
// Init settings
$this->settings = array(
array(
'name' => __( 'Gift Wrapping Enabled by Default?', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'Enable this to allow gift wrapping for products by default.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_enabled',
'type' => 'checkbox',
),
array(
'name' => __( 'Default Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'desc' => __( 'The cost of gift wrap unless overridden per-product.', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_cost',
'type' => 'text',
'desc_tip' => true
),
array(
'name' => __( 'Gift Wrap Message', 'woocommerce-product-gift-wrap' ),
'id' => 'product_gift_wrap_message',
'desc' => __( 'Note: <code>{checkbox}</code> will be replaced with a checkbox and <code>{price}</code> will be replaced with the gift wrap cost.', 'woocommerce-product-gift-wrap' ),
'type' => 'text',
'desc_tip' => __( 'The checkbox and label shown to the user on the frontend.', 'woocommerce-product-gift-wrap' )
),
);
// Display on the front end
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'gift_option_html' ), 10 );
// Filters for cart actions
add_filter( 'woocommerce_add_cart_item_data', array( $this, 'add_cart_item_data' ), 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item', array( $this, 'add_cart_item' ), 10, 1 );
add_action( 'woocommerce_add_order_item_meta', array( $this, 'add_order_item_meta' ), 10, 2 );
// Write Panels
add_action( 'woocommerce_product_options_pricing', array( $this, 'write_panel' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'write_panel_save' ) );
// Admin
add_action( 'woocommerce_settings_general_options_end', array( $this, 'admin_settings' ) );
add_action( 'woocommerce_update_options_general', array( $this, 'save_admin_settings' ) );
}
/**
* Show the Gift Checkbox on the frontend
*
* @access public
* @return void
*/
public function gift_option_html() {
global $post;
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( $is_wrappable == 'yes' ) {
$current_value = ! empty( $_REQUEST['gift_wrap'] ) ? 1 : 0;
$cost = get_post_meta( $post->ID, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$price_text = $cost > 0 ? woocommerce_price( $cost ) : __( 'free', 'woocommerce-product-gift-wrap' );
$checkbox = '<input type="checkbox" name="gift_wrap" value="yes" ' . checked( $current_value, 1, false ) . ' />';
woocommerce_get_template( 'gift-wrap.php', array(
'product_gift_wrap_message' => $this->product_gift_wrap_message,
'checkbox' => $checkbox,
'price_text' => $price_text
), 'woocommerce-product-gift-wrap', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
}
}
/**
* When added to cart, save any gift data
*
* @access public
* @param mixed $cart_item_meta
* @param mixed $product_id
* @return void
*/
public function add_cart_item_data( $cart_item_meta, $product_id ) {
$is_wrappable = get_post_meta( $product_id, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
if ( ! empty( $_POST['gift_wrap'] ) && $is_wrappable == 'yes' ) {
$cart_item_meta['gift_wrap'] = true;
}
return $cart_item_meta;
}
/**
* Get the gift data from the session on page load
*
* @access public
* @param mixed $cart_item
* @param mixed $values
* @return void
*/
public function get_cart_item_from_session( $cart_item, $values ) {
if ( ! empty( $values['gift_wrap'] ) ) {
$cart_item['gift_wrap'] = true;
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* Display gift data if present in the cart
*
* @access public
* @param mixed $other_data
* @param mixed $cart_item
* @return void
*/
public function get_item_data( $item_data, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) )
$item_data[] = array(
'name' => __( 'In geschenkmand', 'woocommerce-product-gift-wrap' ),
'value' => __( 'Yes', 'woocommerce-product-gift-wrap' ),
'display' => __( 'Ja', 'woocommerce-product-gift-wrap' )
);
return $item_data;
}
/**
* Adjust price after adding to cart
*
* @access public
* @param mixed $cart_item
* @return void
*/
public function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
$cost = get_post_meta( $cart_item['data']->id, '_gift_wrap_cost', true );
if ( $cost == '' ) {
$cost = $this->gift_wrap_cost;
}
$cart_item['data']->adjust_price( $cost );
}
return $cart_item;
}
/**
* After ordering, add the data to the order line items.
*
* @access public
* @param mixed $item_id
* @param mixed $values
* @return void
*/
public function add_order_item_meta( $item_id, $cart_item ) {
if ( ! empty( $cart_item['gift_wrap'] ) ) {
woocommerce_add_order_item_meta( $item_id, __( 'In geschenkmand', 'woocommerce-product-gift-wrap' ), __( 'Ja', 'woocommerce-product-gift-wrap' ) );
}
}
/**
* write_panel function.
*
* @access public
* @return void
*/
public function write_panel() {
global $post;
echo '</div><div class="options_group show_if_simple show_if_variable">';
$is_wrappable = get_post_meta( $post->ID, '_is_gift_wrappable', true );
if ( $is_wrappable == '' && $this->gift_wrap_enabled ) {
$is_wrappable = 'yes';
}
woocommerce_wp_checkbox( array(
'id' => '_is_gift_wrappable',
'wrapper_class' => '',
'value' => $is_wrappable,
'label' => __( 'Gift Wrappable', 'woocommerce-product-gift-wrap' ),
'description' => __( 'Enable this option if the customer can choose gift wrapping.', 'woocommerce-product-gift-wrap' ),
) );
woocommerce_wp_text_input( array(
'id' => '_gift_wrap_cost',
'label' => __( 'Gift Wrap Cost', 'woocommerce-product-gift-wrap' ),
'placeholder' => $this->gift_wrap_cost,
'desc_tip' => true,
'description' => __( 'Override the default cost by inputting a cost here.', 'woocommerce-product-gift-wrap' ),
) );
wc_enqueue_js( "
jQuery('input#_is_gift_wrappable').change(function(){
jQuery('._gift_wrap_cost_field').hide();
if ( jQuery('#_is_gift_wrappable').is(':checked') ) {
jQuery('._gift_wrap_cost_field').show();
}
}).change();
" );
}
/**
* write_panel_save function.
*
* @access public
* @param mixed $post_id
* @return void
*/
public function write_panel_save( $post_id ) {
$_is_gift_wrappable = ! empty( $_POST['_is_gift_wrappable'] ) ? 'yes' : 'no';
$_gift_wrap_cost = ! empty( $_POST['_gift_wrap_cost'] ) ? woocommerce_clean( $_POST['_gift_wrap_cost'] ) : '';
update_post_meta( $post_id, '_is_gift_wrappable', $_is_gift_wrappable );
update_post_meta( $post_id, '_gift_wrap_cost', $_gift_wrap_cost );
}
/**
* admin_settings function.
*
* @access public
* @return void
*/
public function admin_settings() {
woocommerce_admin_fields( $this->settings );
}
/**
* save_admin_settings function.
*
* @access public
* @return void
*/
public function save_admin_settings() {
woocommerce_update_options( $this->settings );
}