Skip to content

Commit

Permalink
Merge pull request #130 from xxl4/main_de
Browse files Browse the repository at this point in the history
syne main to package admin dev
  • Loading branch information
xxl4 authored Mar 21, 2024
2 parents db5c473 + cb097b7 commit 5255e45
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

custom: https://paypal.me/nicelizhi?country.x=C2&locale.x=zh_XC
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function schedule(Schedule $schedule)
$schedule->command('shopify:order:post')->everyFiveMinutes()->withoutOverlapping(); // post order to shopify
$schedule->command('shopify:order:get --force=true')->hourly()->withoutOverlapping()->timezone('Asia/Shanghai')->between('9:00', '19:00'); // shopify order sync
$schedule->command('shopify:order:get:shipped --force=true')->hourly()->withoutOverlapping()->timezone('Asia/Shanghai')->between('9:00', '19:00'); // shopify order sync
$schedule->command('shopify:customers:get')->everyFiveMinutes()->withoutOverlapping()->timezone('Asia/Shanghai')->between('9:00', '19:00'); // shopify customer sync
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,13 @@ public function detail($slug, Request $request) {

//获取到他底部的商品内容
// $attributes = $this->productRepository->getSuperAttributes($product);
$product_attr_sort_cache_key = "product_attr_sort_23_".$product->id;
$product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort


foreach($attributes['attributes'] as $key=>$attribute) {

$product_attr_sort_cache_key = "product_attr_sort_".$attribute['id']."_".$product->id;
$product_attr_sort = $redis->hgetall($product_attr_sort_cache_key); // get sku sort

//var_dump($attribute);
$attribute['name'] = $attribute['code'];
$options = [];
Expand All @@ -220,7 +223,7 @@ public function detail($slug, Request $request) {
$option['name'] = $option['label'];
unset($option['admin_name']);

if($attribute['id']==23 && !empty($product_attr_sort)) {
if(!empty($product_attr_sort)) {
$sort = isset($product_attr_sort[$option['id']]) ? intval($product_attr_sort[$option['id']]) : 4 ;
$option['sort'] = $sort;
$options[$sort] = $option;
Expand Down Expand Up @@ -678,6 +681,7 @@ public function confirm(Request $request) {
/**
*
* 订单状态查询
* paypal 订单生成
*
*/
public function order_status(Request $request) {
Expand Down Expand Up @@ -711,15 +715,15 @@ public function order_status(Request $request) {
$addressData['billing'] = [];
$address1 = [];
array_push($address1, $input['address']->address_line_1);
$addressData['billing']['city'] = isset($input['address']->admin_area_1) ? $input['address']->admin_area_1 : "";
$addressData['billing']['city'] = isset($input['address']->admin_area_2) ? $input['address']->admin_area_2 : "";
$addressData['billing']['country'] = $input['address']->country_code;
$addressData['billing']['email'] = $payer['email_address'];
$addressData['billing']['first_name'] = $payer['name']->given_name;
$addressData['billing']['last_name'] = $payer['name']->surname;
$national_number = isset($payment_source_paypal['phone_number']) ? $payment_source_paypal['phone_number']->national_number : "";
$addressData['billing']['phone'] = $national_number;
$addressData['billing']['postcode'] = isset($input['address']->postal_code) ? $input['address']->postal_code : "";
$addressData['billing']['state'] = isset($input['address']->admin_area_2) ? $input['address']->admin_area_2 : "";
$addressData['billing']['state'] = isset($input['address']->admin_area_1) ? $input['address']->admin_area_1 : "";
$addressData['billing']['use_for_shipping'] = true;
$addressData['billing']['address1'] = $address1;
$addressData['shipping'] = [];
Expand Down Expand Up @@ -1054,17 +1058,7 @@ private function getCartProductPrice($product, $product_id, $qty) {

$AddcartProduct['selected_configurable_option'] = $product_variant_id;
$AddcartProduct['super_attribute'] = $super_attribute;
//var_dump($super_attribute);exit;

//var_dump($AddcartProduct);exit;
// $attr_ids = explode(',', $product['attr_id']);
// foreach($attr_ids as $key=>$attr_id) {
// $attr = explode('_', $attr_id);
// $super_attribute[$attr[0]] = $attr[1];
// }

//$AddcartProduct['super_attribute'] = $super_attribute;
//var_dump($AddcartProduct);exit;

$cart = Cart::addProduct($product['product_id'], $AddcartProduct);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,7 @@ function createOrder(token, token_field="checkout_frames_token", pay_type="check
}
function getPhonePrefix() {
return '';
var country = $("#country-select").val();
for(var i=0; i<window.countries.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,7 @@ function createOrder(token, token_field="checkout_frames_token", pay_type="check
}
function getPhonePrefix() {
return '';
var country = $("#country-select").val();
for(var i=0; i<window.countries.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,7 @@ function createOrder(token, token_field="checkout_frames_token", pay_type="check
}
function getPhonePrefix() {
return '';
var country = $("#country-select").val();
for(var i=0; i<window.countries.length; i++) {
Expand Down
214 changes: 214 additions & 0 deletions packages/Nicelizhi/Shopify/src/Console/Commands/Customers/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?php

namespace Nicelizhi\Shopify\Console\Commands\Customers;

use Illuminate\Console\Command;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Sales\Repositories\OrderCommentRepository;
use Webkul\Admin\DataGrids\Sales\OrderDataGrid;
use Webkul\Sales\Repositories\ShipmentRepository;
use Webkul\Sales\Repositories\OrderItemRepository;
use Illuminate\Support\Facades\Cache;


use Nicelizhi\Shopify\Models\ShopifyOrder;
use Nicelizhi\Shopify\Models\ShopifyStore;


class Get extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'shopify:customers:get {--shopify_store_id=} {--force=} {--ids=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Get Customers List';

private $lang = null;
private $shopify_store_id = null;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct(
protected OrderRepository $orderRepository,
protected ShopifyOrder $ShopifyOrder,
protected ShopifyStore $ShopifyStore,
protected ShipmentRepository $shipmentRepository,
protected OrderItemRepository $orderItemRepository,
protected OrderCommentRepository $orderCommentRepository
)
{
$this->shopify_store_id = "wmshoe";
$this->shopify_store_id = "hatmeo";
$this->shopify_store_id = config('shopify.shopify_store_id');
$this->lang = config('shopify.store_lang');
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$shopifyStore = Cache::get("shopify_store_".$this->shopify_store_id);

if(empty($shopifyStore)){
$shopifyStore = $this->ShopifyStore->where('shopify_store_id', $this->shopify_store_id)->first();
Cache::put("shopify_store_".$this->shopify_store_id, $shopifyStore, 3600);
}
if(is_null($shopifyStore)) {
$this->error("no store");
return false;
}
$shopify = $shopifyStore->toArray();

$client = new Client();

$force = $this->option('force');

if($force==true) {
Cache::put("shopify_day", 1);
}

// $created_at_min = date("c", strtotime("-3 days"));
// $created_at_max = date("c");

$day = Cache::get("shopify_day");

if(empty($day)) $day = 0;

$day = $day + 1;

$start = $day - 1;
$end = $day - 2;


$created_at_min = date("c", strtotime("-".$start." days"));
$created_at_max = date("c", strtotime("-".$end." days"));

$created_at_min = date("c", strtotime("-3 days"));
$created_at_max = date("c");

$this->info("processed at min ". $created_at_min);
$this->info("processed at max ". $created_at_max);
//exit;




// @https://shopify.dev/docs/api/admin-rest/2023-10/resources/customer#get-customers?ids=207119551,1073339482
$base_url = $shopify['shopify_app_host_name'].'/admin/api/2023-10/customers.json?created_at_min='.$created_at_min.'&created_at_max='.$created_at_max.'&limit=250';
//$base_url = $shopify['shopify_app_host_name'].'/admin/api/2023-10/customers.json?limit=2&ids=7927054762214'; // for test
$this->error("base url ". $base_url);
$response = $client->get($base_url, [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Shopify-Access-Token' => $shopify['shopify_admin_access_token'],
]
]);

$body = $response->getBody();
$body = json_decode($body, true);
$i = 1;
foreach($body['customers'] as $customer) {
$this->error($i."---");

$i++;
//var_dump($customer);
$this->info($customer['id']." start");
if(!isset($customer['email_marketing_consent']['state'])) continue; //when user don't use email
if($customer['email_marketing_consent']['state'] == "not_subscribed") { //
$this->error($customer['email']);


$cusPut = [];
$email_marketing_consent['state'] = "subscribed";
$email_marketing_consent['opt_in_level'] = "confirmed_opt_in";
$email_marketing_consent['consent_updated_at'] = date("c");
$cusPut['customer']['email_marketing_consent'] = $email_marketing_consent;
$base_url = $shopify['shopify_app_host_name'].'/admin/api/2023-10/customers/'.$customer['id'].'.json';
$this->info($base_url);

//var_dump($cusPut);exit;

try {
$response = $client->put($base_url, [
'http_errors' => true,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Shopify-Access-Token' => $shopify['shopify_admin_access_token'],
],
'body' => json_encode($cusPut)
]);

//var_dump($response->getBody(), $cusPut);

}catch(ClientException $e) {
//var_dump($e);
//var_dump($e->getMessage());exit;
Log::error(json_encode($e->getMessage()));
\Nicelizhi\Shopify\Helpers\Utils::send($e->getMessage().'--' .$id. " 需要手动解决 ");
//continue;
return false;
}



sleep(1);
}


$cus = \Nicelizhi\Shopify\Models\ShopifyCustomer::where("email", $customer['email'])->first();
if(is_null($cus)) $cus = new \Nicelizhi\Shopify\Models\ShopifyCustomer();
$cus->email = $customer['email'];
$cus->cus_id = $customer['id'];
$cus->created_at = $customer['created_at'];
$cus->updated_at = $customer['updated_at'];
$cus->first_name = isset($customer['first_name']) ? $customer['first_name'] : "";
$cus->last_name = isset($customer['last_name']) ? $customer['last_name'] : "";
$cus->orders_count = $customer['orders_count'];
$cus->state = $customer['state'];
$cus->total_spent = $customer['total_spent'];
$cus->last_order_id = isset($customer['last_order_id']) ? $customer['last_order_id'] : "";
$cus->note = $customer['note'];
$cus->verified_email = $customer['verified_email'];
$cus->multipass_identifier = $customer['multipass_identifier'];
$cus->tax_exempt = $customer['tax_exempt'];
$cus->tags = $customer['tags'];
$cus->currency = $customer['currency'];
$cus->phone = isset($customer['phone']) ? $customer['phone'] : "" ;
$cus->addresses = $customer['addresses'];
$cus->accepts_marketing = $customer['accepts_marketing'];
$cus->accepts_marketing_updated_at = isset($customer['accepts_marketing_updated_at']) ? $customer['accepts_marketing_updated_at'] : "";
$cus->marketing_opt_in_level = $customer['marketing_opt_in_level'];
$cus->tax_exemptions = $customer['tax_exemptions'];
$cus->email_marketing_consent = $customer['email_marketing_consent'];
$cus->sms_marketing_consent = isset($customer['sms_marketing_consent']) ? $customer['sms_marketing_consent'] : [];
$cus->admin_graphql_api_id = $customer['admin_graphql_api_id'];
$cus->default_address = isset($customer['default_address']) ? $customer['default_address'] : [] ;
$cus->save();


}

Cache::put("shopify_day", $day);

}
}
Loading

0 comments on commit 5255e45

Please sign in to comment.