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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WooCommerce自定义产品类型 – 多个添加到购物车部分问题脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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');
}

关键部分是这个动作add_action(‘woocommerce_booking_add_to_cart’,’booking_add_to_cart’);

这会将添加到购物车按钮放在正确的位置 – 在您自己的自定义产品中使用它会产生一个名为woocommerce_YOURPRODUCTTYPE_add_to_cart的操作

解决方法

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

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

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

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

使用此自定义代码,我不像您那样多个添加到购物车部分.

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

/**
 * 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','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

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自定义产品类型 – 多个添加到购物车部分问题全部内容,希望文章能够帮你解决php – WooCommerce自定义产品类型 – 多个添加到购物车部分问题所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。