WooCommerce自定义产品类型-多个添加到购物车部分的问题 [英] WooCommerce Custom product type - Multiple add to cart sections issue
问题描述
我在woocommerce中创建了一个名为Booking的自定义产品类型.
I've created a custom product type in woocommerce called Booking.
这是我拥有的代码:
class WC_Product_Booking extends WC_Product{
/**
* __construct function.
*
* @access public
* @param mixed $product
*/
public function __construct( $product ) {
$this->product_type = 'booking';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
add_action('woocommerce_booking_add_to_cart', array($this, 'add_to_cart'),30);
}
private function show_pricing_fields(){
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_booking' ).show();
});
</script><?php
}
/**
* Display the add to cart button (same as for simple product)
*/
public function add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
}
现在,唯一的问题是产品页面上的添加到购物车"部分显示了6次,我不知道为什么.
Right now the only problem is that the add to cart section on the product page is displayed 6 times andI cant figure out why.
我该如何解决这个问题?
How can I solve this issue?
谢谢
@Update…解决方案 (如 LoicTheAztec 使我走上正轨):
@Update … Solution (as LoicTheAztec put me on the right track):
我已经找到了使用此代码的解决方案
I've found a solution to this problem, using this code
if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {
/**
* Output the simple product add to cart area.
*
* @subpackage Product
*/
function booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
}
关键是此操作 add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
The crucial part is this action add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
将添加到购物车"按钮一次放在正确的位置-要在您自己的自定义产品中使用该按钮,请执行名为 woocommerce_YOURPRODUCTTYPE_add_to_cart
Which puts the add to cart button once in the right place - to use it in your own custom product make an action called woocommerce_YOURPRODUCTTYPE_add_to_cart
推荐答案
试图在测试服务器上测试您的代码,但您的预订产品未显示在后端.
Tried to test your code on a test server, but your booking product was not showing on backend.
您的问题可能来自这里:我认为您正在使用public function add_to_cart()
现有的名称功能,因此必须对其进行不同的重命名.
Your issue could come from here: I think you are using with public function add_to_cart()
an existing name function, and you have to rename-it differently.
然后,我基于此线程重新访问了您的代码.
Then I have revisited your code, based on this thread.
扩展WC_Product_Simple
类可能比 WC_Product
类更好,因为您的产品使用简单的产品添加到购物车按钮模板:
single-product/add-to-cart/simple.php
.
使用此自定义代码,我不会像您一样面对多个添加到购物车部分.
With this custom code I don't face like you Multiple add to cart sections.
— @ Update1 —
但是您仍然可以保持WC_Product
而不是WC_Product_Simple
的扩展名.这样,根据您的评论要求,您的产品类型将为"simple_booking"
,如第一功能和第二功能中所指定.这并不意味着您的产品很简单(这只是您可以更改的标语,您可以在两个地方同时更改.)
— @Update1 —
But you can still keepWC_Product
extended instead ofWC_Product_Simple
. This way, as you requested in comment, your product type will be"simple_booking"
as specified in 1st and 2nd functions. That does not mean that your product is simple (it's just a slug that you can change, changing at the same time in both places).
因此,我再次修改了代码并进行了测试:它可以正常工作……
这是我使用的代码(并且我已经测试过):
/**
* Register the custom product type after init
*/
function register_simple_booking_product_type() {
/**
* This should be in its own separate file.
*/
class WC_Product_Booking extends WC_Product{ // <= changed back to WC_product class
public function __construct( $product ) {
$this->product_type = 'simple_booking';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
// As we extend simple product class, you may not need this anymore.
add_action('woocommerce_booking_add_to_cart', array($this, 'simple_booking_add_to_cart'),30);
}
}
}
add_action( 'init', 'register_simple_booking_product_type' );
// Registering the slug name (YOU can CHANGE IT)
function add_simple_booking_product( $types ){
// Key should be exactly the same as in the class product_type parameter
$types[ 'simple_booking' ] = __( 'Simple booking' );
return $types;
}
add_filter( 'product_type_selector', 'add_simple_booking_product' );
/**
* Show pricing fields for simple_booking product.
*/
function simple_booking_custom_js() {
if ( 'product' != get_post_type() ) :
return;
endif;
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_simple_booking' ).show();
});
</script><?php
}
add_action( 'admin_footer', 'simple_booking_custom_js' );
// Not sure you will need that (because of the change of the extended class)
function simple_booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
在活动子主题(或主题)中的function.php文件上测试了此代码.
Tested this code on function.php file located in active child theme (or theme).
— @ Update2 —解决方案 (由该问题的作者查找):
要将添加到购物车按钮放置在正确的位置一次(要在您自己的自定义产品中使用该按钮,请执行名为 woocommerce_YOURPRODUCTTYPE_add_to_cart
的操作),这段代码:
To puts the add-to-cart button once in the right place (to use it in your own custom product make an action called woocommerce_YOURPRODUCTTYPE_add_to_cart
), with this code:
if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {
/**
* Output the simple product add to cart area.
*
* @subpackage Product
*/
function booking_add_to_cart() {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');
}
这篇关于WooCommerce自定义产品类型-多个添加到购物车部分的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!