在主网络站点(blog_id = 1)上显示带有WooCommerce商店(blog_id = 2)的WordPress网络子站点中的“现货”产品

时间:2019-11-18 13:22:32

标签: wordpress woocommerce share multisite

我们正在设置WordPress多站点/网络安装,以替换多个单个WordPress实例,这使得在相关站点之间共享数据变得困难。我们将站点的不同区域隔离开来,因此我们可以为每个部分使用不同的主题。

设置为/是:

  • 主站点,其中包含有关威士忌俱乐部的酒桶共享计划的一般信息,其中包括有关我们拥有酒桶的所有酒厂的概述
    • 在这里我们要添加所有在商店中仍为“有货”状态的桶共享程序的概述
    • 在这里我们没有激活WooCommerce插件,我们只想在这里购买一些库存产品,显示的产品将必须链接到子站点1上正确的商店页面。
  • 子站点1 仅WooCommerce商店,俱乐部会员可以在其中购买桶计划中的份额
    • 在这里我们激活了WooCommerce插件
  • 子站点2 我们的论坛

我一直在编写一个短代码,以使产品在主站点上可见,但是它什么也没显示。在网上搜索解决方案两天后,我有点被卡住了。

这是我目前拥有的:

// 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()正常运行?

0 个答案:

没有答案