gpt4 book ai didi

php - WooCommerce 自定义产品类型 - 多个添加到购物车部分问题

转载 作者:可可西里 更新时间:2023-11-01 00:19:12 26 4
gpt4 key购买 nike

我在 woocommerce 中创建了一个名为 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 次,我不明白为什么。

我该如何解决这个问题?

谢谢


@Update … 解决方案 (因为 LoicTheAztec 让我走上正轨):

我已经找到了解决这个问题的方法,使用这段代码

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');
}

关键部分是这个 Action add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');

将添加到购物车按钮放在正确的位置 - 要在您自己的自定义产品中使用它,请执行名为 woocommerce_YOURPRODUCTTYPE_add_to_cart

最佳答案

试图在测试服务器上测试您的代码,但您的预订产品未在后端显示。

您的问题可能来自这里:我认为您正在使用 public function add_to_cart() 一个现有的名称函数,您必须以不同的方式重命名它。

然后我重新访问了你的代码,based on this thread .

扩展 WC_Product_Simple可能比 WC_Product 类更好,因为您的产品使用简单的产品添加到购物车按钮模板:
single-product/add-to-cart/simple.php

有了这个自定义代码,我就不会像你一样面对多次添加到购物车部分。

— @Update1 —
But you can still keep WC_Product extended instead of WC_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).

So I have modified the code again and tested: It works…

这是我使用的代码(并且我已经测试过):

/**
* 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 文件上测试了此代码。

引用:Adding a custom WooCommerce product type


— @Update2 — The solution (find by the author of this question):

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');
}

关于php - WooCommerce 自定义产品类型 - 多个添加到购物车部分问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com