如何添加将 WC_Product_Variable 扩展到购物车的自定义 WooCommerce 产品类型 [英] How to add a custom WooCommerce product type that extends WC_Product_Variable to the cart

查看:13
本文介绍了如何添加将 WC_Product_Variable 扩展到购物车的自定义 WooCommerce 产品类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基于内置变量产品类型的自定义 WooCommerce 产品类型.我希望拥有特定于我的产品类型的产品变体和自定义特征和字段.这一切都在管理和我的自定义前端产品页面模板中正常工作和显示.

I've created a custom WooCommerce product type that's based on the built-in variable product type. I want to have both product variations and custom characteristics and fields specific to my product type. This is all working and displaying fine in the admin and my custom front-end product page template.

这是我的定制产品,供参考:

Here's my custom product, for reference:

add_action( 'init', 'register_book_type' );
function register_book_type () {
    class WC_Product_Book extends WC_Product_Variable {
        public function __construct( $product ) {
            parent::__construct( $product );
        }

        public function get_type() {
            return 'book';
        }
    }
}

但是,当我将带有变体的产品发布到购物车时,由于 add_to_cart_action() 中的代码 $adding_to_cart->get_type() 导致行为混乱(在 WC_Form_Handler 类中)将产品标识为我的自定义书"类型,而不是将其视为可变"产品,而是默认将其视为简单"产品类型.

However, when I post the product with its variation to the cart, the behavior messes up because the code $adding_to_cart->get_type() in add_to_cart_action() (in the WC_Form_Handler class) is identifying the product as my custom "book" type and not treating it as a "variable" product and falling back to treating it as a "simple" product type by default.

这是给我带来麻烦的内置 WooCommerce 区域:

Here's that built-in WooCommerce area that's causing me trouble:

$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart );
if ( 'variable' === $add_to_cart_handler || 'variation' === $add_to_cart_handler ) {
    $was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
} elseif ( 'grouped' === $add_to_cart_handler ) {
    $was_added_to_cart = self::add_to_cart_handler_grouped( $product_id );
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) {
    do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Custom handler.
} else {
    $was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
}

问题似乎是我自己的 get_type() 方法在此代码期望变量"时返回book".我需要它返回book",以便产品编辑页面正确识别类型.

The problem seems to be that my own get_type() method returns "book" when this code is expecting "variable." I need it to return "book" so the product edit page will recognize the type properly.

我知道我可以在自己的代码中删除和替换 add_to_cart_action() 函数来覆盖此行为并添加我的自定义类型,但随后我无法调用其中的所有其他私有方法WC_Form_Handler 类.或者可以覆盖整个类..?

I know I can remove and replace the add_to_cart_action() function in my own code to override this behavior and add my custom type, but then I'm unable to call all the other private methods in the WC_Form_Handler class. Or is it okay to just override that entire class..?

还有其他方法可以绕过这一切,将我的基于变量的定制产品放入购物车吗?

Any other way to bypass all this to get my variable-based custom product into the shopping cart?

推荐答案

我是这样解决这个问题的:

This is how I solved this issue:

public function get_type()
{
    return get_current_screen()->id === 'product' ? 'book' : parent::get_type();
}

这将在编辑产品页面上返回book,但在其他地方返回variable.

This will return book on the edit product page however return variable everywhere else.

这篇关于如何添加将 WC_Product_Variable 扩展到购物车的自定义 WooCommerce 产品类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆