• Resolved Steven

    (@stgoos)


    Howdy, I’ve setup a WordPress MultiSite network. On the main site / blog I’ve general info about our club. On the first subsite (blog_id = 2) we’ve our WooCommerce shop for some club merchandise.

    Is there a way I can show all ‘in stock’ products on the main site (blog_id = 1) so I can click there to be redirected to the product page within the shop?

    Preferably by using the build-in WooCommerce shortcodes and by not having to activate the WooCommerce plugin on the main site (I don’t want all those extra db tables and menu items).

    Is this possible with a custom shortcode in the main site with the use of switch_to_blog() and a do_shortcode() that calls for a build-in WC shortcode?

    If so, do I need to load the WooCommerce functions somehow?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Zach W

    (@dynamiczach)

    Automattic Happiness Engineer

    Howdy!

    I’m not aware of a way to show the shortcodes on the main site without activating WooCommerce, but if you decide to go that route, this option should work for you:

    https://github.com/WPprodigy/woocommerce-external-product-embed

    Thread Starter Steven

    (@stgoos)

    Thanks @dynamiczach, will have a look at it tonight.

    In the meantime I’ve also started to look into the WooCommerce REST API v3 but it breaks on the first use line :/

    <?php
    
    function shortcode_wooproducts_REST_API( $args = [] )
    {
        // Normalize attribute keys, lowercase
        $args = array_change_key_case( (array)$args, CASE_LOWER );
    
        // Merge supplied $args with default $args (override defaults) 
        $args = shortcode_atts([
          'status' => 'publish',
          'stock_status' => 'instock',
        ], $args);
    
        ## https://docs.woocommerce.com/document/woocommerce-rest-api/
        ## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#rest-api-keys
        ## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#authentication-over-https
        
        require_once( WP_PLUGIN_DIR ."/woocommerce/vendor/autoload.php" ); //require_once( __DIR__ . '/vendor/autoload.php' );
        use Automattic\WooCommerce\Client;
        
        $woocommerce = new Client(
            'https://domain.tld/blog2',
            'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // consumer key
            'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // consumer secret
            [
                'wp_api' => true,
                'version' => 'wc/v3',
                'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
            ]
        );
        
        ## https://packagist.org/packages/automattic/woocommerce#3.0.0
        ## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products
        
        use Automattic\WooCommerce\HttpClient\HttpClientException;
        
        try {
            $results = $woocommerce->get( 'products', $args );
            echo '<pre><code>' . print_r( $results, true ) . '</code><pre>'; // JSON output
        } catch (HttpClientException $e) {
            echo '<pre><code>' . print_r( $e->getMessage(), true ) . '</code><pre>'; // Error message.
            echo '<pre><code>' . print_r( $e->getRequest(), true ) . '</code><pre>'; // Last request data.
            echo '<pre><code>' . print_r( $e->getResponse(), true ) . '</code><pre>'; // Last response data.
        }    
    }
    
    function shortcode_wooproducts_REST_API_init()
    {
        ## https://stackoverflow.com/questions/57372608/shortcode-doesnt-work-when-im-getting-content-from-wp-rest-api-call
    
        add_shortcode('wooproducts_REST_API', 'shortcode_wooproducts_REST_API');
    }
    add_action('init', 'shortcode_wooproducts_REST_API_init');

    Error message:

    PHP Parse error: syntax error, unexpected 'use' (T_USE) in /home/***/domain.tld/.../wp-content/themes/child/functions.php on line 19\

    • This reply was modified 5 years ago by Steven.
    Zach W

    (@dynamiczach)

    Automattic Happiness Engineer

    Nice!

    That’s not something that I’m equipped to help with, but I’ll leave this thread open in the event that someone else can.

    If you get stuck and don’t have any other replies here, there is an Advanced WooCommerce Facebook Group here: https://www.facebook.com/groups/advanced.woocommerce/ and our WooCommerce Slack Community here: https://woocommerceslack.herokuapp.com/ and someone in one of those may be able to help.

    Thread Starter Steven

    (@stgoos)

    We can shake hands, it’s not my cup of tea either. In fact – it’s my first time dealing with a REST API ever ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show products overview on another multisite blog (without WooCommerce activated)’ is closed to new replies.