wordpress 中的 woocommerce 返回总是像产品类型一样简单 [英] woocommerce in wordpress return always simple as product type

查看:25
本文介绍了wordpress 中的 woocommerce 返回总是像产品类型一样简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取分组产品的类型,但如果我使用 WC_Product_Factory,woocommerce 返回空或总是简单".

I try of get the type of a grouped product but woocommerce returns empty or always "simple" if I use WC_Product_Factory.

当我使用时:

$the_product = new WC_Product(2886);
echo $the_product->product_type;

返回空.

当我使用 WC_Product_Factory 时:

When I use WC_Product_Factory:

$the_product = new WC_Product(2886);
$the_product_factory = new WC_Product_Factory();
$the_product = $the_product_factory->get_product($the_product);
echo $the_product->product_type;

总是返回简单"

我尝试使用:

$the_product = wc_get_product(2886);
echo $the_product->product_type;

但这会返回错误调用...中的非对象上的成员函数 get_product()"

but this returns an error "Call to a member function get_product() on a non-object in..."

我的代码在:

add_action( 'plugins_loaded', function(){
    $the_product = new WC_Product(2886);
    echo $the_product->product_type;

    $the_product = new WC_Product(2886);
    $the_product_factory = new WC_Product_Factory();
    $the_product = $the_product_factory->get_product($the_product);
    echo $the_product->product_type;

    $the_product = wc_get_product(2886);
    echo $the_product->product_type;

    die();
});

好的,谢谢你的帮助!

推荐答案

我找到了解决方案或我的错误,将 plugins_loaded 更改为 init:

I found the solution or my mistake, change plugins_loaded to init:

add_action( 'init', function(){
    $the_product = new WC_Product(2886);
    echo $the_product->product_type;

    $the_product = new WC_Product(2886);
    $the_product_factory = new WC_Product_Factory();
    $the_product = $the_product_factory->get_product($the_product);
    echo $the_product->product_type;

    $the_product = wc_get_product(2886);
    echo $the_product->product_type;
    die();
});

还有这项工作!

这个网址 https://wordpress.stackexchange.com/questions/120055/woocommerce-create-new-product-and-add-to-cart-on-form-submit 给了我这个想法.

This url https://wordpress.stackexchange.com/questions/120055/woocommerce-create-new-product-and-add-to-cart-on-form-submit gave me the idea.

快乐编码!:)

这篇关于wordpress 中的 woocommerce 返回总是像产品类型一样简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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