I'm building a mobile app using angular/ionic 4 and I have a problem with custom woocommerce product type ( my backend ) called yith-composite
.
(我正在使用angular / ionic 4构建移动应用程序,而名为yith-composite
自定义woocommerce产品类型 (我的后端)存在问题。)
The problem is how to add this type of product to the cart?
(问题是如何将这种类型的产品添加到购物车?)
I'm using a plugin called cocart for making custom rest API endpoints for cart and it's working great but only support the default types of woocommerce products like simple
or variable
.
(我正在使用一个名为cocart的插件来为cart创建自定义的rest API端点,它的工作原理很好,但仅支持woocommerce产品的默认类型,例如simple
或variable
。)
I tried to make a custom endpoint that submits the form add-to-cart
in each product page and it's working only if I pass the user cookie in headers
not token which mean I can't send a request with my angular as there's no token will be set so will return undefined.
(我试图制作一个自定义终结点,该终结点在每个产品页面中都提交add-to-cart
表单,并且仅当我在headers
而不是令牌)中传递用户cookie时,该端点才起作用,这意味着我无法发送带有角度的请求,因为没有令牌将被设置,因此将返回未定义。)
The code for that custom endpoint I have made
(我为该自定义端点编写的代码)
add_action('rest_api_init', function () {
register_rest_route( 'yith-composite', 'add-item',array(
'methods' => 'POST',
'callback' => 'yith_custom_add_item'
));
});
function yith_custom_add_item( WP_REST_Request $request ) {
$currentuserid_fromjwt = get_current_user_id();
$user = get_user_by( 'id', $currentuserid_fromjwt );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login, $user );
$product_id = $_GET['id'];
$url = get_permalink( $product_id );
$cookie = '';
foreach ($_COOKIE as $key => $value) {
$cookie .= $key . '=' . $value . ';';
}
$response = wp_remote_post( $url, array(
'headers' => array(
'Cookie' => $cookie,
// 'Authorization' => $request->get_header('Authorization'),
'Content-Type' => 'application/x-www-form-urlencoded',
),
'body' => $_POST,
));
if ( !empty($response) ) {
return $response
}
}
}
ask by hesham shawky translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…