Last active
June 24, 2020 08:08
-
-
Save carrowheap/3873a2e24f882addb080668fad1da859 to your computer and use it in GitHub Desktop.
woocomerce_create_and_send_variations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Controller; | |
| use Automattic\WooCommerce\Client; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\Routing\Annotation\Route; | |
| class DefaultController extends AbstractController | |
| { | |
| public function index() | |
| { | |
| $client = new Client( | |
| 'http://172.22.0.1:8090', | |
| 'ck_4e2802c67793d32dba3237f994dbbca45577e2d5', | |
| 'cs_796a3ab9ec3fb6bf6482280f3fcfbaa90a71a55c', | |
| [ | |
| 'wp_api' => true, | |
| 'version' => 'wc/v3', | |
| 'query_string_auth' => true, | |
| 'verify_ssl' => false, | |
| 'timeout' => 400, // curl timeout | |
| ] | |
| ); | |
| // product to create | |
| $product = array( | |
| 'name' => 'MAC PRO BOOK ZEND X MAN', | |
| 'regular_price' => '537.90', | |
| 'attributes' => array( | |
| array( | |
| 'name' => 'Color', // parameter for custom attributes | |
| 'visible' => true, // default: false | |
| 'variation' => true, | |
| 'options' => array( | |
| 'RED', | |
| 'BLUE' | |
| ) | |
| ))); | |
| // variant to send | |
| $data = [ | |
| 'regular_price' => '7.00', | |
| 'manage_stock' => true, | |
| 'stock_quantity' => 56, | |
| 'status' => 'publish', | |
| 'attributes' => [ | |
| [ | |
| 'name' => 'Color', | |
| 'option' => 'RED' | |
| ] | |
| ], | |
| ]; | |
| // post product | |
| $resp = $client->post('products', $product); | |
| // post product variations | |
| $data = $client->post('products/' . $resp->id . '/variations', $data); | |
| return $this->render('default/index.html.twig', [ | |
| 'controller_name' => 'DefaultController', | |
| ]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment