我们正在设置WordPress多站点/网络安装,以替换多个单个WordPress实例,这使得在相关站点之间共享数据变得困难。我们将站点的不同区域隔离开来,因此我们可以为每个部分使用不同的主题。
设置为/是:
我一直在编写一个短代码,以使产品在主站点上可见,但是它什么也没显示。在网上搜索解决方案两天后,我有点被卡住了。
这是我目前拥有的:
// Goal: to show WooCommerce products, in catalog mode, on the main site of a multisite network.
// Info: - the actual WooCommerce shop is located on the first subsite in the multisite network, so: blog_id = 2
// - as I want to make as much as possible use of build-in WooCommerce functions, the plugins is loaded
// manually as I don't want the WooCommerce plugin to be activated on the main site
// Shortcode: [wooproducts]
function shortcode_wooproducts( $atts = [] ) {
// See if WooCommerce is available in plugins folder, we'll need it
// BUT... does this really make the WooCommerce functions / shortcodes available?!?
if( file_exists( WP_PLUGIN_DIR ."/woocommerce/woocommerce.php" ) ){
require_once( WP_PLUGIN_DIR ."/woocommerce/woocommerce.php" );
} else {
return "WooCommerce plugin can't be found!";
}
// Normalize attribute keys, lowercase
$atts = array_change_key_case( (array)$atts, CASE_LOWER );
// Default attributes and overwrite defaults with supplied attributes in shortcode
$atts = shortcode_atts([
'blog_id' => 2,
'status' => 'publish',
'stock_status' => 'instock'
], $atts );
// Switch to site with WooCommerce shop
switch_to_blog( $atts['blog_id'] );
// This should get me WooCommerce product info... right?!
$products = wc_get_products( $atts );
var_dump( $products ); // let's do a var_dump to see if we get anything at all
// Return to current site
restore_current_blog();
return true; // to be used later on to return the html
}
function shortcode_wooproducts_init() {
add_shortcode('wooproducts', 'shortcode_wooproducts');
}
add_action('init', 'shortcode_wooproducts_init');
使用自定义WP_Query
而不是wc_get_products()
,我设法从“ in_stock”桶共享程序获取了永久链接,因此似乎切换到子站点1(blog_id = 2)的其他博客/数据库。发挥作用。当然,WP_Query
不需要WooCommerce代码,因此我必须查看代码的这一部分。
我的问题是:在没有激活WooCommerce插件的情况下,如何在主站点上wc_get_products()
正常运行?