Skip to content

Commit

Permalink
#165 shopify add checkout url
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Apr 12, 2024
1 parent 0b58b52 commit 25a2869
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function registerCommands(): void
\Nicelizhi\OneBuy\Console\Commands\Countries\Get::class,
\Nicelizhi\OneBuy\Console\Commands\Countries\StateCopy::class,
\Nicelizhi\OneBuy\Console\Commands\Order\CartToOrder::class,
\Nicelizhi\OneBuy\Console\Commands\Paypal\OrderGet::class,
\Nicelizhi\OneBuy\Console\Commands\Imports\ImportFaq::class,
\Nicelizhi\OneBuy\Console\Commands\Imports\ImportProductComments::class,
]);
Expand Down
44 changes: 41 additions & 3 deletions packages/Nicelizhi/Shopify/src/Console/Commands/Product/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Webkul\Product\Models\ProductImage;
use Nicelizhi\Shopify\Models\ShopifyStore;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class Get extends Command
{
Expand Down Expand Up @@ -151,16 +152,31 @@ public function handle()

}

/**
*
*
* Clear the cache for this product
*
* @param int $pid
*
*
*/
protected function clearCache($pid) {
Cache::pull("product_color_size_".$pid);
Cache::pull("product_attributes_".$pid);
Cache::pull("product_sku_size_".$pid);
Cache::pull("product_sku_".$pid);
Cache::pull("product_sku_".$pid);
}

protected function Permissions() {
$local_image_path = "storage/product/";
$path = public_path($local_image_path);
$execPath = "chown www:www ". $path."* -R";
$this->error($execPath);
//exit;
exec($execPath);
}


}

/**
*
Expand All @@ -180,6 +196,8 @@ public function syncProductToLocal($shopify_pro_id) {
// ba_attribute_options
// ba_attribute_option_translations

$redis = Redis::connection('default');

$images_map = [];

$options = $item->options;
Expand All @@ -204,6 +222,7 @@ public function syncProductToLocal($shopify_pro_id) {
$color = [];
$size = [];
$error = 0;
$LocalOptions = [];
foreach($options as $kk => $option) {
$option['name'] = strtolower($option['name']);
$attr_id = 0;
Expand Down Expand Up @@ -264,11 +283,16 @@ public function syncProductToLocal($shopify_pro_id) {
}
if($attr_id==23) $color[$attribute_option_id] = $attribute_option_id; //array_push($color, $attribute_option_id);
if($attr_id==24) $size[$attribute_option_id] = $attribute_option_id;

$LocalOptions[$attr_id][] = $attribute_option_id;
}

}

if($error==1) continue;

var_dump($LocalOptions);



// add product
Expand All @@ -295,6 +319,20 @@ public function syncProductToLocal($shopify_pro_id) {
}else{
$id = $product->id;
}

$this->clearCache($id);

// update the sku sort
foreach($LocalOptions as $key=>$LocalOption) {
$cache_key = "product_attr_sort_".$key."_".$id;
echo $cache_key."\r\n";
foreach($LocalOption as $k => $localOpt) {
$redis->hSet($cache_key, $localOpt, $k);
}
//$redis->hSet($this->cache_key.$this->prod_id, $key, json_encode($value));
}

//exit;

//var_dump($product);exit;

Expand Down
133 changes: 132 additions & 1 deletion packages/Nicelizhi/Shopify/src/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Webkul\Attribute\Models\AttributeOption;
use Webkul\Product\Repositories\ProductRepository;
use Illuminate\Support\Facades\Artisan;
use Nicelizhi\Shopify\Models\ShopifyStore;
use Illuminate\Support\Facades\Cache;

class ProductController extends Controller
{


public function __construct(
//protected ProductRepository $productRepository,
protected ShopifyStore $ShopifyStore,
protected ShopifyProduct $ShopifyProduct

){
Expand All @@ -28,6 +31,13 @@ public function __construct(
*/
public function index()
{
$shopify_store_id = config('shopify.shopify_store_id');
$shopifyStore = Cache::get("shopify_store_".$shopify_store_id);

if(empty($shopifyStore)){
$shopifyStore = $this->ShopifyStore->where('shopify_store_id', $shopify_store_id)->first();
Cache::put("shopify_store_".$shopify_store_id, $shopifyStore, 3600);
}

if (request()->ajax()) {
$table_pre = config("database.connections.mysql.prefix");
Expand All @@ -42,6 +52,9 @@ public function index()
return $d;
} ),
array( 'db' => '`p`.`title`', 'dt' => 'title', 'field'=>'title' ),
array( 'db' => '`p`.`handle`', 'dt' => 'handle', 'field'=>'handle', 'formatter' => function($d, $row){
return "/products/".$d;
}),
array( 'db' => '`p`.`status`', 'dt' => 'status', 'field'=>'status' ),
array( 'db' => '`p`.`updated_at`', 'dt' => 'updated_at', 'field'=>'updated_at' )
);
Expand All @@ -56,7 +69,7 @@ public function index()
return json_encode(SSP::simple( request()->input(), $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraCondition ));
}

return view('shopify::products.index');
return view('shopify::products.index', compact('shopifyStore'));
}

public function sync($product_id) {
Expand Down Expand Up @@ -176,4 +189,122 @@ public function sync($product_id) {
//Artisan::call("shopify:product:getv3", ["--prod_id"=> $product_id]);
}
}

public function checkoutUrlGet($product_id) {
$item = \Nicelizhi\Shopify\Models\ShopifyProduct::where("product_id", $product_id)->first();

if(is_null($item)) {
return false;
}

$options = $item->options;
//var_dump($options);
$color = [];
$size = [];
foreach($options as $kk => $option) {
$option['name'] = strtolower($option['name']);
echo $option['name']."\r\n";
$attr_id = 0;
if(strpos($option['name'], "Size")!==false) $attr_id = 24;
if(strpos($option['name'], "size")!==false) $attr_id = 24;
if(strpos($option['name'], "GRÖSSE")!==false) $attr_id = 24;
if(strpos($option['name'], "grÖsse")!==false) $attr_id = 24;
if(strpos($option['name'], "尺码") !==false) $attr_id = 24;
if(strpos($option['name'], "Length") !==false) $attr_id = 24;
if(strpos($option['name'], "größe") !==false) $attr_id = 24;
if(strpos($option['name'], "größe") !==false) $attr_id = 24;
if(strpos($option['name'], "Color") !==false) $attr_id = 23;
if(strpos($option['name'], "color") !==false) $attr_id = 23;
if(strpos($option['name'], "Couleur") !==false) $attr_id = 23;
if(strpos($option['name'], "颜色") !==false) $attr_id = 23;
if(strpos($option['name'], "FARBE") !==false) $attr_id = 23;
if(strpos($option['name'], "farbe") !==false) $attr_id = 23;

echo $attr_id."\r\n";

if(empty($attr_id)) {
$error = 1;
continue;
//exit;
}

$values = $option['values'];
$version = null;
foreach($values as $kky => $value) {
$attr_option = AttributeOption::where("attribute_id", $attr_id)->where("admin_name", $value)->first();
if(is_null($attr_option)) {
$attr_option = new AttributeOption();
$attr_option->attribute_id = $attr_id;
$attr_option->admin_name = $value;
$attr_option->save();
$attribute_option_id = $attr_option->id;
}else{
$attribute_option_id = $attr_option->id;
}

$locales = core()->getAllLocales()->pluck('code')->toArray();

//var_dump($attr_opt_tran);exit;
foreach($locales as $kl => $locale) {
$attr_opt_tran = AttributeOptionTranslation::where("attribute_option_id", $attribute_option_id)->where("locale", $locale)->first();
if(is_null($attr_opt_tran)) {
$attr_opt_tran = new AttributeOptionTranslation();
if($locale==$this->lang) {
$attr_opt_tran->label = $value;
} else{
$attr_opt_tran->label = "";
}
$attr_opt_tran->locale = $locale;
$attr_opt_tran->attribute_option_id = $attribute_option_id;
$attr_opt_tran->save();
}
}
if($attr_id==23) $color[$attribute_option_id] = $attribute_option_id; //array_push($color, $attribute_option_id);
if($attr_id==24) $size[$attribute_option_id] = $attribute_option_id;
}

// two attr
if(!empty($color) && !empty($size)) {
$version = "v1";
}

// one attr
if(!empty($color) || !empty($size)) {
if(empty($version)) $version = "v2";

}

// zero attr
if(empty($color) && empty($size)) {
if(empty($version)) $version = "v3";
}



}

// two attr
if($version=='v1') {
//Artisan::call("shopify:product:get", ["--prod_id"=> $product_id]);
//echo config("app.url")."/onebuy/".$product_id."\r\n";
return redirect(config("app.url")."/onebuy/".$product_id."?time=".time());

}

// one attr
if($version=='v2') {
//Artisan::call("shopify:product:getv2", ["--prod_id"=> $product_id]);
//echo config("app.url")."/onebuy/v2/".$product_id."\r\n";
return redirect(config("app.url")."/onebuy/v2/".$product_id."?time=".time());

}

// zero attr
if($version=='v3') {
//Artisan::call("shopify:product:getv3", ["--prod_id"=> $product_id]);
//echo config("app.url")."/onebuy/v3/".$product_id."\r\n";
return redirect(config("app.url")."/onebuy/v3/".$product_id."?time=".time());

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
<tr>
<th>Product ID</th>
<th>Title</th>
<th>handle</th>
<th>Status</th>
<th>Checkout</th>
<th>updated at</th>
<th>Action</th>
</tr>
Expand Down Expand Up @@ -82,10 +84,22 @@
},
{
data: 'title'
},
},{
data: 'handle',
render: function(data, type, row, meta) {
return '<a href="<?php echo $shopifyStore->shopify_app_host_name?>'+data+'" target="_blank">'+data+'</a>';
}
}
,
{
data: 'status'
},
},{
data: 'product_id',
render: function(data, type, row, meta) {
return '<a href="./products/checkout-url-get/'+data+'" target="_blank" class="btn btn-primary btn-sm">Checkout URl</a>';
}
}
,
{
data: 'updated_at'
},
Expand Down
1 change: 1 addition & 0 deletions packages/Nicelizhi/Shopify/src/Routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Route::controller(ProductController::class)->prefix('products')->group(function () {
Route::get('', 'index')->name('admin.shopify.products.index');
Route::get('sync/{product_id}', 'sync')->name('admin.shopify.products.sync');
Route::get('checkout-url-get/{product_id}', 'checkoutUrlGet')->name('admin.shopify.products.checkout-url-get');



Expand Down

0 comments on commit 25a2869

Please sign in to comment.